2013-09-10 333 views
0

我有我想要編譯,以便它可以不用常規的系統上運行Groovy腳本。我正在使用this文章作爲參考。它似乎編譯好,但是當我運行腳本,它啓動,然後給我一個java.lang.ClassNotFoundException。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][email protected]) 
       { 
        case "0": 
         [email protected] = "8B" 
         break 
        case "1": 
         [email protected] = "3B" 
         break 
        case "2": 
         [email protected] = "10B" 
         break 
        case "3": 
         [email protected] = "5B" 
         break 
        case "4": 
         [email protected] ="12B" 
         break 
        case "5": 
         [email protected] ="7B" 
         break 
        case "6": 
         [email protected] ="2B" 
         break 
        case "7": 
         [email protected] ="9B" 
         break 
        case "8": 
         [email protected] ="4B" 
         break 
        case "9": 
         [email protected] ="11B" 
         break 
        case "10": 
         [email protected] ="6B" 
         break 
        case "11": 
         [email protected] ="1B" 
         break 
        case "12": 
         [email protected] ="5A" 
         break 
        case "13": 
         [email protected] ="12A" 
         break 
        case "14": 
         [email protected] ="7A" 
         break 
        case "15": 
         [email protected] ="2A" 
         break 
        case "16": 
         [email protected] ="9A" 
         break 
        case "17": 
         [email protected] ="4A" 
         break 
        case "18": 
         [email protected] ="11A" 
         break 
        case "19": 
         [email protected] ="6A" 
         break 
        case "20": 
         [email protected] ="1A" 
         break 
        case "21": 
         [email protected] ="8A" 
         break 
        case "22": 
         [email protected] ="3A" 
         break 
        case "23": 
         [email protected] ="10A" 
         break 
        default: 
         println "something went wrong!" 
         println it.MUSICAL_KEY[0][email protected] 
         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 

這是我使用

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} ...\'") 

的GroovyWrapper這是我使用編譯命令:

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 ...' 

我失去了somethi沿着這些線應該添加缺失的類嗎? Wrapper在編譯時還創建了另外兩個類。

+0

腳本是什麼?任何舊腳本是否失敗?或者只是這個特定的未顯示的腳本? –

+0

@tim_yates由於這是我的第一個Groovy程序,因此我沒有其他腳本來測試它。我會繼續寫下來,真的很快。我還在腳本中添加了腳本。 –

+0

@tim_yates一個簡單的「HelloWorld」的劇本工作正常 –

回答

1

我的問題有一些東西需要與包我的劇本。我感動的文件出來的包,到精編src文件夾和一切。我運行了jar文件,它也工作。

相關問題