Zeus
Zeus

Reputation: 6566

Java -version prints a wrong version

I have multiple versions of Java installed in the environment(I know it is bad). I've set the JAVA_HOME to jdk 1_4 directory. This is not installed, but extracted from zip and placed it somewhere. We have java 1.8 JRE also installed in the system. I never set the path to this installed directory. Now i'm trying to run some ant script that depends on jdk 1_4. I get some exception saying that it is not able to find tools.jar in java1.8... .

My question is that when the path and java_home are set to jdk 14 why does the ant look for 1.8 version of java? I'm confused.

Update: I have looked up the duplicate issue.

Additional details: Java -version prints 1.8 with jdk 14 in the path and java_home. I uninstalled 1.8. reopened cmd tried java -version, now it errors out saying that it is not able to find java1.8 instead of trying to find the next java available in the path or java_home.

Error shown:

C:\Users\usrpao>java
Error: could not open `C:\Program Files\Java\jre8\lib\amd64\jvm.cfg'

Upvotes: 1

Views: 7608

Answers (3)

David W.
David W.

Reputation: 107030

I have multiple versions of Java installed in the environment(I know it is bad). I've set the JAVA_HOME to jdk 1_4 directory.

Not wrong at all. I have a similar setup because I have to switch between Java version.

  • First, install Java JDKs in the root of your drive. No spaces in the directory names. For example, C:\Java\Java-1.6-21 and C:\Java\Java-1.7-5.
  • In your Environment Variables section in your System Control Panel (under Advance), create an environment variable to point to each one of these Java Home directories. For example, JAVA_HOME_17 = C:\Java\Java-1.7.5 and JAVA_HOME_16 = C:\Java\Java-1.6-21. This should be a System Environment variable.
  • Create a JAVA_HOME environment variable that points to the Java version you want: JAVA_HOME = %JAVA_HOME_17%.
  • Now in the System PATH, prefix the PATH (the very first entry) with %JAVA_HOME%\bin.

When you open a console window, your default java and javac commands will be the correct Java version.

If you need to change a version, change the JAVA_HOME environment variable to point to the correct environment variable and open a new console window. Now that new Java will be in your path.

NOTE: It is vitally important that %JAVA_HOME%\bin is in the first part of your PATH before C:\Windows\System32. You don't want the java.exe that exists in that directory to be your default java.

Upvotes: 7

Zeus
Zeus

Reputation: 6566

Multiple java versions in the system is the problem, I uninstalled all the java versions first. Restarted my machine. Path and java home points to the jdk 1.4. java -version now points to the version specified in the path. Thanks all.

Upvotes: 0

radai
radai

Reputation: 24192

You haven't specified the operating system, so:

  • on windows, java installs a java.exe in C:\Windows\system32, which is probably on the path before java_home, and so gets picked up
  • on Linux, various distributions that support multiple installed java versions will also have some symlink earlier on the path. Run 'which java' to determine where that symlink is

Upvotes: 1

Related Questions