0

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.

I also have Java 13 on my machine:enter image description here

5
  • 1
    Please never post images of text. They are not searchable, we cannot copy-paste... Always copy-paste the text and format it properly.. The error message tells you the reason for the problem. You have two different Javas - one in your IDE and one visible in PATH. The latter is too low a version to run the jar Commented May 12, 2022 at 11:48
  • If you're using JNI you need to give the LD_LIBRARY_PATH to the JDK on startup. IntelliJ is out of the picture once you try to execute in the terminal. Commented May 12, 2022 at 11:49
  • Make sure that the machine you execute your jar file on has the same or higher java version like your project. Commented May 12, 2022 at 11:49
  • duffymo's advice is incorrect here; they are being mislead by the error message. This problem has no relationship to LD_LIBRARY_PATH or JNI. Commented May 12, 2022 at 11:50
  • @g00se , Thank you for the advice about images of the text. I will follow it. Commented May 14, 2022 at 14:46

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

6 Comments

@rzwiserloot Thank you for your help. Project (code) is configured as Java 13 (see the screenshot above). I have Java 13 on my machine ( I have added the screenshot into my question). Following your advice I tried to run explicitly with JDK 13: C:\Program Files\Java\jdk-13.0.2_windows-x64_bin\jdk-13.0.2\bin> java.exe -jar 06_02.jar However, the response would be: Error: Unable to access jarfile 06_02.jar I also tried to give an absolute pass to the jar file also (-jar C:\Users\vshumitskiy\IdeaProjects\Ex_Files_Java_E...artifacts\06_02_jar\06_02.jar ). But unfortunately result is the same.
Then you didn't correctly type the absolute path - if it has spaces in it, you need to surround with quotes. That question ("How do I pass this jar file on the command line") is fundamentally different from this one (and probably too simple for SO).
@rzwiserloot Thank you again you were absolutely right. I have enclosed the whole command in quotation marks: C:\Program Files\Java\jdk-13.0.2_windows-x64_bin\jdk-13.0.2\bin> java.exe -jar "C:\Users\vod\IdeaProjects\Ex_Files_Java_EssT_APIs\Ex_Fil es_Java_EssT_APIs\Exercise Files\Ch06\06_02\out\artifacts\06_02_jar\06_02.jar" so, I am using now abolute pass to the bin folder and to the jar file and unfortunately the error is still the same: Error: A JNI error has occurred, please check your installation and try again Exception in thread "main" java.lang.UnsupporteClass...(as described above)
Try .\java.exe - it's still finding a different java. Just Java.exe on the command line doesn't neccessarily use the java.exe in that dir first, it dpeends on your PATH. ".\java.exe" instead of "java.exe" should force it.
@zwitserloot Thank you for the answer! It Does Work! So, the following will work: .\java.exe -jar "C:\Users\vod\IdeaProjects\Ex_Files_Java_EssT_APIs\Ex_F iles_Java_EssT_APIs\Exercise Files\Ch06\06_02\out\artifacts\06_02_jar\06_02.jar" After I cahnged it to be .\java.exe and not java.exe and after I provided an explicit path to the jar file it does work.
|
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.