JDK java executable vs JRE executable - java

I installed the Oracle JDK to /usr/jdk/jdk1.8.0_25 and set that directory to JAVA_HOME.
JAVA_HOME/bin contains the java executable and JAVA_HOME/jre/bin contains another java executable.
The guide I followed said to add JAVA_HOME/bin and JAVA_HOME/jre/bin to the PATH but which is the correct executable to use and which will be used if I call java from the command line?

Here is a simplified overview of the differences between JDK and JRE
JRE means JavaRuntimeEnvironment : it contains only binaries for running java program
JDK means JavaDevelopmentKit : it contains binaries for running java program + binaries to compile java source code (and produce a java program).
A JDK always contains a JRE inside (under directory <JDK_HOME>/jre)
The major difference between JRE and JDK is the javac program. (javac means java compiler)
(you will also find some other programs under the <JDK_HOME>/bin that aren't present under <JDK_HOME>/jre/bin : all of them may be useful to do java development - but in most case - useless to run a java program.
All programs that are in both locations (i.e. <JDK_HOME>/bin and <JDK_HOME>/jre/bin) are the same, so it is not very important to make a distinction.
To answer precisely to your question : the java instance that will run when you execute a command line starting with java is the first instance of java found in your PATH system variable... just like any other program.

Whatever is earlier in the path for java - JAVA_HOME/bin or JAVA_HOME/jre/bin.
If you take a look at the folder structure where jdk / jre is installed you would see that in the jdk/bin folder there are quite a collection of binaries such as java, javac, javap etc. The JRE would include java but not javac, javap etc. I am assuming this is linux and you downloaded the compressed archive and extracted that.

If you are really curious, you can write this to the command line:
where java
This will give the exact location of the executable. On my PC this gives me
C:\ProgramData\Oracle\Java\javapath\java.exe
which is a soft link for the system default runtime java.exe (in my case it is C:\Program files\Java\jre 1.8.0_25\bin\java.exe)

Related

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

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?
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
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 ...

Compile a java file using javac [duplicate]

This question already has answers here:
javac is not recognized as an internal or external command, operable program or batch file [closed]
(6 answers)
Closed 7 years ago.
Ok now it feels like I've tried everything, can someone please tell me how I can compile a Java file ? My file/code looks like this:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
And I have saved it as HelloWorld.java and in All Files. But the problem is that everytime I try to compile the Java file it says:
'javac' is not recognized as an internal or external command,
operable program or batch file.
I have downloaded JDK and JRE, set/changed the path to C:\Program Files\Java\jre1.8.0_45\bin; but when I look through the commands I can't see the javac command anywhere!
I thought for a while that I may have downloaded the wrong JDK, but when I see what's supposed to include in JDK it says that javac is supposed to be there too - but I can't find it!
Any suggestion how to fix this ?
I use Windows 8.1 (I don't know if that helps but I write it just in case). I really need some help I've been trying to fix this for about 6 months now and it's getting really annoying not know what to do.
Having the path C:\Program Files\Java\jre1.8.0_45\bin won't help you with the Java compiler javac, which is in the Java Development Kit (JDK) rather than the Java Runtime Environment (JRE).
As a first step, make sure your path includes a bin directory from some directory with "jdk" in the name.
I see that you've downloaded jdk-8u51-windows-x64.exe, which is (what I presume to be) the JDK installer. If it is the JDK installer from a reputable source, run it and and it should produce an directory containing the JDK in a predictable place (such as adjacent to the JRE, or in the root C: directory as seanhodges helpfully commented below).
You need to add your java's installation bin folder to your windows path.
Additionally, the path you set was the the JRE not the JDK. You need the JDK to compile code.
https://www.java.com/en/download/help/path.xml
javac can be found in the JDK directory - C:\Program Files\Java\jre1.8.0_45\bin is a JRE directory. JDK directories start with jdk.
Before all, make sure you understand the differences between a JRE and a JDK:
The JRE (Java Runtime Environment) is used to execute java programs
The JDK (Java Development Kit) is used to compile java source code (and embed a JRE)
If you look into the bin folder inside your JDK installation path, you may find, among other commands, javac.
So all you have to do is to configure some environment variables:
JAVA_HOME to your JDK installation path, most likely C:\Program Files\Java\jdk1.8.0_51.
PATH, you should append :$JAVA_HOME$\bin to the existing path.
Then, you may be able to execute javac HelloWorld.java in a terminal without problem, since javac is available in the %PATH%

Can't install Java JDK

Ok, this is embarrassing, but I can't install Java. I have no experience in Java so any help appreciated.
So as ussual I install Java JDK (The JRE I think is already there before) I have jre7 and jdk1.8.0.0_05
I have install it, but Java still not in my computer
Here's some desperate cmd check
>C:\>WHERE java
>INFO: Could not find files for the giving pattern
>C:\>java
>'java is not recognized as an internal or external command, operable program or batch file.
Where my java folder is C:\Program Files (x86)\Java\jdk1.8.0.0_5
On my desperation I created the variable JAVA_HOME on the given directory. Afterward, I tried this.
>C:\>echo %JAVA_HOME%
%JAVA_HOME%
I was expecting the cmd to echo the directory.
The variable PATHEXT has the following in it:
.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
Clariffication:
I tried to reinstall the Java Jdk Installer with Windows Installer, but no luck.
If this matters, I'm running from Windows 7.
I tried to learn from Here
After closer inspection, I can run java and javac after going all the way into the directory (so inputing java outside the bin will do nothing good)
Add C:\Program Files (x86)\Java\jdk1.8.0.0_5\bin to your PATH variable
Here is how mine is
I think you may have not added JAVA_HOME environment variable, and/or to PATH environment variable neither.
Give a read to this article
http://www3.ntu.edu.sg/home/ehchua/programming/howto/jdk_howto.html
Regarding you last comment, i think you should put the java directory right infront of all the other directory because, if you were to install another version of java it might not be used if you place it behind.
(For Advanced Users Only)
I suggested that you place the JDK bin directory in front of "c:\windows\system32" and "c:\windows". This is because some Windows systems may have an out-dated copy of JDK/JRE in these directories. Do a search for "java.exe", and you will be amazed by the findings.
You could read "Java Applications and Environment Variable" for more discussions about PATH environment variable.
I also recommend that you define an environment variable called JAVA_HOME, which contains the JDK installed directory, and include the JDK bin directory in the PATH via JAVA_HOME, i.e., PATH=%JAVA_HOME%\bin;.....

Java not finding the proper path

I am having a problem with running java from cmd.exe.
When i type in 'java' this errors shows:
Error: could not open `D:\Program Files (x86)\Java\jre6\lib\i386\jvm.cfg'
The problem is, my java is not in jre6, but in jre7. The jre6 folder doesn't even exist anymore.
I've checked the path and all other environmental variables and java was not mentioned anywhere. So I updated path to point to D:\Program Files (x86)\Java\jre7\bin but that didn't help.
I am running Windows 7 64-bit so I also tried running java from 64-bit cmd.exe but it didn't help.
Is there anywhere else except path to look and change the way Windows looks for java?
The problem is that older JDKs copy java.exe among some other Java executables into C:\Windows\System32 which happens to be on the PATH before the new JDK/JRE location.
The solution is to delete java.exe and other Java related executables from System32 as they aren't required any more. This could however potentially break applications that depend on the older JDK/JRE and hence they may need to be re-configured.

compilation of java program

I have JRE 6 in my PC (Windows XP) but when I compile a program in the command prompt it shows javac is not recognised as internal or external command. Which software is needed to compile a Java program?
You need to download and install a JDK, not a JRE.
As a (very rough) explanation, the JRE contains just the Java Virtual Machine, whereas the JDK contains not only the JRE but also the compiler (javac), some debugging tools (javap, jvisualvm, ...), extra libraries and the API documentation.
See also:
What is the difference between JRE and JDK?
as recommended by dogbane, you can follow the Java Hello World for Windows Tutorial.
I suggest you run through the steps of the Hello World! Tutorial.
As per the instructions:
Download JDK6. (Make sure you download the JDK, not the JRE.)
Update the PATH variable to be able to conveniently run the JDK executables such as javac from any directory without having to type the full path of the command
JRE stands for Java Runtime Environment. It allows you to run already compiled java programs. To compile your own programs, you need JDK which stands for Java Development Kit. You can download it at JDK Downloads Page.
You need to install JDK-6 (Java Development Kit) which contains javac Java compiler. JRE does not contain it.
Instead of a simple JRE, you'll need a full JDK. As an example, youc an download sun one here : http://www.oracle.com/technetwork/java/javase/downloads/index.html#need (and don't forget to select "download JDK").
JDK is needed to develop applications whereas JRE is enough to run applications.
In other words JRE is enough to run a application (i.e) it includes java.exe and doesnot contain javac.exe
JDK contains both java.exe and javac.exe
There are also other chances that you dint set your class path properly.

Categories