0

Good morning everyone. I read many posts related to this problem but the solution is not clear. I Just bought a new laptop, installed the last version of JDK, and wrote a simple "Hello world" java program in order to see if everything was ok with compiling and executing. So, compiling is fine, the file .class is created, but when I try to execute, it displays this:

Exception in thread "main" java.lang.UnsupportedClassVersionError: File1 has been compiled by a more recent version of the Java Runtime (class file version 59.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)

I think I already found the problem, I'm just here to confirm: In addition to the JDK v15.0.1, I also installed a JRE v1.8.0_271 in order to play some games which require a specific installation of the JRE manually (Minecraft for example). I suspect that JAVA is compiling with the JDK 15.0.1 (of course), BUT then it tries to execute it with JRE 1.8... That's why, maybe, it displays a version error... May someone confirm or disprove what I said?

(I suspect this because if it's only JDK's fault, How could they implement a JDK which can compile but not execute its own programs? xD)

AleMaffe
  • 23
  • 6
  • It is not "JAVA is compiling". In the end, you, the user tell some component to do something. So most likely, *whatever* you do to *build* (compile) some stuff ... is different from how you later *run* that stuff. But given that we only have vague descriptions, we really cant help with that. See [mcve]. In any case: the error is clear. You compile with a newer javac, and an older java jre wont accept that. – GhostCat Dec 17 '20 at 12:59
  • regardless of the meticulous definitions, which we can now gloss over, you think I was right or not? Am i compiling with the JDK 15.1 and then it executing with the JRE 1.8? What should I do to compile correctly? – AleMaffe Dec 17 '20 at 13:07
  • That is what I just told you. And as you arent telling us HOW exactly you compiled whatever you compiled ... how could I tell you what to fix? I would start by: A) understanding what the thing you want to compile prefers as version ... to then B) uninstall any other version of java. – GhostCat Dec 17 '20 at 13:17
  • Well, you are right. A) I'm compiling a simple java file "Hello world" on windows with ```javac File1.java``` and then trying to execute with ```java File1``` from windows powershell. You already know which folders I have in my "C:->Program Files->Java" folder. (jdk15.0.1 AND JRE1.8.0_271). I would like to compile and execute directly with the JDK of course. B) As I said before, I'll prefer not to uninstall the JRE, because it's still useful to play some games which require a manually installation of it (Minecraft e.g., but not only). – AleMaffe Dec 17 '20 at 13:30
  • SO, my question was: Is there a way to maintain ALL those 2 versions, avoiding to use JRE to execute? – AleMaffe Dec 17 '20 at 13:32
  • *You already know which folders* ...no, I dont. You gave us a mix of explanations, and basically just that single stack trace. That is insufficient information to give guidance. As written above, see [mcve] first. And please note: there are plenty of resources that tell you "In general" how you can use multiple versions of java on windows. See https://stackoverflow.com/questions/26993101/switching-between-different-jdk-versions-in-windows for example. – GhostCat Dec 17 '20 at 13:38
  • Ahem, if I wrote in the answer I downloaded those two versions, basically I should have them... After reading my last comment, I really can't understand what sort of other information you need... anyway, thanks for your time – AleMaffe Dec 17 '20 at 15:20

0 Answers0