OpenJDK 在 jar 中找不到 main class 而 OracleJDK 可以

OpenJDK cannot find main class in jar while OracleJDK can

我有问题,我根本无法 运行 任何带有 OpenJDK 的 jar,而对于普通的 OracleJDK 是没有问题的。

OpenJDK # java -version

openjdk version "1.8.0_101"
OpenJDK Runtime Environment (IcedTea 3.1.0) (suse-14.3-x86_64)
OpenJDK 64-Bit Server VM (build 25.101-b13, mixed mode)

当我 运行 一个装有这个 JDK 的罐子时,它永远找不到主要的 class 即使它在清单中也是如此。


OracleJDK # java -version

java version "1.8.0_102"
Java(TM) SE Runtime Environment (build 1.8.0_102-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.102-b14, mixed mode)

当我用这个 JDK 启动一个 jar 时,没问题。

我是否需要在 OpenJDK 中配置一些东西,以便它可以从清单中找到主要的 class 或者 OpenJDK 无法找到的原因是什么?

编辑:

源文件结构:

-- ui  
---- Main.java  

Gradle 构建脚本:

group 'some.group'
version '0.1'

apply plugin: 'java'
apply plugin: 'application'

mainClassName = "ui.Main"

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    compile group: 'org.zeromq', name: 'jeromq', version: '0.3.5'
    compile group: 'org.controlsfx', name: 'controlsfx', version: '8.40.12'

    testCompile group: 'junit', name: 'junit', version: '4.11'
}

jar {
    manifest {
        attributes 'Implementation-Title': 'PlaceholderTitle',
                'Implementation-Version': version,
                'Class-Path': configurations.compile.collect { it.getName() }.join(' '),
                'Main-Class': mainClassName
    }
}

installDist

建造

清单:

Manifest-Version: 1.0
Implementation-Title: PlaceholderTitle
Implementation-Version: 0.1
Class-Path: jeromq-0.3.5.jar controlsfx-8.40.12.jar
Main-Class: ui.Main
//new line here

您无需配置任何内容即可启动 jar。 OracleJDK 与 OpenJDK 大部分相同,它基本上添加了一些商业功能。

如果发生奇怪的事情,最好的处理方法是用尽可能小的例子重现奇怪的事情。如果你真的不能 laucnh any jar 那么 Gradle 是一个大材小用来重现它。另一方面,用于启动 JVM 的实际命令会有所帮助。

这里是创建一个带有主 class 的 jar 并执行它的非常基本的命令 - 它应该 运行 适合任何 JDK 和任何配置。

$ java -version
java version "1.8.0_111"
Java(TM) SE Runtime enter code hereEnvironment (build 1.8.0_111-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.111-b14, mixed mode)
$ echo 'public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } }' > HelloWorld.java
$ javac HelloWorld.java
$ jar cfe HelloWorld.jar HelloWorld HelloWorld.class
$ java -jar HelloWorld.jar
Hello, World!

好的,我找到了答案。问题是我有一个 JavaFX 应用程序并且安装的 OpenJDK 运行时环境不支持它,我不明白,因为 JavaFX 是 Java 8.

OpenJDK lib/ext 文件夹:

cldrdata.jar       nashorn.jar
dnsns.jar          sunec.jar
icedtea-sound.jar  sunjce_provider.jar
jaccess.jar        sunpkcs11.jar
localedata.jar     zipfs.jar
meta-index

熟悉的可以看看jfxrt.jar少了。这解释了为什么它不能加载 Main-Class,因为它继承自 javafx.application.Application