Home > database >  Confused about what jdk and jre my command prompt is using for java
Confused about what jdk and jre my command prompt is using for java

Time:08-05

I was super confused about compatibility on a java tool I was using and ended up downloading different versions of java so now I do not know what versions of jdk or jre I am actually using. When I compile I use -- release 8 in my command prompt, what does that exactly mean?

Currently my programs folder has jdk-18 in the java folder and my program files(x86) folder has

  • jdk1.6.0_45

  • jdk 1.7.0_80

  • jre 1.8.0_ 321

  • jre6

  • jre7

My java home environment variable is C:\Program Files\Java\jdk-18

My classpath has the file C:\Program Files (x86)\Java\jdk1.7.0_80\lib\tools.jar

when I type java -version in my command prompt I get

java version "1.8.0_321"
Java(TM) SE Runtime Environment (build 1.8.0_321-b07)
Java HotSpot(TM) Client VM (build 25.321-b07, mixed mode)

So does that mean my cmd uses jre 1.8.0_ 321? What about the jdk? How do I change it if I need to?

CodePudding user response:

When you run java -version - first java found in PATH environment variable is used. To be sure use commands where java (Windows) or which java (Linux) - it will tell you the location.

When you run mvn install - JDK found in JAVA_HOME environment variable is used. To be sure use mvn -version - it will tell you the location.

When you run from IDE - IDE settings matter, usually you specify JDK per project.

When you run javac --release 8 Something.java you are asking compiler to produce output compatible with the version you specified - it has nothing to do with JDK you are actually running it on. This flag was added in JDK 9, so if it doesn't fail for you then it means you are running on JDK>=9.

If you want to be 100% sure just fully qualify the path - for example on Windows "C:\Program Files\Java\jdk1.8.0_152\bin\javac.exe" Something.java

CodePudding user response:

So does that mean my cmd uses jre 1.8.0_ 321?

Yes.

What about the jdk?

Unclear.

Run javac -version and that will tell you what version compiler you will run by default.

The versions of the Java tools that you get when you type java, javac, jar (etcetera) at the CMD prompt are solely determined by the PATH environment variable setting in effect at the command prompt.

The other variables do other things ... but not this.

How do I change it if I need to?

If you want to change what typing java does, change PATH.

Note that the PATH variable is a standard Windows environment variable that affects all* commands, not just the Java tools. There should be lots of tutorials on how to set PATH, and on the Windows CMD prompt in general. You should probably take the time to read them.

You can also just use the absolute path for the Java tools; e.g. "C:\Program Files (x86)\Java\jdk1.7.0_80\bin\java.exe" is probably the correct path for the Java 7 java command. (You can easily check ... and find the correct path by looking at the installation tree.)


Regarding your other variables:

My java home environment variable is C:\Program Files\Java\jdk-18

That is presumably JAVA_HOME. That tells (many) 3rd-party tools which Java tools to use. But unless you have configured PATH to depend on JAVA_HOME, it won't alter what typing java does.

My classpath has the file C:\Program Files (x86)\Java\jdk1.7.0_80\lib\tools.jar

That is presumably CLASSPATH. That variable provides a default path that java will use to find classes to load. For example, if you run java com.acme.Example, it will use CLASSPATH to search for the compiled Example class.

So "C:\Program Files (x86)\Java\jdk1.7.0_80\lib\tools.jar" is almost certainly incorrect. (That JAR file contains the classes that implement various Java tools. It doesn't need to be on the classpath, unless your Java application is running one of those tools. And even then there would need to be other things on the classpath for your application to work.

You need to do some reading on what the Classpath is, how it works, and how to set it correctly. Setting it to stuff randomly is a sure fire way to make your Java code not work ...

  • Related