• Post Reply Bookmark Topic Watch Topic
  • New Topic

running java from command line  RSS feed

 
Abigail Decan
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have windows 7.
cmd.exe told me it couldn't recognize javac.
i have a JDK installed though...

what should i do??
 
Sohail Razvi
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abigail, Have you set the System Variables?
If No, this is how you do it.
Right Click on My Computer, go to "Properties"
Select "Advanced System Settings"
Under "Advanced" tab click on "Environment Variables"
Under "System Variables" Section search for "Path"
Click on "Edit"
At the end put a semi-colon " ; " and type this :
C:\Program Files (x86)\Java\jdk1.7.0\bin
Click "Ok" and "Ok" and "Ok" which will bring you back to System Properties.
Close the window and try javac again in your cmd

If am not clear you can check this video:
http://www.youtube.com/watch?v=RkycwpimOEc

Thanks
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds like a correct diagnosis for that problem, but I am afraid there are two errors in that:
  • 1: The path on AD’s PC will probably be different, probably including 1.7.0_51 or similar.
  • 2: The new PATH should go at the beginning of the PATH string, not the end.
  •  
    Campbell Ritchie
    Marshal
    Posts: 56536
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Razvi Code wrote: . . . If am not clear you can check this video: . . .
    That video is truly dreadful, with the music making it very difficult to follow.
     
    Randall Twede
    Ranch Hand
    Posts: 4696
    8
    Java Scala
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The new PATH should go at the beginning of the PATH string, not the end.

    first i have heard of that.
    it worked for me when i added it to the end of the path.
     
    Winston Gutkowski
    Bartender
    Posts: 10575
    66
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Randall Twede wrote:first i have heard of that.
    it worked for me when i added it to the end of the path.

    Very likely because you haven't had to look for something that is also in one of the other directories in the path.

    The fact is that you should generally replace equivalent directories. Putting it at the front is simply the "lazy man's" (sorry Campbell ) way of making sure that your new one is always checked first.

    Winston
     
    fred rosenberger
    lowercase baba
    Bartender
    Posts: 12563
    49
    Chrome Java Linux
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    One should really UNDERSTAND what the PATH is used for, rather than just making random edits because someone said "You should do this".

    The PATH is a list of directories. It is used by the OS to find things. So, on the command line, when you type "javac", Windows (or whatever OS you are on) has to find the executable named "javac.exe".

    I think it looks in the current directory by default. If it can't find it there, it goes to the PATH, and reads the first directory listed there. It checks to see if javac.exe is there. If it finds it, that is what runs. If it is not found, it checks the next directory in the PATH list, and then the next, and so on. As soon as it finds a match, it quits. If it gets through all the listed directories and never finds it, it says "cannot find executable".

    So...the order of your PATH does matter, since the first match is the one that will run. Having extra directories in there can slow down a program starting up, since it takes the OS longer to find the actual program, but that may not matter too much.

     
    Campbell Ritchie
    Marshal
    Posts: 56536
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    fred rosenberger wrote: . . . I think it looks in the current directory by default. . . .
    I don't know whether that happens in Windows®, but it doesn't on my Linux boxes. If I want to execute a program in the current directory I have to pass ./foo to the terminal.
     
    Jesper de Jong
    Java Cowboy
    Sheriff
    Posts: 16059
    88
    Android IntelliJ IDE Java Scala Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes, the Windows command prompt looks in the current directory first; this is different from how it works on Linux (or even in Windows PowerShell).
     
    Campbell Ritchie
    Marshal
    Posts: 56536
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you, Jesper.
    And I shall have to defend myself to Winston I usually add a new path at the beginning of the path string, then change the version number when I upgrade to a new version of Java®.
     
    Winston Gutkowski
    Bartender
    Posts: 10575
    66
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:And I shall have to defend myself to Winston...

    No need. I often do it myself.

    Winston
     
    Jelle Klap
    Bartender
    Posts: 1952
    7
    Eclipse IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I find it more convenient to not refer to the JDK root directly from the PATH. I always set JAVA_#_HOME (where # is a number) for every major JDK version I have on my system and have JAVA_HOME refer to the version I want to use by default. Then I refer to %JAVA_HOME%\bin on the PATH.
     
    Winston Gutkowski
    Bartender
    Posts: 10575
    66
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Jelle Klap wrote:I always set JAVA_#_HOME (where # is a number) for every major JDK version I have on my system and have JAVA_HOME refer to the version I want to use by default. Then I refer to %JAVA_HOME%\bin on the PATH.

    Ooo. Layers of indirection in PATH. Fancy.

    Winston
     
    Jesper de Jong
    Java Cowboy
    Sheriff
    Posts: 16059
    88
    Android IntelliJ IDE Java Scala Spring
    • Likes 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Did you know that Windows has symbolic links, just like most Unix-like operating systems have? You can create links with the MKLINK command.

    I install different JDK versions in C:\Java, for example: C:\Java\jdk1.6.0_45, C:\Java\jdk1.7.0_51

    Then I make links: C:\Java\jdk6 => C:\Java\jdk1.6.0_45, C:\Java\jdk7 => C:\Java\jdk1.7.0_51

    C:\Java> mklink /D jdk6 jdk1.6.0_45
    C:\Java> mklink /D jdk7 jdk1.7.0_51

    (on my PC I need to be administrator to be able to do that).

    I set JAVA_HOME to C:\Java\jdk7, and in my PATH I add %JAVA_HOME%\bin

    If there's a new update for JDK 7, for example, I install it in a new directory, for example C:\Java\jdk1.7.0_60 and then I change the link C:\Java\jdk7 to point to the new version. I don't need to update JAVA_HOME or PATH, because they are already going through the link.
     
    Jelle Klap
    Bartender
    Posts: 1952
    7
    Eclipse IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ooooh, I like it!
     
    Robert D. Smith
    Ranch Hand
    Posts: 221
    5
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Jesper de Jong wrote:[...]
    I set JAVA_HOME to C:\Java\jdk7, and in my PATH I add %JAVA_HOME%\bin

    If there's a new update for JDK 7, for example, I install it in a new directory, for example C:\Java\jdk1.7.0_60 and then I change the link C:\Java\jdk7 to point to the new version. I don't need to update JAVA_HOME or PATH, because they are already going through the link.

    I was going to mention that this tidbit might be a great addition to the FAQ on setting the Java path, but all I could find was the page on setting the Class Path. The only thing I was able to turn up on the ranch regarding setting the path was a (very) brief mention over at the Cattle Drive.
     
    Campbell Ritchie
    Marshal
    Posts: 56536
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    That is because setting the PATH is covered in this FAQ.
     
    Robert D. Smith
    Ranch Hand
    Posts: 221
    5
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:That is because setting the PATH is covered in this FAQ.

    I knew I saw it somewhere, but couldn't find it. Didn't think to look there.

    [edited for a tyypo]
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!