This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark ""Path" Environment Variable" Watch ""Path" Environment Variable" New topic
Author

"Path" Environment Variable

Daniel Bosch
Greenhorn

Joined: Oct 03, 2012
Posts: 5
Alright, I just started trying to learn Java, and I'm using the Java Head First (2nd Edition) book to help me learn. One of the things it tells me to do is "add an entry to your PATH environment variable that points to the /bin directory". I already installed Java 7. The bin directory is located in: C:\Program Files\Java\jre7. I went to Control Panel and got to the Environment Variables window, and from there I proceeded to make a User Name variable called "JAVA_HOME" like this site suggested, with the Value set as "C:\Program Files\Java\jre7". Then, I proceeded to edit the System variable "Path", with "%JAVA_HOME%\bin" as the name (without the quotation marks of course), and I placed this at the beginning of the Value without deleting any of the pre-existing text that was already there. I made sure to include a semicolon after the %JAVA_HOME%\bin. When I ran command prompt and typed in "javac", it gives me this message:

" 'javac' is not recognized as an internal or external command, operable program or batch file."

Apparently, I'm not supposed to get this message... At least, I don't think I should be getting it. I restarted my computer to see if that would change anything, but it didn't. I triple checked the values of the environment variables to see if I made a typo, but I failed to find any. I looked through many of the topics related to this problem, but I can't make heads or tails of any of the information provided... Can someone please help me? I apologize for the stupid question and for my ignorance ... :\

By the way, I'm running Windows 7 64-bit, if that makes any difference at all... Any help is greatly appreciated!
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

You need to put the JDK's bin in your PATH, not the JRE's.

The JDK is the devleopment kit, meant for developing programs, and it includes the compiler, javac.

The JRE is just the runtime, for running programs. (The JDK is a superset of the JRE, or close to it, so if you have the JDK's bin in your path, you're covered for what's in the JRE as well.)

Also, make sure you open a new cmd prompt AFTER updating your PATH env var.

(And if you only downlaoded the JRE, and not the JDK, you'll have to go back and get that, of course.)
Daniel Bosch
Greenhorn

Joined: Oct 03, 2012
Posts: 5
Thank you for replying! I changed the JAVA_HOME variable to "C:\Program Files\Java\jdk1.7.0_07" and kept the "Path" variable the same because I didn't know if I should change that or not, so the "Path" variable still has "%JAVA_HOME%\bin" at the beginning of it. I exited out of the existing command prompt and tried it again, typing in javac in the new command prompt window, and it still says that 'javac' is not recognized... Am I still doing something wrong? jdk1.7.0_07 is the JDK, correct...? Sorry; I'm probably messing up something really simple... =_=
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38462
    
  23
Welcome to the Ranch

We have an FAQ which covers that problem: here. You can tell whether you have got the PATH set up correctly by issuing these two commands:-Remember you usually need to reopen the command line window before the new PATH takes effect.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38462
    
  23
Daniel Bosch wrote: . . . I changed the JAVA_HOME variable to "C:\Program Files\Java\jdk1.7.0_07" and . . . the "Path" variable still has "%JAVA_HOME%\bin" at the beginning of it. . . . it still says that 'javac' is not recognized . . .
That combination looks all right. Check the following with Windows Explorer or similar:
  • 1: Is there such a folder as C:\Program Files\Java\jdk1.7.0_07?
  • 2: Does the folder have a bin subfolder inside it?
  • 3: Does that bin folder have a javac program inside it? It is probably called “javac.exe”.
  • Also confirm you haven’t set a user “JAVA_HOME” and a system “PATH”; that doesn’t work.
    Daniel Bosch
    Greenhorn

    Joined: Oct 03, 2012
    Posts: 5
    Thanks for the welcome, Campbell Ritchie! I also read the entire FAQ that you provided a link to prior to posting this thread, but I did what it said and either I don't understand what it's trying to tell me or I missed something that I was supposed to do. In response to your help...

    1.) Yes, there is a jdk1.7.0_07 folder located in the Java folder. I made sure that I was in Program Files and not some other folder as well.
    2.) Inside that folder, there is a bin folder with a lot of stuff that seems really complicated inside it...
    3.) Yes, there is an executable javac file in there.

    I don't really understand your last piece of advice though; I have a user environment variable "JAVA_HOME"... Am I supposed to put "JAVA_HOME" as a system environment variable, and "Path" as a user environment variable...?

    EDIT: And I ran command prompt, and for some reason, "java -version" worked and it told me the version of Java I had... But "javac -version" did not.
    Jeff Verdegan
    Bartender

    Joined: Jan 03, 2004
    Posts: 6109
        
        6

    Daniel Bosch wrote:
    EDIT: And I ran command prompt, and for some reason, "java -version" worked and it told me the version of Java I had... But "javac -version" did not.


    Execute


    Make sure the JDK's bin directory is there. Copy/paste that directory into, for example a dir command so you can see what's there.

    Daniel Bosch
    Greenhorn

    Joined: Oct 03, 2012
    Posts: 5
    I executed "echo %PATH%" in the command prompt, and a whole bunch of stuff was returned. But I did see "%JAVA_HOME%\bin" shown at the beginning, and the %JAVA_HOME% environment variable leads to the JDK's folder... I made sure of that. What was supposed to happen (or, more fittingly... What was I supposed to notice) when I executed the "echo %PATH%" command in the command prompt? (Or was I not supposed to execute that in the command prompt...?) So I'm not sure what I should be looking for. Should I be setting the %JAVA_HOME% variable as "C:\Program Files\Java\jdk1.7.0_07\bin" instead of "C:\Program Files\Java\jdk1.7.0_07"...? The only reason why I didn't do this was because I thought that since the "Path" environment variable has the %JAVA_HOME% variable leading to the jdk1.7.0_07 folder, and then from there looking into the bin folder, I didn't think I needed to include "\bin" to the end of the %JAVA_HOME% environment variable.

    Ugh... I feel like I'm missing something really simple here, and you people are trying to explain it to me but I'm just not seeing it...
    Jeff Verdegan
    Bartender

    Joined: Jan 03, 2004
    Posts: 6109
        
        6

    Daniel Bosch wrote:I executed "echo %PATH%" in the command prompt, and a whole bunch of stuff was returned. But I did see "%JAVA_HOME%\bin" shown at the beginning


    Did it actually show "%JAVA_HOME%"? If so, that's a problem. It needs to have expanded that by this point. So either you misspelled JAVA_HOME when you set that env var, or you may have set JAVA_HOME as a personal variable and PATH as a system one, or something like that. I did something like that once, and got basically the same result. I always make PATH and JAVA_HOME system variables, because it's my computer and I don't know how Windows handles the two types of varaibles and I don't want to know.

    Should I be setting the %JAVA_HOME% variable as "C:\Program Files\Java\jdk1.7.0_07\bin" instead of "C:\Program Files\Java\jdk1.7.0_07"...?


    JAVA_HOME should be set to NOT include the bin directory, and PATH should be set to include "%JAVA_HOME%\bin", and when you echo the PATH, it sould expand JAVA_HOME to "C:\Program Files\Java\jdk1.7.0_07" and then append the "\bin" so you get "C:\Program Files\Java\jdk1.7.0_07\bin" in the output from your echo command.

    The only reason why I didn't do this was because I thought that since the "Path" environment variable has the %JAVA_HOME% variable leading to the jdk1.7.0_07 folder, and then from there looking into the bin folder, I didn't think I needed to include "\bin" to the end of the %JAVA_HOME% environment variable.


    You did that part correctly. It's just something in where you set JAVA_HOME and PATH or how you spelled it that's causing it not to get expanded in the path like it should be.

    Ugh... I feel like I'm missing something really simple here, and you people are trying to explain it to me but I'm just not seeing it...


    Don't feel bad. It's fairly simple in theory, but there are subtleties and plenty of ways to easily trip yourself up. And it's the kind of thing that can be hard to explain when you're not sitting right there at the computer with the person, so PATH and CLASSPATH questions tend to take a few more iterations than some other ones.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38462
        
      23
    Lots of other people have had that problem with user JAVA_HOME and system PATH. That is why there is a mention of that problem in the FAQ I showed you yesterday. If you follow the link from the FAQ, you find somebody else did that and it took us all ages to work out what the problem was; it was a particularly difficult problem to solve.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38462
        
      23
    Presumably the OS looks through the system environment so as to create a system PATH, and through the user environment to create a user PATH. So follow Jeff’s suggestions.

    I don’t know what happens if you make both those environment variables user variables. If the OS finds a JRE installation with a java.exe file for Java6 first, and there is only your new javac.exe from Java7, you will have no end of problems about trying to execute bytecode which is newer than the java.exe file.
    fred rosenberger
    lowercase baba
    Bartender

    Joined: Oct 02, 2003
    Posts: 11250
        
      16

    Daniel Bosch wrote:I changed the JAVA_HOME variable to "C:\Program Files\Java\jdk1.7.0_07" and kept the "Path" variable the same because I didn't know if I should change that or not, so the "Path" variable still has "%JAVA_HOME%\bin" at the beginning of it.

    I would suggest some caution when changing your environment variables. If you just change them willy-nilly without understanding what you are doing, you can make like VERY difficult for yourself.


    There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
    Jayesh A Lalwani
    Bartender

    Joined: Jan 17, 2008
    Posts: 2339
        
      28

    One thing that has used to trip me up (not sure if you have the same problem). After you change an environment variable, you need to open a new Command prompt for the environment variables to take effect. If you open a Command prompt, then change environment variables, then issue a command on the Command Prompt, the COmmand prompt will use the old environment variables, not the new environment variables
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38462
        
      23
    I think it is a different problem because Daniel Bosch said here he had closed and reopened the command prompt.
    Daniel Bosch
    Greenhorn

    Joined: Oct 03, 2012
    Posts: 5
    Thank you Jeff Verdegan! I knew it was something simple like that... After setting JAVA_HOME as a System variable, everything worked fine in the command prompt. And Campbell Ritchie, I did read through that topic... But for some reason, I failed to understand what it was I was supposed to do... :\ Fred Rosenberger, I will keep that in mind. Hopefully I won't have to do much with paths later on because I feel like I'm bound to mess something up. Thank you all for helping me out!

    (And another question that has nothing to do with Java... I see there's a "Resolved" button at the top of this page. Since my problem is solved, am I supposed to click that button? Sorry; I'm new to this whole internet forum thing)
    Jeff Verdegan
    Bartender

    Joined: Jan 03, 2004
    Posts: 6109
        
        6

    Daniel Bosch wrote:Thank you Jeff Verdegan!


    You're quite welcome. Glad I could be of help!

    (And another question that has nothing to do with Java... I see there's a "Resolved" button at the top of this page. Since my problem is solved, am I supposed to click that button? Sorry; I'm new to this whole internet forum thing)


    Yep, that's the idea.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38462
        
      23
    We got there in the end Glad to be able to help.
     
    jQuery in Action, 2nd edition
     
    subject: "Path" Environment Variable