The mysterious java.exe was the reason why javac and java versions could not be matched no matter how many times I tried.

environment

Purchased by: Mouse Computer Co., Ltd. Notebook / Desktop: Desktop PC OS: Windows 10 Home 64-bit CPU: Intel (R) Core (TM) i5-7400 processor (4 cores / 4 threads / 3.00GHz / TB up to 3.50GHz / 6MB cache) Memory: 16 GB (8 GB at the time of purchase) HDD: 1TB HDD (7200rpm / 6Gbps compatible) SSD: None Graphics: Intel HD Graphics 630 Implementation date of the following contents: November 12, 2019

Execution error in Hello world

I decided to start studying Java, so I installed Eclipse and JDK to build the environment, and translated Eclipse into Japanese. I wanted to output "Hello world" immediately, so I saved almost the copy and paste code in Hello.java and > javac Hello.java Compile successfully with. > java Hello I ran it in, but an error occurred.

Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.UnsupportedClassVersionError: Hello has been compiled by a more recent version of the Java Runtime (class file version 57.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)

After checking, if the versions of "javac (JDK)" and "java (JRE)" are different (especially the latter is older), such an error will occur. When I actually checked the version,

> javac -version
javac 13.0.1

> java -version
java version "1.8.0_231"
Java(TM) SE Runtime Environment (build 1.8.0_231-b11)
Java HotSpot(TM) Client VM (build 25.231-b11, mixed mode)

After all it is different. I thought that the JRE that I originally put in was older, so I updated it from the official website to the latest version, but the version display does not change. Therefore, I uninstalled all Java related (JDK, JRE) from "Control Panel"-> "Uninstall a program", and deleted all Paths that seemed to be related from "Environment variable settings". After that, I reinstalled the JDK (it seems that the JRE will be included at the same time) and re-passed the Path, but the version display does not change.

When I uninstalled both of them and ran the version check command with Java not completely installed, the following was displayed.

> javac -version
'javac'Is an internal or external command,
It is not recognized as an operable program or batch file.

> java -version
Error: opening registry key 'Software\JavaSoft\Java Runtime Environment'
Error: could not find java.dll
Error: Could not find Java SE Runtime Environment.

It seems that the command itself is invalid for javac, but is javac recognized as a command and an error occurs?

Discover the mysterious java.exe

At this time, I remembered that some of the deleted Java-related paths were unknown (certainly C: \ ProgramData \ Oracle \ Java), and when I opened that folder, qiita_java.png There was java.exe that should have been completely erased. The update date is the date when the PC was purchased. Is it the one that was originally included? I took a closer look at the "Uninstall a program" list again, but couldn't find a corresponding program. I changed the name of the "Oracle" folder to "Oracle_nazonojava" and executed the java version information command again.

> java -version
'java'Is an internal or external command,
It is not recognized as an operable program or batch file.

Like javac, the command itself is no longer recognized. I should have deleted the Path to the changed folder (from both the user environment variable and the system environment variable), but why does the command result change when the folder name is changed? In fact, Path may have passed (lack of study).

When I reinstalled the JDK in this state, the versions matched successfully (java changed to the same version as javac).

> javac -version
javac 13.0.1

> java -version
java version "13.0.1" 2019-10-15
Java(TM) SE Runtime Environment (build 13.0.1+9)
Java HotSpot(TM) 64-Bit Server VM (build 13.0.1+9, mixed mode, sharing)

"Hello world" was also successfully compiled and executed.

Finally

When I googled, there are cases where the versions of java and javac are different, but both have been resolved by reinstalling, and I could hardly find anything similar to this example. (The only case where the Java version remains 1.8.0_231 no matter how many times it is reinstalled, which is almost the same as this time, existed as the Google cache of the question (deleted) of Yahoo! Chiebukuro.) It may be a big problem that depends on the environment (PC purchaser?), But there may be people who have similar symptoms, so I will write it as an article.

Recommended Posts

The mysterious java.exe was the reason why javac and java versions could not be matched no matter how many times I tried.
[CentOS8] Since the javac command could not be used, install the java development environment.
"Java.lang.NoClassDefFoundError: Could not initialize class java.nio.file.FileSystems $ DefaultFileSystemHolder" occurs even though I can check the classpath and encoding many times.
Why Java was the target language I hate