I have downloaded and installed oraclejdk11 from oracle official site and modified PATH & JAVA_HOME variable in system environment variable on windows
C:\Users\Aviral>javac -version
javac 11.0.1
C:\Users\Aviral>java -version
java version "1.8.0_102"
Java(TM) SE Runtime Environment (build 1.8.0_102-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.102-b14, mixed mode)
i know that jdk11 do not contain jre but how can i run a java program from cmd in java 11
The JDK includes the JRE which you can launch by using the
java
executable in thebin
folder. You use this executable just like any other executable.When you type
java
in the command line it is actually shorthand. It searches yourPATH
until it finds the first matchingjava
executable. If you want to specify a differentjava
executable you can give the absolute path to the executable.You may be wondering, if you've set
JAVA_HOME
andPATH
to point to JDK-11, why doesjava -version
still use Java 8?Take a look at your
PATH
and you'll likely find something likeC:\ProgramData\Oracle\Java\javapath
as one of the first entries (see this). This entry was added automatically when you installed Java 8 and points to the Java 8 executables (java
,javaw
, andjavaws
). Since it's before your%JAVA_HOME%\bin
entry, it takes precedence. However,...\javapath
doesn't containjavac
and that's whyjavac -version
usedJAVA_HOME
(Java 11).In order to execte program from another folder than bin using java 11 you must set the JAVA_HOME path as follows from CMD:
Open CMD as Administrator
Set JAVA_HOME to JDK 11 bin folder
bin folder contains all the traditional JRE tools. In Java 11, both JDK and JRE tools are consolidated so that there is no JRE within JDK 11.
I upgraded to JDK 11 from JDK 8. After adding Java 11 to the path [ point path up to
bin foler ] and JAVA_HOME [ only upto jdk folder (don't include bin ) ]
successfully,
java -version
was still pointing to the previous java version (java 8 in my case.)Then, I ran a command
"setx -m JAVA_HOME "C:\Program Files\Java\jdk-11.0.7"
Now
java -version
orjavac -version
both shows java 11.I hope it helps you too.