wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes javac command won't work, java command will Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "javac command won Watch "javac command won New topic
Author

javac command won't work, java command will

Gary Farms
Greenhorn

Joined: Jul 31, 2002
Posts: 12
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
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
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.
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1502
    
    5

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.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38045
    
  22
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.
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
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.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38045
    
  22
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.
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
Edit: Ignore. I'm confused by Campbell's argument, so it's not worth continuing.
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1502
    
    5

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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38045
    
  22
Thank you. I often find that somebody else can understand the point and explain it more clearly.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: javac command won't work, java command will
 
Similar Threads
Program: Change Due/Tendered
javacommon.zip and partitioned hard drive.
Trouble when I execute my archive .class
classpath problem
Cannot run .jar file