0

我尝试编译 gradle 附带的 groovy quickstart 示例(在 samples/groovy/quickstart 目录中)。gradle compileGroovy并且gradle 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?(或者我错过了重点......)

4

1 回答 1

2

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

于 2012-08-16T22:57:57.277 回答