javac:找不到文件:first.java 用法:javac <options> <source files>

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20587840/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 02:57:41  来源:igfitidea点击:

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

java

提问by Ali

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.

我在记事本++的桌面上保存了一个名为“first.java”的文件名。当我运行 cmd 命令“javac first.java”时,它给了我这个错误。

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

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

我知道您需要转到 C:\Programfiles\java\jdk。

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

在我的 C:\Program Files\Java\ 我有这些E文件夹

"jdk1.8.0" "jre6" "jre8"

"jdk1.8.0" "jre6" "jre8"

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

在 C:\Program Files (x86)\Java 我有这个文件夹
“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

CLASSPATH
C:\Program Files\Java\jre8\bin
变量名:LEJOS_NXT_JAVA_HOME
变量值:C:\Program Files (x86)\Java\jdk1.6.0_21\bin

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

PATH
变量名:PATH
变量值: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.

请告诉我哪里出错了。我在网上看了好几篇文章,都搞不清楚。

回答by KhAn SaAb

SET path of JRE as well

JRE 的 SET 路径

jre is nothing but responsible for execute the program

jre 只是负责执行程序

PATH Variable value:

路径变量值:

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

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

回答by Makoto

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

对于 Windows,javac应该是一个可以从任何地方运行的命令。如果不是(这本身就很奇怪),您需要javac从它所在的位置运行,但导航到Java 类文件的确切位置以成功编译它。

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

默认情况下,javac会编译一个相对于当前路径的文件名,如果找不到该文件,则不会编译它。

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 javaccontained in it. Remember: the Java Runtime Environment runs Java classes; the Java Development Kit compiles them.

请注意:您只能使用 jdk1.8.0 来实际编译,因为那将是其中javac包含的唯一库集。请记住:Java 运行时环境运行 Java 类;Java 开发工具包编译它们。

回答by Mr. Polywhirl

As nAvEeD kHaNhas stated, you need to set your path.

如前所述nAvEeD kHaN,您需要设置路径。

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

但是为了让您的 JDK 将来能够利用诸如 Maven 之类的工具,我会分配一个JAVA_HOME路径来指向您的 JDK 文件夹,然后将该%JAVA_HOME%\bin目录添加到您的PATH.

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


In Windows:

在 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 folderinto the "Variable value" field.
  7. Click OK
  8. In the same section, choose "Path" and change the text where it says:
  1. right-click“我的电脑”并从上下文菜单中选择“属性”。
  2. 选择左侧的“高级系统设置”。
  3. 在“系统属性”弹出窗口中,在“高级”选项卡中,选择“环境变量”
  4. 在“环境变量”弹出窗口中,在“系统变量”部分下,单击“新建”
  5. 在“变量名”字段中输入“ JAVA_HOME
  6. 将 JDK 文件夹路径粘贴到“变量值”字段中。
  7. 点击 OK
  8. 在同一部分中,选择“路径”并更改其显示的文本:

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

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

      to:

      到:

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

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

回答by vinaykotagi

If this is the problem then go to the place where the javac is present and then type

如果这是问题,请转到存在 javac 的位置,然后键入

  1. notepad Yourfilename.java
  2. it will ask for creation of the file
  3. say yes and copy the code from your previous source file to this file
  4. now execute the cmd------>javac Yourfilename.java
  1. 记事本你的文件名.java
  2. 它将要求创建文件
  3. 说是,然后将您之前的源文件中的代码复制到此文件中
  4. 现在执行 cmd------>javac Yourfilename.java

It will work for sure.

它肯定会起作用。

回答by Ramson Mwangi

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.

这应该显示您的java版本。如果没有,则说明您没有在路径中正确设置 java。

navigate to C:

导航到 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).

-sourcepath 是 .java 文件的完整路径,-d 是您希望 .class 文件所在的路径/目录,最后是您想要编译的文件(first.java)。

回答by Jai Bansal

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.javafile was located. So I used the command cdand right after that the location of the file. That worked for me. Tell me if it helps! Thanks!

所以我遇到了同样的问题,因为我不在我的文件所在的正确目录中。所以当我跑javac Example.java(对我来说)它说它找不到它。但我需要去我的Example.java文件所在的地方。所以我使用了命令cd,然后是文件的位置。那对我有用。告诉我它是否有帮助!谢谢!

回答by Stephen Dryden

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.

我有同样的问题,这与我的文件名有关。如果您在 CMD 中使用 CD 设置文件位置,然后键入 DIR,它将列出该目录中的文件。检查文件名是否出现并检查拼写和文件名结尾是否正确。

It should be .java but mine was .java.txt. The instructions on the Java tutorials websitestate 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.

它应该是 .java 但我的是 .java.txt。在该指令的Java教程的网站,你应该选择“保存类型文本文档”,但对我来说总是添加到名为.txt的文件名的最终状态。如果我将其更改为“另存为类型所有文档”,它会正确保存文件名。

CMD DIR Example

CMD 目录示例

回答by shareToProgress

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.

我有同样的问题。问题的根源是在记事本中创建文本文件并将其重命名为 java 文件。这意味着该文件已保存为 WorCount.java.txt 文件。

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

为了解决这个问题,我必须将文件保存为IDE 或 Nodepad++ 中的java 文件

回答by Android

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

首先在环境变量中将路径设置为 C:/Program Files/Java/jdk1.8.0/bin。假设您的文件保存在 C: 驱动器中 现在打开命令提示符并使用 c: javac Filename.java java Filename 移动到目录

Make sure you save the file name as .java.

确保将文件名另存为 .java。

If it is saved as a text file name then file not found error results as it cannot the text file.

如果将其保存为文本文件名,则会导致找不到文件错误,因为它无法保存文本文件。

回答by Beginner_PCR

Here is the way I executed the program without environment variable configured.

这是我在没有配置环境变量的情况下执行程序的方式。

Java file execution procedure: After you saved a file MyFirstJavaProgram.java

Java文件执行程序:保存文件后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

输入“Javac”的整个路径后跟java文件用于执行输出路径后跟注释<-cp>后跟

Given below is the example of execution

下面给出的是执行的例子

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

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