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%
Related
It's definitely installed, I can run Minecraft. But I'm trying to run a .jar file and Windows 11 is asking "How do you want to open the file?" And java compiler doesn't show up. btw I've treid where java, it said java didn't exist on the computer
UPDATE: I solved the problem but not the issue, there were some mods that were incompatible with eachother
You could try to find it out via printing your PATH.
On Windows: Open the command line via
Start -> CMD
Paste in
echo %PATH%
There you should be able to find the java path. Otherwise try to search for JRE.
By the way it is not always 100% sure that you have the java runtime and java compiler installed.
JRE = Java Runtime Environment for executing .jar
JDK = Java Development Kit for compiling java files and creating .jar files
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 ...
Mac OSX 10.11.14 (El Capatin) does not ship with Java, either the complete development kit (JDK) or the run-time environment (JRE). My need was to just run a .jar file and not for complete Java development. So I downloaded the JRE from here, which is a much smaller file compared to JDK (quite obviously).
This SO answer says that JRE is enough to run jar files.
After installation of JRE I couldn't locate the directory where installation happened or run java command to execute files on my mac. How do I run jar files now?
To run java application you can either use
/System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java
or (as you found)
/Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java
In any case, if you need to run java often, you should set JAVA_HOME in either your ~/.bash_profile or ~/.profile and amend the PATH to include the bin directory.
After JRE installation, the correct path is:
/Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin
But now, how do I run the java command? It seems like the java file located in the directory above is not an executable.
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)
I have a problem involving setting up Java.
I have installed the JRE, added its path to PATH, and set JAVA_HOME and CLASSPATH. Now, java and javacpl work fine, but running javac generates a command-not-found error. Furthermore, javac.exe does not even seem to exist in the JRE's bin folder.
How do I run javac?
The JRE is merely the Java Runtime Environment, which includes only the infrastructure needed to run Java programs that are already compiled.
To compile Java source code using javac, you need the Java Development Kit (JDK).
On Oracle's Java download page, choose the package labelled "JDK".
In order to use javac in cmd , JDK must be installed in your system...
javac will not work if you are pointing "bin" folder inside JRE (C:\Program Files\Java**jre7**\bin)
Please check for javac.exe inside your bin folder(C:\Program Files\Java**jdk1.7.0_45**\bin)
javac.exe must be inside JDK(C:\Program Files\Java\jdk1.7.0_45\bin) not inside JRE (C:\Program Files (x86)\Java\jre7\bin) "JRE doesn't come with a compiler. It is simply a java runtime environment. What you need is the developmental kit." in order to use compiler javac
For javac path(Points to remember while setting system env variable)
path = C:\Program Files (x86)\Java\jre7\bin this is wrong
path = C:\Program Files\Java\jdk1.7.0_45\bin this is correct
Make sure "javac.exe" must be inside your "C:\Program Files\Java\jdk1.7.0_45\bin"
Dont confuse with JRE and JDK both are totally different
if you dont have JDK pls download from this link http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html
reference thread for JDK VS JRE What is the difference between JDK and JRE?
Procedure:-
Install JDK
open cmd prompt
type "cd C:\Program Files\Java\jdk1.7.0_45\bin " press enter(path may change based on jdk version and 32bit and 64bit os version)
type "javac" press enter
its done
Now go and change your system environment variable
path = C:\Program Files\Java\jdk1.7.0_45\bin
this will set the path permanently
The JRE doesn't have javac - you need to download the JDK (Java Development Kit).
First, you need to install the jdk, then add the path of bin folder of jdk in the path vaiable.
you can refer to this link