2

I have a simple Java program that consists of two .java files. It runs fine when executed from my IDE (IntelliJ). It also compiles (via the javac utility) just fine. However, when I attempt to run it from the Windows command prompt (using java <<myClassName>>) I get this error:

Exception in thread "main" java.lang.UnsupportedClassVersionError: JVMCFRE003 bad major version; class=RunExport, offset=6
        at java.lang.ClassLoader.defineClassImpl(Native Method)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:364)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:154)
        at java.net.URLClassLoader.defineClass(URLClassLoader.java:777)
        at java.net.URLClassLoader.access$400(URLClassLoader.java:96)
        at java.net.URLClassLoader$ClassFinder.run(URLClassLoader.java:1225)
        at java.security.AccessController.doPrivileged(AccessController.java:366)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:660)
        at java.lang.ClassLoader.loadClassHelper(ClassLoader.java:942)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:851)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:827)
        at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:494)

I have read various questions and proposed solutions for this error, but I have not been able to solve it. It appears that the version of Java used to compile the code is not recognized as being the same as that of the JRE in which I am running it. I am using jdk1.8.0_91. The javac.exe and java.exe files I am using are both in C:\Program Files\Java\jdk1.8.0_91\bin. This is also the path defined in my Windows PATH environment variable.

7
  • 2
    try java -version and javac -version to be sure which version is being executed. is RunExport one of the classes you created/compiled? The Exception should also state what the bad version number is, could help finding the correct java version to use. Commented Jun 30, 2017 at 18:55
  • @CarlosHeuberger, I think you may have uncovered the issue. javac -version returns 1.8.0_91 and java -version returns 1.7.0. How do I ensure these are in sync? And yes, RunExport is the class I am compiling. There is one other class that also gets automatically compiled as it is referenced by RunExport. Commented Jun 30, 2017 at 19:01
  • 2
    1) try using the Java control panel - I believe there is a Java tab with options to set the java version to be used. 2) if that was no help, change the order of directories on the PATH env. var. the java one should be before the windows one; 3) use the full path to start java like C:\Program Files\Java\jdk1.8.0_91\bin\java.exe <classname> Commented Jun 30, 2017 at 19:18
  • @CarlosHeuberger, thank you so much! I was able to get it working simply by using the full path to java.exe ("C:\Program Files\Java\jdk1.8.0_91\bin\java.exe" RunExport). If you post the answer, I will gladly accept it. Thanks again for your help! Commented Jun 30, 2017 at 19:48
  • If you don’t need that Java 7 installation, you could just deinstall it… Commented Jul 3, 2017 at 11:01

2 Answers 2

3

This error means you are compiling and running with different, incompatible versions of Java.

Do java -version and javac -version to find out what versions these commands are actually using.

Then, to fix the version, you have three options:

  • Change which version of Java is used through the Java control panel.

  • Change which version of Java is used by editing your PATH environment variable. The path to your desired Java version should come before the path to any other Java versions.

  • If neither of these seems to work, or if this is a one-time use, you can simply specify the full path when running/compiling your program:

    C:\Program Files\Java\jdk1.8.0_91\bin\java.exe
    

This material taken from comments by Carlos Heuberger

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

1 Comment

I answered this just so we have the answer as an answer (not a comment) and can mark the question as solved. If Carlos comes along and writes an answer, I'll delete this one.
-1

It is possible that your IntelliJ is referring to default JRE. Go through this link https://www.jetbrains.com/help/idea/2017.1/defining-a-jdk-and-a-mobile-sdk-in-intellij-idea.html to setup intelliJ. Ensure is points to the JRE configured as your path.

1 Comment

Thanks for the info. I followed the instructions in the link and added the JDK just to be sure (even though everything in there already appeared to be the right version) and I still have the same issue. I am able to run it from Intellij but not from the command prompt.

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.