Reputation: 121
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.
Upvotes: 11
Views: 233891
Reputation: 11
This error usually happens if you're not in the same directory as the file. Here are solutions for different scenarios:
Terminal: Navigate to the directory where filename.java is located before running: cd /path/to/your/file javac filename.java IDE (e.g., VS Code): Clear the cache and reload the IDE. This should recognize the file even if the terminal isn’t in the exact directory.
Hope this helps! 😊
Upvotes: 0
Reputation: 11
You are most probably saving your file as a .txt document (App.java.txt), to fix this while Saving As write the file name in "", like "App.java"
Writing file names in double quotation marks ensures that the file is saved with the exact extension specified.
You can confirm this by running the command dir
and seeing the extension of your file
Upvotes: 0
Reputation: 75
There was an extra space in my case. I wrote MyFile .java instead of MyFile.java. I corrected this mistake and I was free of this issue.
Upvotes: 0
Reputation: 138
if your file located different folder
cd cd C:\Users\Owner\IdeaProjects\headFirst\sink-startups-game
then cd src
If you have just one file that located somewhere press Shift+right click -> Copy file Path
and then you can execute same way : cd + path(Ctrl+v)
to see files in that directory - dir
+Enter
javac filename.java
then java filename
To compile all files in given directory: javac *.java
then easyly you can use java anyfilename
Upvotes: 0
Reputation: 48610
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:
blah;blah;blah;
C:\Program Files\Java\jdk1.8.0\bin;blah,blah;blah
to:
blah;blah;blah;
%JAVA_HOME%\bin;blah,blah;blah
Upvotes: 6
Reputation: 690
first set the path of jdk bin steps to be follow: - open computer properties. - Advanced system settings - Environment Variables look for the system variables -Click on the "path" variable - Edit copy the jdk bin path and done.
The Problem is you just open the command prompt which is default set on current user like "C:\users\ABC>" You have to change the location where your .java file store like "D:\javafiles>"
Now you are able to run the command javac filename.java
Upvotes: 0
Reputation: 584
just open file then click on save as then name it as first.java then in that box there is another dropdown in that dropdown (save as type) select type as all and save it now you can compile it
Upvotes: 0
Reputation: 11
Sometimes this issue occurs, If you are creating a java file for the first time in your system. The Extension of your Java file gets saved as the text file.
e.g Example.java.txt (wrong extension)
You need to change the text file extension to java file.
It should be like:
Example.java (right extension)
Upvotes: 1
Reputation: 11
Java file execution procedure: After you saved a file MyFirstJavaProgram2.java
Enter the whole Path of "Javac" followed by java file For executing output Path of followed by comment <-cp> followed by followed by
C:\Program Files\Java\jdk1.8.0_101\bin>javac C:\Sample\MyFirstJavaProgram2.java
C:\Program Files\Java\jdk1.8.0_101\bin>java -cp C:\Sample MyFirstJavaProgram2
Hello World
Upvotes: 1
Reputation: 11
Here is the way I executed the program without environment variable configured.
Java file execution procedure: After you saved a file MyFirstJavaProgram.java
Enter the whole Path of "Javac" followed by java file For executing output Path of followed by comment <-cp> followed by followed by
C:\Program Files\Java\jdk1.8.0_101\bin>javac C:\Sample\MyFirstJavaProgram2.java C:\Program Files\Java\jdk1.8.0_101\bin>java -cp C:\Sample MyFirstJavaProgram2 Hello World
Upvotes: 0
Reputation: 54
First set your path to C:/Program Files/Java/jdk1.8.0/bin in the environment variables. Say, your file is saved in the C: drive Now open the command prompt and move to the directory using c: javac Filename.java java Filename
Make sure you save the file name as .java.
If it is saved as a text file name then file not found error results as it cannot the text file.
Upvotes: 0
Reputation: 31
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++.
Upvotes: 3
Reputation: 571
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.
Upvotes: 9
Reputation: 11
So I had the same problem because I wasn't in the right directory where my file was located. So when I ran javac Example.java
(for me) it said it couldn't find it. But I needed to go to where my Example.java
file was located. So I used the command cd
and right after that the location of the file. That worked for me. Tell me if it helps! Thanks!
Upvotes: 1
Reputation: 135
Seems like you have your path right. But what is your working directory? on command prompt run:
javac -version
this should show your java version. if it doesnt, you have not set java in path correctly.
navigate to C:
cd \
Then:
javac -sourcepath users\AccName\Desktop -d users\AccName\Desktop first.java
-sourcepath is the complete path to your .java file, -d is the path/directory you want your .class files to be, then finally the file you want compiled (first.java).
Upvotes: 4
Reputation: 11
If this is the problem then go to the place where the javac is present and then type
It will work for sure.
Upvotes: 0
Reputation: 106430
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.
Upvotes: 6
Reputation: 5366
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;.;
Upvotes: 4