Javac isn't working in windows command prompt

43,739

Solution 1

How to run .java files from CMD

  1. go to your Computer -> C: -> Program Files -> Java -> jdk1.7.0_25-bin
  2. copy the path (example: C:\Program Files (x86)\Java\jdk1.7.0_25\bin)
  3. Go to Control Panel -> System and Security -> System-Advance System Settings -> Advanced -> Environment Variables
  4. open the Environment Variables screen and go to System Variables and look for "Path"
  5. after finding the Path system variable, double click it or press edit button and in the Variable value you paste the path from java you just copied after the last values already existing there.
  6. Note!
    • make sure you DO NOT enter any extra space in this field as it won't work;
    • make sure you have one semi-colon before pasting the path, example: Path : ...%ANT_HOME%\bin;C:\Program Files (x86)\Java\jdk1.7.0_25\bin
  7. Note! If you previously tried to compile the .java file in a CMD, close that CMD and open it again as the changes made will take effect only using a new instance of CMD
  8. Go to the location of the file.java , example:E:\Projects , right-click by holding the Shift button pressed inside your folder and in the options from the window that just appeared select: Open command window here
  9. Another solution is to normally open a CMD and change the directory using : cd command until you reach your folder
  10. After the CMD window opened, type: javac HelloWorld.java
  11. Note! Make sure the class name written in your file.java is the same as the file name. Example your file name should be: HelloWorld.java and your class inside that file must also be:

    class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); // Display the string. } }

Otherwise, it will not compile! It is successful if no error message is displayed, if the path of the folder appears again in CMD and of course if the HelloWorld.class file appears now in the directory. To check that type the command dir which will show what contains your currect directory.

  1. To finally run the file type in the CMD: java HelloWorld
  2. Observe that no extension is needed when running the file(the file you run already has the .class extension)

This is how it worked for me! If something is not right, please inform me! Thanks!

Solution 2

For a beginner programmer that doesn't have admin rights on his/her computer, I'd recommend the Eclipse IDE.

Since you already have the JDK the only installation step needed requires no admin rights. From here you must download the "Eclipse Standard" option, and you will get a very large zip archive. You can extract it onto the desktop or my documents. Windows comes with a utility to do this via drag-and-drop right from the explorer or your machine may have another program such as WinRAR installed to do this.

You can then run eclipse.exe from the place where you extracted it by browsing to, and double-clicking this file.

The IDE is very powerful and self-explanatory. You can create projects, run, and debug code, and it's nice for beginners. It's truly worth the long wait in downloading it.

Solution 3

First, yes you did add JDK to PATH. But you didn't do that correctly. You already have these variables added to PATH: C:\Windows\system32;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\

To add JDK put semicolon before you add it because you have multiple path's assigned to PATH variable. So it should be something like this:

C:\Windows\system32;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Java\jdk1.7.0_25\bin

Solution 4

in my case, javac was not working just because while specifying the path to javac in the path variable I gave space after putting semicolon to the end of the previous entry

Share:
43,739
Fuzail Gilani
Author by

Fuzail Gilani

Updated on November 04, 2020

Comments

  • Fuzail Gilani
    Fuzail Gilani over 3 years

    javac not working in windows command prompt

    ^I tried the suggestions on this post, and it was SLIGHTLY helpful, but not completely.

    I opened up my command prompt and I typed in "javac" after putting in the path in my Environment Variables and it didn't work, at which point I googled it and found that thread.

    I knew that I had closed and re-opened my cmd already, and that didn't work, so I skipped that bit and I saw the part telling me to make sure that javac.exe exists, which I verified with the "dir" command in the cmd. Afterwords, while in the "C:\Program Files\Java\jdk1.7.0_25\bin" folder on the command prompt, I typed in the next bit of advice, which was

    for %i in (javac.exe) do @echo %~$PATH:i
    

    After entering this into my command prompt, I got the message "ECHO is on". Upon seeing this, I typed in "javac" again and this time, it worked. So I decided to test this out by backing out of the directory and going to a folder in which I had a .java file saved and running it, but it again told me that

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

    This was disappointing. I think it'll only work if I'm INSIDE the bin file on the command prompt, which is annoying because I'm not an administrator on this computer and it will be annoying to always have to get admin permission (from my parents) to code. They will also never give me the password. Can anyone help me? Thanks in advance! And sorry for the huge wall of text...

    EDIT: Someone has asked what would the output of "echo %path%" be. It is this:

    C:\Windows\system32;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\
    

    ^Is it not supposed to say this?

    EDIT 2: @brano88...I think I did? I right clicked computer, went to properties, went to advanced system settings, clicked Environment Variables and went to the top part and pressed "New..." There, I entered the Variable Name as Path and the Variable Value as the location of the bin folder. Is this incorrect? I followed a YouTube tutorial step by step while doing this.

    This one: http://www.youtube.com/watch?v=Hl-zzrqQoSE