This week's book giveaway is in the Mac OS forum. We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line! See this thread for details.
I'm reading through Head First Java and in the section "Setting up Java" they are telling me that I "need to add an entry to my PATH environment variable that points to the /bin directory inside the main Java directory".
Can, umm, can somehow please explain to me what they mean? I have almost no clue what I'm doing and any help would be very appreciated.
The path environment variable is used by the console do determine which methods can be called. Usually it's the executes that are in the current directory and which are on the path.
On windows you can edit your environment variables in your control panel under user accounts. Just append ";C:\pathToJava\bin" to it without the "".
I'm sure that someone else can answer your question for a unix/linux environment.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Whenever you try to compile or run a java code you have to use javac/java. Now these javac or java are nothing but some executable files. In windows when you open cmd prompt and write some thing it expects some command/batch file/exe file. To locate those it search in the PATH variable's value which is nothing but combination of some path element (like c:/test;d:/abc/xyx etc). So to make the javac/java visible to the windows or any other environment you need to update the PATH variable. Then the cmd prompt knows from where to fetch the javac/java.
So are they basically saying that, when I open command prompt, I have to type in "cd C:\Program Files\Java\jdk1.6.0_21\bin" so it knows where the .exe files are?
Joined: Jun 26, 2007
That's true then you are in the right location. The command prompt will look into the current directory and able to get it. But if you are in say d:\test location from there you want to run javac/java then the command prompt should have some idea or road map kind of thing which the PATH variable will provide. So we can avoid every time moving to C:\Program Files\Java\jdk1.6.0_21\bin location for compilation. We can compile/run from any location.
Eric Halle wrote:So are they basically saying that, when I open command prompt, I have to type in "cd C:\Program Files\Java\jdk1.6.0_21\bin" so it knows where the .exe files are?
No. Not at all.
THAT is the ONLY reason to set your PATH variable. Why bother setting when you have to take the pain of going to the bin !
You don not want to go to the bin (reason being that your java program is not in bin but somewhere else) so you mention in your path that whenever you try to run javac compiler , it searches in the PATH you have mentioned/set up.
Ok well i got that figured out but now I have another question: where do I put my .java files? I had it in the java folder in another folder I labeled "source codes", but it didn't work when I ran it, so i tried moving the source code folder into my bin directory but that didn't work either. where should i place it?
Alright Eric, I know where you are, and I've been there. there are all kinds of "directions" but very few offering an "understanding". I'm going to try to do that here.
Alright, from here I am only speaking in specifics of using notepad to write the program and the command prompt to compile and run it.
writing a "hello world" program (you can find it anywhere) in notepad and saving it as a .java file anywhere (on your desktop is fine for now).
Open command prompt and change directories to your desktop probably something like "cd Desktop" should do it.
type "javac HelloWorld.java" (or whatever you named the file/program). javac is the compiler, it will compile the .java file and create a .class file. this will only work if path is set correctly. if you recieve an error 'javac' is not recognized as an ... then your path is not set correctly for javac. To set it, go to start -> right click "computer" -> properties. on the left side of the window select advanced system settings (windows 7). Another window opens, select "Environment Variables" button on bottom. New window opens, in the top half, select "path" then press the "edit" button. At the end of the list, add a semicolon " ; " and for me, I added "C:\Program Files\Java\jdk1.6.0_21\bin" This points to the folder where windows can find the "javac" executible. save changes, and try to compile same as before. If it works correctly, it will create on the desktop (same directory as the source file) another file named the same but it will be a .class file. Your path for the compiler is now set.
Now to run the file all you need to type is "java HelloWorld" do NOT add the .class to the end or you will receive errors.
where ever you decide to put the source file, you should be good.
But for some reason, even though I don't have to change the directory to run javac (meaning I must have set the path correctly), I still have to change the directory to wherever I saved the .java file. Is this how it supposed to work or am I still doing something wrong?
Eric Halle wrote:...I don't have to change the directory to run javac (meaning I must have set the path correctly), I still have to change the directory to wherever I saved the .java file...
It sounds like you are set up correctly.
The PATH variable tells your system where to find javac, which allows you to simply type "javac" from any directory and your system will find the executable. But you still need to tell javac where to find the source file you want to compile. One way to do this is to change the directory to where the .java file is located. Then all you need to do is supply the file name. For example...
> javac MyFile.java
Another way is to provide a path (relative or absolute) for your .java file. For example...
I am at exactly the same point as the OP and also working from the same book :P Only difference is i'm on os x. Any tips on how to set the path for my os?
From what i can garner from the above posts i just use a text editor to write the code and compile with the javac via the terminal on os x? I plan on using aquamacs as my text editor. Any mac users out there that can help get me started?!
Peter Caulfield wrote:I am at exactly the same point as the OP and also working from the same book :P Only difference is i'm on os x. Any tips on how to set the path for my os? ...
Welcome to the ranch!
If you're using a Mac, you've got it easy (of course). Java should already be installed and ready to go. See this thread in our Mac forum to get started.
Joined: Oct 13, 2005
Eric Halle wrote:And sorry for the barage of noob questions.
Not at all.
What I suggest is that you
1: open your command prompt (if on Windows® you usually start in "My Documents")
2: write "mkdir Java" (or some other useful name for it)
3: change to the directory by writing "cd Java" (or whatever you called it)
Now you can always find your Java work by repeating the procedure, well stages 1 and 3.
Joined: Oct 13, 2005
Yes, you usually have to navigate to the folder where the Java file is located. If there are files in several folders, you will have to add them to your classpath, but it will be some time before you have to do that.
It is usually best to set the classpath at compile time with the -cp option.
I am new, just installed java and added to PATH C:\program files\java\jdk1.6.0_21\bin. I edited a source MyFirstApp.java and saved it in C:\users\name\documents\. After compiling it with javac it runs fine with the command java MyFIrstApp. So my PATH command seems to work. However I cannot compile the source file with Javac from that directory. I have to go to C:\program files\java\jdk1.6.0_21\bin and type javac C:\users\name\documents\MyFirstApp.java.
I do not understand why java.exe runs fine from C:\users etc. but javac.exe does not. Have you got an explanation or help?