Forums Register Login

javac command won't work, java command will

+Pie Number of slices to send: Send
I have Helloapp.java and HelloApp.class in c:\My_Java

when I do:
cd\My_Java
javac Jelloapp.java - I get error msg "javac is not recognized as a internal or external command"

But, when I do:
cd\My_Java
java HelloApp - it runs fine!

So the system recognizes the java command, but not the javac command to compile. Why?

I've added c:\Program Files\Java\jdk1.7.0_09\bin to end of my PATH statement
+Pie Number of slices to send: Send
 

Gary Farms wrote:I've added c:\Program Files\Java\jdk1.7.0_09\bin to end of my PATH statement


In that case it should work.
Try typing
echo %PATH%
in a command prompt and check that it has actually been added.
If it is, then check that javac.exe exists in that directory.
+Pie Number of slices to send: Send
Also, whenever you make any changes to env variables, please make sure to close and start command prompt again.

Secondly, please post output of 'java -version' command (ideally, it should be 1.7.0_09).

Further, make sure that you've put double quotes (") while adding Java path to PATH env variable.
+Pie Number of slices to send: Send
 

Gary Farms wrote: . . .
I've added c:\Program Files\Java\jdk1.7.0_09\bin to end of my PATH statement

That is not actually correct. You should add that to the beginning of your PATH. You may need quotes around that path because of the space. You usually need to close the command line and reopen it before the new path takes effect. Have a look at our FAQ, which uses a slightly different technique for setting the path.
1
+Pie Number of slices to send: Send
 

Campbell Ritchie wrote:

Gary Farms wrote: . . .
I've added c:\Program Files\Java\jdk1.7.0_09\bin to end of my PATH statement

That is not actually correct. You should add that to the beginning of your PATH.


Saying it's not correct is a little strong. If you want to be absolutely certain that the javac.exe that you run is the one in that directory then putting it at the start is a good idea. But if you only have one JDK installed and no other programs called javac, then you can put it anywhere in the PATH that you want.
In the OP's case there is no other javac.exe on his system (at least not in a place referenced by the PATH), so putting the bin directory at the start, the end or somewhere in the middle of the PATH will not make any difference.

+Pie Number of slices to send: Send
It will make a difference.
What if you install a JDK7 at the end of the PATH? Then the only javac found will be for Java7. What if there is already a java executable already installed, as there is in most operating systems? If that is JDK6, as is likely, you will be presenting JDK7 .class files to a Java6 runtime, and they will suffer an exception and fail to run.
That is why you need to add the new Java installation at the beginning of the path.
+Pie Number of slices to send: Send
Edit: Ignore. I'm confused by Campbell's argument, so it's not worth continuing.
+Pie Number of slices to send: Send
 

Stuart A. Burkett wrote:Edit: Ignore. I'm confused by Campbell's argument, so it's not worth continuing.


I think this is what Cambell is saying:
If your OS comes with pre-installed Java (i.e. JRE) 6 - this means that you have java.exe in your PATH - and that java.exe is of version 6.

Now, you add JDK 7 dir's path at end of PATH variable - and you compile your program.

At this point, you have only 1 javac.exe (bundled with JDK 7) and hence, your .class file is compliant to Java 7.

However, when you try to run that class file, by default, you would refer java.exe from JRE 6 - and that might cause the issue (typically an UnsupportedClassVersionError).

And this issue can be avoided if you put JDK 7 dir's path at the beginning of PATH variable (in that case - both javac.exe and java.exe would be picked from JDK 7).

I hope this helps.
+Pie Number of slices to send: Send
Thank you. I often find that somebody else can understand the point and explain it more clearly.
"Don't believe every tiny ad you see on the internet. But this one is rock solid." - George Washington
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1306 times.
Similar Threads
Cannot run .jar file
Program: Change Due/Tendered
classpath problem
javacommon.zip and partitioned hard drive.
Trouble when I execute my archive .class
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 01:30:15.