编译groovy类

时间:2012-08-16 22:46:47

标签: groovy gradle

我尝试编译gradle快速启动示例,它带有gradle(在samples / groovy / quickstart目录中)。 gradle compileGroovygradle jar都成功完成,但我无法运行其中任何一项:

c:\tmp\quickstart\build\classes\main\org\gradle>java Person
Exception in thread "main" java.lang.NoClassDefFoundError: Person (wrong name: org/gradle/Person)
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClassCond(Unknown Source)
        at java.lang.ClassLoader.defineClass(Unknown Source)
        at java.security.SecureClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.access$000(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)
Could not find the main class: Person.  Program will exit.

c:\tmp\quickstart\build\libs>java -jar quickstart.jar
Failed to load Main-Class manifest attribute from
quickstart.jar

然后我将清单属性添加到bulid.gradle

jar {
    manifest {
        attributes("Main-Class": "org.gradle.Person")
    }
}

现在到了:

c:\tmp\quickstart\build\libs>java -jar quickstart.jar
Exception in thread "main" java.lang.NoClassDefFoundError: groovy/lang/GroovyObject
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClassCond(Unknown Source)
        at java.lang.ClassLoader.defineClass(Unknown Source)
        at java.security.SecureClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.access$000(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)
Caused by: java.lang.ClassNotFoundException: groovy.lang.GroovyObject
        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)
        ... 12 more
Could not find the main class: org.gradle.Person. Program will exit.

如何运行创建的jar? (或者我错过了重点......)

1 个答案:

答案 0 :(得分:2)

Gradle发行版中的groovy/quickstart示例并不意味着直接执行。除其他外,Person类没有main方法。把它想象成一个图书馆。