【发布时间】:2016-01-21 09:28:04
【问题描述】:
我在 one 的项目中实现了 Gradle。我使用带有 gradle 插件的 Netbeans 8.02。
结构应该是这样,来源位于jgli/src/main/java/下,资源位于jgli/src/main/resources/下
主类是jgli/src/main/java/test/Main.java
如果我通过 ide 运行它,它会在 windows 上运行,它在 linux 上运行 crashes。这就是我现在尝试通过控制台运行它的原因:
java -jar jgli.jar
但我不断得到:
错误无法找到或加载主类'test.Main'
这是我的build.gradle
apply plugin: 'java'
sourceCompatibility = '1.8'
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
// NetBeans will automatically add "run" and "debug" tasks relying on the
// "mainClass" property. You may however define the property prior executing
// tasks by passing a "-PmainClass=<QUALIFIED_CLASS_NAME>" argument.
//
// Note however, that you may define your own "run" and "debug" task if you
// prefer. In this case NetBeans will not add these tasks but you may rely on
// your own implementation.
if (!hasProperty('mainClass')) {
ext.mainClass = ''
}
repositories {
mavenCentral()
// You may define additional repositories, or even remove "mavenCentral()".
// Read more about repositories here:
// http://www.gradle.org/docs/current/userguide/dependency_management.html#sec:repositories
}
dependencies {
// TODO: Add dependencies here ...
// You can read more about how to add dependency here:
// http://www.gradle.org/docs/current/userguide/dependency_management.html#sec:how_to_declare_your_dependencies
testCompile group: 'junit', name: 'junit', version: '4.10'
compile 'org.jogamp.jogl:jogl-all-main:2.3.2'
compile 'org.jogamp.jogl:nativewindow-main:2.3.2'
compile 'org.jogamp.jogl:newt-main:2.3.2'
compile 'org.jogamp.gluegen:gluegen-rt-main:2.3.2'
}
jar {
manifest {
attributes 'Main-Class': 'test.Main'
}
}
我刚刚修改了dependecies 部分并添加了清单部分。
我尝试将'test.Main' 添加到 ext.mainClass,但没有任何改变..
Here你可以下载jar。
这是我的MANIFEST.MF:
Manifest-Version: 1.0
Main-Class: test.Main
Main.class 正确地位于 jar 中。
Main.java 有包声明package test;
也试过了
apply plugin: 'application'
mainClassName = 'test.Main'
没有成功..
我错过了什么?
【问题讨论】:
-
您能否检查一下,您的 jar 是否包含具有 MainClass 属性的有效清单文件,并且此 jar 中提供了您的 Main 类?
-
当然,
manifest似乎是正确的,Main.java在那里 -
然后检查您的 Main.java 文件中是否包含包声明,例如
package test;。没有它,类就找不到了 -
是的,它有,反正 ide 之前会触发它。
-
我刚刚找到了你的 git repo 和 Main 类的来源,似乎它实现了一些接口,这些接口在你的依赖项中提供。在这种情况下,当您运行 Main 类时,您的依赖项必须位于您的类路径中。尝试提供
-cp选项来指向依赖jar的路径
标签: java gradle find main netbeans-8