Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javac isn't working in windows command prompt

Tags:

java

cmd

javac

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

like image 253
Fuzail Gilani Avatar asked Jul 15 '13 23:07

Fuzail Gilani


1 Answers

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!

like image 84
crina Avatar answered Nov 10 '22 04:11

crina