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 が作成したさらに 2 つのクラスもあります。

4

1 に答える 1

3

私の問題は、スクリプトが含まれていたパッケージと関係がありました。ファイルをパッケージから src フォルダーに移動し、すべてが正常にコンパイルされました。jarファイルを実行しましたが、同様に機能しました。

于 2013-09-11T13:24:40.207 に答える