Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javac: file not found: first.java Usage: javac <options> <source files>

Tags:

java

I have a filename named "first.java" saved on my desktop in notepad++. When I run the cmd command "javac first.java" it gives me this error.

javac: file not found: first.java 
Usage: javac <options> <source files>

I know you are required to go to C:\Programfiles\java\jdk.

and in my C:\Program Files\Java\ I have thesE folders

"jdk1.8.0" "jre6" "jre8"

In C:\Program Files (x86)\Java I have this folder
"jre6"

The Environmental settings are as follows

CLASSPATH
C:\Program Files\Java\jre8\bin
Variable name: LEJOS_NXT_JAVA_HOME
Variable value: C:\Program Files (x86)\Java\jdk1.6.0_21\bin

PATH
Variable name: PATH
Variable value: C:\Program Files\Java\jdk1.8.0\bin

Please tell me where I am going wrong. I have read several posts on the Internet, and I can't figure it out.

like image 838
Ali Avatar asked Dec 14 '13 20:12

Ali


People also ask

What is javac filename Java?

Description. The javac command reads source files that contain module, package and type declarations written in the Java programming language, and compiles them into class files that run on the Java Virtual Machine. The javac command can also process annotations in Java source files and classes.

In which folder javac and Java commands are available?

“javac” is the java compiler available in bin folder of the jdk. “-d” stands for the “directory“. it explains compiler that where the class files should be created. Last argument is the Complete path, where the java file exists.

How do you compile and run a Java program?

Type 'javac MyFirstJavaProgram. java' and press enter to compile your code. If there are no errors in your code, the command prompt will take you to the next line (Assumption: The path variable is set). Now, type ' java MyFirstJavaProgram ' to run your program.


5 Answers

I had the same issue and it was to do with my file name. If you set the file location using CD in CMD, and then type DIR it will list the files in that directory. Check that the file name appears and check that the spelling and filename ending is correct.

It should be .java but mine was .java.txt. The instructions on the Java tutorials website state that you should select "Save as Type Text Documents" but for me that always adds .txt onto the end of the file name. If I change it to "Save as Type All Documents" it correctly saved the file name.

CMD DIR Example

like image 197
Stephen Dryden Avatar answered Oct 23 '22 21:10

Stephen Dryden


I had same problem. The origin of the problem was the creation of text file in Notepad and renaming it as a java file. This implied that the file was saved as WorCount.java.txt file.

To solve this I had to save the file as java file in an IDE or in Nodepad++.

like image 32
shareToProgress Avatar answered Oct 21 '22 04:10

shareToProgress


As KhAn SaAb has stated, you need to set your path.

But in order for your JDK to take advantage of tools such as Maven in the future, I would assign a JAVA_HOME path to point to your JDK folder and then just add the %JAVA_HOME%\bin directory to your PATH.

JAVA_HOME = "C:\Program Files\Java\jdk1.8.0"
PATH = %PATH%\%JAVA_HOME%\bin

In Windows:

  1. right-click "My Computer" and choose "properties" from the context menu.
  2. Choose "Advanced system settings" on the left.
  3. In the "System Properties" popup, in the "Advanced" tab, choose "Environment Variables"
  4. In the "Environment Variables" popup, under the "System variables" section, click "New"
  5. Type "JAVA_HOME" in the "Variable name" field
  6. Paste the path to your JDK folder into the "Variable value" field.
  7. Click OK
  8. In the same section, choose "Path" and change the text where it says:

      blah;blah;blah;C:\Program Files\Java\jdk1.8.0\bin;blah,blah;blah

      to:

      blah;blah;blah;%JAVA_HOME%\bin;blah,blah;blah

like image 6
Mr. Polywhirl Avatar answered Oct 23 '22 19:10

Mr. Polywhirl


For Windows, javac should be a command that you can run from anywhere. If it isn't (which is strange in and of itself), you need to run javac from where it's located, but navigate to the exact location of your Java class file in order to compile it successfully.

By default, javac will compile a file name relative to the current path, and if it can't find the file, it won't compile it.

Please note: You would only be able to use jdk1.8.0 to actually compile, since that would be the only library set that has javac contained in it. Remember: the Java Runtime Environment runs Java classes; the Java Development Kit compiles them.

like image 5
Makoto Avatar answered Oct 23 '22 19:10

Makoto


SET path of JRE as well

jre is nothing but responsible for execute the program

PATH Variable value:

C:\Program Files\Java\jdk1.8.0\bin;C:\Program Files\Java\jre\bin;.;

like image 4
KhAn SaAb Avatar answered Oct 23 '22 19:10

KhAn SaAb