繁体   English   中英

在OracleJDK可以的时候,OpenJDK在jar中找不到主类

[英]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运行一个jar时,它永远找不到主类,即使它在清单中也很难。


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中配置一些东西,以便它可以从清单中找到主类或者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

好的,我找到了答案。 问题是我有一个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如果你熟悉它, jfxrt.jar就会丢失。 这解释了为什么它无法加载Main-Class,因为它继承自javafx.application.Application

您无需为jar启动任何配置。 OracleJDK与OpenJDK大致相同,它基本上增加了一些商业功能。

如果发生了一些奇怪的事情,那么最好的方法就是用尽可能小的例子重现怪异。 如果你真的不能使用任何罐子,那么Gradle很难重现它。 另一方面,用于启动JVM的实际命令会很有帮助。

以下是使用主类创建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!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM