创建Jar存档会导致无法找到或加载主类"

时间:2015-11-13 09:13:40

标签: java jar manifest

$ cat Manifest.txt 
Main-Class: org.fenix.llanfair.Llanfair

$ cat org/fenix/llanfair/Llanfair.java | grep main
public static void main(String[] args) {
$ jar cfm Llanfair.jar Manifest.txt org
$ java -jar Llanfair.jar 
Error: Could not find or load main class org.fenix.llanfair.Llanfair
$ jar -xf Llanfair.jar
$ cat Llanfair/META-INF/MANIFEST.MF 
Manifest-Version: 1.0
Created-By: 1.8.0_25 (Oracle Corporation)
Main-Class: org.fenix.llanfair.Llanfair

$ ls Llanfair/org/fenix/llanfair/ | grep Llanfair
Llanfair$1.class
Llanfair$2.class
Llanfair$MenuItem.class
Llanfair$RecentMenuItem.class
Llanfair.class
$ java -version
java version "1.8.0_25"
Java(TM) SE Runtime Environment (build 1.8.0_25-b17)
Java HotSpot(TM) 64-Bit Server VM (build 25.25-b02, mixed mode)

我希望java -jar Llanfair.jar能够启动我的应用程序,所以我不确定我在这里做错了什么。当我正常运行java代码时,我在lib /中有几个文件在我的类路径中,我是否需要包含这些文件?

我的操作系统是Mac OS 10.11.1,如果重要的话。

2 个答案:

答案 0 :(得分:0)

似乎Llanfair.jar中的文件结构错误。

根据您的评论jar -xf Llanfair.jar创建类似

的结构
Llanfair/META-INF/MANIFEST.MF 
Llanfair/org/fenix/llanfair/Llanfair.class

但是jar文件中的结构必须是

META-INF/MANIFEST.MF 
org/fenix/llanfair/Llanfair.class

答案 1 :(得分:-1)

我错过了清单中的Class-Path:值,为什么它会以这种方式出现在我之外。

$ cat Manifest.txt
Class-Path: lib/ui.jar lib/jnativehook-2.0.2.jar
Main-Class: org.fenix.llanfair.Llanfair

$ jar cfm Llanfair.jar Manifest.txt org lib
$ java -jar Llanfair.jar 
<normal application output>
^C$ jar -xf Llanfair.jar
$ ls Llanfair
ls: Llanfair: No such file or directory
<manually extracted Llanfair.jar using Archive Utility>
$ cat Llanfair/META-INF/MANIFEST.MF 
Manifest-Version: 1.0
Class-Path: lib/ui.jar lib/jnativehook-2.0.2.jar
Created-By: 1.8.0_25 (Oracle Corporation)
Main-Class: org.fenix.llanfair.Llanfair

$

编辑:它有点复杂。我试图在Llanfair.jar中包含jar依赖项。这是不允许的,因为/ reason /。以上是有效的,因为我在相同的目录中运行jar - 所以lib / ui.jar存在,因为我正在使用它来构建。