Home > Mobile >  Java - JAR file would not execute from the terminal command line
Java - JAR file would not execute from the terminal command line

Time:05-13

I am relatively new to Java I finished tutorial where an example of how to create and run JAR file was explained. Could you give an advice of what should be done in order to run my JAR in command line, please?

I created JAR file:

enter image description here

This Jar file was copied to the desktop

enter image description here

I would expect that command: java -jar 06_02.jar should execute the file.

In IntelliJ terminal I switch to the desktop directory (where the file is located) and run:
java -jar 06_02.jar

enter image description here

This results with an error:

Error: A JNI error has occurred, please check your installation and try again Exception in thread "main" java.lang.UnsupportedClassVersionError: com/example/java/Main has been compiled by a more recent version of the Java Run time (class file version 55.0), this version of the Java Runtime only recognizes class file versions up to 52.0

at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.access$100(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)

At the same time if I execute JAR file from IntelliJ GUI it is working:

enter image description here

enter image description here

My Project settings are:

enter image description here

Please, help me to solve this error.

CodePudding user response:

When you type java -jar myjar.jar on the command line, your command line first does all sorts of stuff to figure out what you actually mean. In particular, java gets translated to the absolute path of some executable file. It does this (generally - each shell is different, but most work this way) by scanning the environment variable known as the PATH for directorys, and then checking each one, in turn, if it contains a file named java.exe. If it does, that's the one that is assumed you meant.

The java.exe that your shell finds in this manner is the java.exe from a JDK8 installation. I know that because class file v52 is the one JDK8 produces/is the max. that a JDK8 can run.

Your code, as your intellij screenshots show, is configured as at least JDK11 (I know because version 55 is what JDK11s and up produce in -release 11 mode, which is what you're doing, given those screenshots).

The fix, therefore, is one of these 3:

  • Explicitly run this with a JDK11. Don't run java -jar foo.jar, run C:\Program Files\Adoptium\JDK11\bin\java.exe -jar myjar.jar or whatnot (that path isn't quite correct, I don't know what your harddisk looks like, but you get the idea).
  • Figure out how to configure the default JDK on your system. This depends, again, on many factors; every OS does this differently. Sometimes, the java.exe that is run is just a wrapper executable that checks some other setting, such as JAVA_HOME or a registry key, for what the 'active JDK' is, and then runs that instead. In which case you'd have to fix JAVA_HOME or whatnot - check if there's a java config app installed on your system. Alternatively, just uninstall JDK8 instead, and pray it falls back to JDK11 (or re-install that). Sometimes it's just a matter of editing PATH.
  • Tell intellij that you want to target JDK8 instead. Note that you then won't be able to use any language features (such as var) or API (such as the new HTTP client) that was introduced in JDK9, 10, or 11, of course.

Presumably you should be working on the second option here.

CodePudding user response:

You have compiled your code for Java 11 (class file format version 55) and you try to run it with Java 8 (class file format version 52).

Java 8 does not understand the class file formats from later Java versions.

  • Related