Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenJDK cannot find main class in jar while OracleJDK can

I have the problem, that I cannot run any jars with OpenJDK at all, where as with the normal OracleJDK it is no problem.

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)

when I run a jar with this JDK it can never find the main class even tough it is in the manifest.


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)

when I launch a jar with this JDK it is no problem.

Do I need to configure something in OpenJDK so it can find the main class from the manifest or what is it that OpenJDK fails to do so?

Edit:

Source file structure:

-- ui  
---- Main.java  

Gradle build script:

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
    }
}

building with installDist

Manifest:

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
like image 454
Jhonny007 Avatar asked Feb 05 '23 20:02

Jhonny007


2 Answers

Ok I found the answer. The problem was that I had a JavaFX application and the OpenJDK runtime enviroment that was installed doesn't support that, which I don't understand, since JavaFX is part of the standard in Java 8.

OpenJDK lib/ext folder:

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

as you can see if you are familiar with it jfxrt.jar is missing. Which explains why it can't load the Main-Class, since it inherited from javafx.application.Application.

like image 179
Jhonny007 Avatar answered Feb 08 '23 15:02

Jhonny007


You don't need to configure anything for a jar to start. OracleJDK is mostly the same as OpenJDK, it basically adds a couple of commercial features.

If something weird is happening the best way to proceed is to reproduce the weirdness with the smallest example possible. If you really can't laucnh any jar then Gradle is an overkill to reproduce it. On the other hand, an actual command that you use to start the JVM would be helpful.

Here are the very basic commands that create a jar with a main class and execute it - it should run fine with any JDK and any configuration.

$ 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!
like image 45
Stanislav Lukyanov Avatar answered Feb 08 '23 14:02

Stanislav Lukyanov