I'm an on and off beginner in Java, studying in my own time using the library book Learn Java in a Weekend by Joseph P. Russell. I have been using Java on Mac OS X to follow the book, where I was able to get things up and running pretty quickly and type in and compile and run some of the example programs.
However, today I decided to try get it up and running with Windows XP, and leaving all advantages and disadvantages of each OS aside, I've run into problems getting programs to compile from outside the current directory, despite having set the system PATH variable correctly (as far as I can see). I may have missed something very obvious, and probably can't see the wood for the trees, so it would be great if someone could help me.
So far, this is what I've done:
1) Installed latest JDK for 32-bit Windows (1.6.0_24).
2) Restarted my machine (I did this after some of the other steps, after reading that it is important after installing JDK. The installer did not request a restart.)
3) Set system PATH environment variable so that it now reads %CommonProgramFiles%\Microsoft Shared\Windows Live;C:\Documents and Settings\Java
Note: I had previously set the second path in the variable to C:\Program Files\Java\jdk1.6.0_24\bin as advised by Oracle/Sun here, step 4: http://www.oracle.com/technetwork/java/javase/documentation/install-windows-152927.html . After reading Christophe Verre's post (2nd one down) here http://www.coderanch.com/t/501657/java/java/Javac-File-Not-Found , stating using the jdk bin for source files is not recommended, I changed it to the directory given above.
Note 2: In another forum elsewhere on the web, someone had recommended changing the CLASSPATH user environment variable to make sure it includes a dot on it's own as one of the paths. As someone in the same thread said it's not recommended for beginners to make changes to CLASSPATH, I left it alone; however, it did have a dot anyway.
4) Typed in 'java -version' and 'javac' at the command prompt. Java in a Weekend says if you get the right output from these commands, i.e. information about your java version and help on the javac command, you have set up your path correctly.
5) Got error message 'javac: file not found: HelloWorld.java' whenever entering 'javac HelloWorld.java' from a directory other than the directory containing HelloWorld.java file. If entering the command actually from within the directory, the file compiles without a hitch.
Since then I've spent over an hour trawling web forums but haven't turned up anyone with the same exact problem.
I apologise if I've included some unnecessary extra information, but I hoped that the thread might be easier to read for anyone who has the same problem.
Short answer: put things back the way you had them at first. C:\Program Files\Java\jdk1.6.0_24\bin must be on your path. This does not prevent you from storing your source code elsewhere; in fact, it enables it.
I've now set the PATH back to C:\Program Files\Java\jdk1.6.0_24\bin as you recommended. However, the problem as described above persists. No matter where the HelloWorld.java file is stored, entering javac HelloWorld.java only finds it if I'm already in that directory containing the file, be it jdk1.6.0_24\bin or C:\Documents and Settings\Java .
How does it enable the storing of source code elsewhere? I thought the PATH variable tells the compiler where to look for source files?
No, the PATH variable tells the computer where to find the executable files, eg java.exe.
You are supposed to stay in the same directory you created the files in. If they have package names, you may need to move towards the root to execute the files, but you probably aren't using package names yet.
If you are in a different directory, how is the compiler expected to find the files? You can find a .java file by giving the compiler its complete relative path, but that won't help because it won't find other files in the same directory.
Thanks, Campbell, and Ernest. It's clear now that I had missed something obvious. Must always make sure I'm in the right directory, or quote the full path in the command.
I've only begun using command prompts a little more intensively than 'hardly at all' in recent months. I think I've been spoiled by growing up in a GUI world, where boundaries between directories can often seem somehow blurred, such as with indexed searching and opening recent documents from applications, the Finder or the Start Menu (for all I know, these features can be used from the command prompt too, I guess). And I misconstrued 'executables' (which it is clear enough about in the book, by the way) to include source files as well, having grown up using the 'Mac' term 'application' instead of 'executable'. Adding to that is some general sloppiness on my part, making errors like this more likely.