• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

How to point PATH envirnoment to /bin directory?

 
Greenhorn
Posts: 7
Netbeans IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Eric Halle
Greenhorn
Posts: 7
Netbeans IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 80244
426
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 80244
426
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to the Ranch , Eric Halle
 
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you get it working Eric? or were you just looking for an explanation?


Al
 
Eric Halle
Greenhorn
Posts: 7
Netbeans IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, I think I got it figured out finally. Thank you very much for the help.
 
Eric Halle
Greenhorn
Posts: 7
Netbeans IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7
Netbeans IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And sorry for the barage of noob questions.
 
Al Fraelich
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7
Netbeans IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Eric Halle
Greenhorn
Posts: 7
Netbeans IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
aha. Well thank you very much everyone. You all have been a great help!
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 80244
426
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
    Marshal
    Posts: 80244
    426
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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.
     
    Greenhorn
    Posts: 3
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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?
     
    Greenhorn
    Posts: 16
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    My guess is, you have more than one copy of java.exe and one of those copies is in your PATH.
     
    Campbell Ritchie
    Marshal
    Posts: 80244
    426
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Welcome to the Ranch Jerome Bean.

    Please don't add new questions to an old thread of somebody else's.
     
    Sheriff
    Posts: 14691
    16
    Eclipse IDE VI Editor Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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.
     
    Jerome Bean
    Greenhorn
    Posts: 3
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks Christophe en Allen for your advise. My apology to Campbell; point taken.
     
    Jerome Bean
    Greenhorn
    Posts: 3
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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.
     
    This will take every ounce of my mental strength! All for a tiny ad:
    Smokeless wood heat with a rocket mass heater
    https://woodheat.net
    reply
      Bookmark Topic Watch Topic
    • New Topic