File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes How to point PATH envirnoment to /bin directory? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to point PATH envirnoment to /bin directory?" Watch "How to point PATH envirnoment to /bin directory?" New topic
Author

How to point PATH envirnoment to /bin directory?

Eric Halle
Greenhorn

Joined: Sep 27, 2010
Posts: 7

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.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

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.
Soumyajit Hazra
Ranch Hand

Joined: Jun 26, 2007
Posts: 136
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.


Java Programmer | SCJP 1.5 | SCWCD 1.4
Eric Halle
Greenhorn

Joined: Sep 27, 2010
Posts: 7

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?
Soumyajit Hazra
Ranch Hand

Joined: Jun 26, 2007
Posts: 136
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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39826
    
  28
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.

You would have to type C:\Program Files\Java\jdk1.6.0_21\bin\javac MyClass.java
Don't change to the bin directory.

You will find instructions for setting the PATH here (Windows 32-bit version).
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39826
    
  28
And welcome to the Ranch , Eric Halle
Kr Manish
Ranch Hand

Joined: Jul 30, 2010
Posts: 138
Campbell Ritchie wrote:
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.


You know what I am saying ?
Al Fraelich
Greenhorn

Joined: May 25, 2009
Posts: 20
Did you get it working Eric? or were you just looking for an explanation?


Al
Eric Halle
Greenhorn

Joined: Sep 27, 2010
Posts: 7

Ok, I think I got it figured out finally. Thank you very much for the help.
Eric Halle
Greenhorn

Joined: Sep 27, 2010
Posts: 7

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?
Eric Halle
Greenhorn

Joined: Sep 27, 2010
Posts: 7

And sorry for the barage of noob questions.
Al Fraelich
Greenhorn

Joined: May 25, 2009
Posts: 20
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.

Hope this helps
Al
Eric Halle
Greenhorn

Joined: Sep 27, 2010
Posts: 7

I FINALLY GOT IT TO WORK!

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?
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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...

> javac C:\MyFiles\Programming\Java\Tests\MyFile.java

So you don't have to change the directory to where the .java file is saved, but that's is a common way to do it.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Eric Halle
Greenhorn

Joined: Sep 27, 2010
Posts: 7

aha. Well thank you very much everyone. You all have been a great help!
Peter Caulfield
Greenhorn

Joined: Sep 27, 2010
Posts: 10
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?!

marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Oct 13, 2005
Posts: 39826
    
  28
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.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 39826
        
      28
    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.
    Jerome Bean
    Greenhorn

    Joined: Oct 05, 2010
    Posts: 3
    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?
    Allan A Peak
    Greenhorn

    Joined: Sep 20, 2010
    Posts: 16
    My guess is, you have more than one copy of java.exe and one of those copies is in your PATH.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 39826
        
      28
    Welcome to the Ranch Jerome Bean.

    Please don't add new questions to an old thread of somebody else's.
    Christophe Verré
    Sheriff

    Joined: Nov 24, 2005
    Posts: 14688
        
      16

    You may be interested to know that there's a new FAQ about creating java programs and setting environment variables : http://faq.javaranch.com/java/how-to-create-java-program.


    [My Blog]
    All roads lead to JavaRanch
    Jerome Bean
    Greenhorn

    Joined: Oct 05, 2010
    Posts: 3
    Thanks Christophe en Allen for your advise. My apology to Campbell; point taken.
    Jerome Bean
    Greenhorn

    Joined: Oct 05, 2010
    Posts: 3
    Allen you were right. There was more java.exe's in other directories. I type a space in PATH before C:\. Without that space things work smoothly. Thanks.
     
    Consider Paul's rocket mass heater.
     
    subject: How to point PATH envirnoment to /bin directory?