1

我有一个我正在尝试编译的 groovy 脚本,以便它可以在没有 groovy 的系统上运行。我用这篇文章作为参考。它似乎可以编译,但是当我运行脚本时,它会启动,然后给我一个 java.lang.ClassNotFoundException。

package org.something.groovy.project

import groovy.xml.MarkupBuilder
import groovy.xml.StreamingMarkupBuilder
class LibraryParser
{
    public static void main(args)
    {
        println "setting file path"
        def xmlFile = "collection.nml"
        println "parsing collection"
        def xml = new XmlParser().parse(xmlFile)
        println "beginning loop"
        def counter = 0

        xml.COLLECTION.ENTRY.each{ 

            if (it.MUSICAL_KEY[0] != null)
            {
                switch (it.MUSICAL_KEY[0].@VALUE)
                {
                    case "0":
                        it.INFO.@KEY = "8B"
                        break
                    case "1":
                        it.INFO.@KEY = "3B"
                        break
                    case "2":
                        it.INFO.@KEY = "10B"
                        break
                    case "3":
                        it.INFO.@KEY = "5B"
                        break
                    case "4":
                        it.INFO.@KEY ="12B"
                        break
                    case "5":
                        it.INFO.@KEY ="7B"
                        break
                    case "6":
                        it.INFO.@KEY ="2B"
                        break
                    case "7":
                        it.INFO.@KEY ="9B"
                        break
                    case "8":
                        it.INFO.@KEY ="4B"
                        break
                    case "9":
                        it.INFO.@KEY ="11B"
                        break
                    case "10":
                        it.INFO.@KEY ="6B"
                        break
                    case "11":
                        it.INFO.@KEY ="1B"
                        break
                    case "12":
                        it.INFO.@KEY ="5A"
                        break
                    case "13":
                        it.INFO.@KEY ="12A"
                        break
                    case "14":
                        it.INFO.@KEY ="7A"
                        break
                    case "15":
                        it.INFO.@KEY ="2A"
                        break
                    case "16":
                        it.INFO.@KEY ="9A"
                        break
                    case "17":
                        it.INFO.@KEY ="4A"
                        break
                    case "18":
                        it.INFO.@KEY ="11A"
                        break
                    case "19":
                        it.INFO.@KEY ="6A"
                        break
                    case "20":
                        it.INFO.@KEY ="1A"
                        break
                    case "21":
                        it.INFO.@KEY ="8A"
                        break
                    case "22":
                        it.INFO.@KEY ="3A"
                        break
                    case "23":
                        it.INFO.@KEY ="10A"
                        break
                    default:
                        println "something went wrong!"
                        println it.MUSICAL_KEY[0].@VALUE
                        break
                }
            }
        }



        new File("C:/workspacGROOVY/org.something.groovy/collection_out.nml").withWriter('UTF-8') { out ->
            out << new StreamingMarkupBuilder().bind { mkp.pi( xml:[ version:'1.0', encoding: 'UTF-8', standalone:'no' ] ) }
            new XmlNodePrinter(new PrintWriter(out)).print(xml)
        }
        println "Finished"
    }
}

命令提示符中的错误:

setting file path //normal output of the script
parsing collection
beginning loop
Exception in thread "main" java.lang.NoClassDefFoundError: org/something/groovy/
project/LibraryParser$_main_closure1
        at org.something.groovy.project.LibraryParser.main(LibraryParser.groovy:
18)
Caused by: java.lang.ClassNotFoundException: org.something.groovy.project.Librar
yParser$_main_closure1
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 1 more

这是我正在使用的 GroovyWrapper

package org.something.groovy.project
import groovy.xml.MarkupBuilder
import groovy.xml.StreamingMarkupBuilder
/*
 * Copyright 2002-2007 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * Wrap a script and groovy jars to an executable jar
 */
def cli = new CliBuilder()
cli.h( longOpt: 'help', required: false, 'show usage information' )
cli.d( longOpt: 'destfile', argName: 'destfile', required: false, args: 1, 'jar destintation filename, defaults to {mainclass}.jar' )
cli.m( longOpt: 'mainclass', argName: 'mainclass', required: true, args: 1, 'fully qualified main class, eg. HelloWorld' )
cli.c( longOpt: 'groovyc', required: false, 'Run groovyc' )

//--------------------------------------------------------------------------
def opt = cli.parse(args)
if (!opt) { return }
if (opt.h) {
  cli.usage();
  return
}

def mainClass = opt.m
def scriptBase = mainClass.replace( '.', '/' )
def scriptFile = new File( scriptBase + '.groovy' )
if (!scriptFile.canRead()) {
   println "Cannot read script file: '${scriptFile}'"
   return
}
def destFile = scriptBase + '.jar'
if (opt.d) {
  destFile = opt.d
}

//--------------------------------------------------------------------------
def ant = new AntBuilder()

if (opt.c) {
  ant.echo( "Compiling ${scriptFile}" )
  org.codehaus.groovy.tools.FileSystemCompiler.main( [ scriptFile ] as String[] )
}

def GROOVY_HOME = new File( System.getenv('GROOVY_HOME') )
if (!GROOVY_HOME.canRead()) {
  ant.echo( "Missing environment variable GROOVY_HOME: '${GROOVY_HOME}'" )
  return
}

ant.jar( destfile: destFile, compress: true, index: true ) {
  fileset( dir: '.', includes: scriptBase + '*.class' )

  zipgroupfileset( dir: GROOVY_HOME, includes: 'embeddable/groovy-all-*.jar' )
  zipgroupfileset( dir: GROOVY_HOME, includes: 'lib/commons*.jar' )
  // add more jars here

  manifest {
    attribute( name: 'Main-Class', value: mainClass )
    attribute(name: 'Class-Path', value: 'embeddable/groovy-all-2.1.1.jar')
  }
}

ant.echo( "Run script using: \'java -jar ${destFile} ...\'" )

这是我用来编译它的命令:

C:\workspacGROOVY\org.something.groovy\src>groovy org\something\groovy\project\G
roovyWrapper -c -m org/something/groovy/project/LibraryParser
     [echo] Compiling org\something\groovy\project\LibraryParser.groovy
      [jar] Building jar: C:\workspacGROOVY\org.something.groovy\src\org\somethi
ng\groovy\project\LibraryParser.jar
     [echo] Run script using: 'java -jar org/something/groovy/project/LibraryPar
ser.jar ...'

我是否遗漏了一些应该添加缺失类的内容?Wrapper 在编译时还创建了另外两个类。

4

1 回答 1

3

我的问题与我的脚本所在的包有关。我将文件从包中移出到 src 文件夹中,一切都编译得很好。我运行了 jar 文件,它也能正常工作。

于 2013-09-11T13:24:40.207 回答