Linux 为什么这个 javac: File Not Found 错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6108598/
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-04 00:56:33  来源:igfitidea点击:

Why this javac: File Not Found error?

javalinuxcommand-linecentosjavac

提问by algorithmicCoder

Working with some basic java apps on CentOS 5 linux and I have my classpathset to point to home/pathToJava/binwhich contains javacand java

在 CentOS 5 linux 上使用一些基本的 java 应用程序,我classpath设置指向home/pathToJava/bin哪个包含javacjava

and I have .javafiles in home/pathToFolderA/src

我有.java文件home/pathToFolderA/src

and home/pathToFolderB/gen-java

home/pathToFolderB/gen-java

When I run javacand javain home/pathToFolderA/srceverything works perfectly

当我运行javac,并javahome/pathToFolderA/src一切完美

But when I run javacfrom within home/pathToFolderB/gen-javaon fileName.javaI get a file not found error, specifically

但是当我javac从内部运行时home/pathToFolderB/gen-javafileName.java我收到一个找不到文件的错误,特别是

javac: file Not found: fileName.java
Usage: javac <options> <source files>

Why could this be happening?

为什么会发生这种情况?

Thanks for all help

感谢所有帮助

采纳答案by Liv

You shouldn't set your classpathto point to your JDK bin directory -- instead it should be the PATHenvironment variable, which serves a different purpose to classpath. (The classpath defines a list of jars and directories containing compiled Java .class code; the PATH variable defines a list of paths where the shell needs to look and locate programs to execute when they are not found in the current directory -- so if you type for instance zip-- it would look in all the directories defined in PATHand figure out that zipprogram is located under /usr/bin) Secondly if you want to compile sources from both directory you need to specify:

您不应该将类路径设置为指向 JDK bin 目录——而应该是PATH环境变量,它与类路径的用途不同。(类路径定义了一个包含已编译 Java .class 代码的 jar 和目录列表;PATH 变量定义了一个路径列表,当在当前目录中找不到它们时,shell 需要在其中查找和定位要执行的程序——所以如果你例如输入zip——它会查看定义在的所有目录PATH并找出该zip程序位于/usr/bin) 其次,如果你想从两个目录编译源代码,你需要指定:

  • all the paths where the sources are (both home/pathToFolderA/srcand home/pathToFolderB/gen-java)
  • the path where the compiled .class files to be generated
  • specify in the classpath any library you might use in your source files
  • 来源所在的所有路径(home/pathToFolderA/srchome/pathToFolderB/gen-java
  • 要生成的已编译 .class 文件的路径
  • 在类路径中指定您可能在源文件中使用的任何库

To sum it up, it would be something like this to compile:

总而言之,编译将是这样的:

javac -d /home/pathToFolderWithResultsOfCompilation -classpath /path/to/some.jar:/path/to/another.jar home/pathToFolderA/src/*.java home/pathToFolderB/gen-java/*.java 

and to run your compiled programs:

并运行您编译的程序:

java -classpath /path/to/some.jar:/path/to/another.jar:/home/pathToFolderWithResultsOfCompilation full.name.of.your.Java

回答by Jon Skeet

The classpath is used to find classfiles, not sourcefiles. (Nor is it used to find the javaand javacbinaries; those are found in your normal path.) You need to specify the files to compile explicitly:

类路径用于查找文件,而不是文件。(它也不用于查找javajavac二进制文件;它们可以在您的正常路径中找到。)您需要明确指定要编译的文件:

javac /home/pathToFolderA/src/fileName.java

Obviously if you're already in/home/pathToFolderA/srcthen you can just use fileName.javabecause that's treated as being relative to your current directory.

显然,如果您已经在,/home/pathToFolderA/src那么您可以使用,fileName.java因为它被视为相对于您的当前目录。

回答by Jesper

Working with some basic java apps on CentOS 5 linux and I have my classpathset to point to home/pathToJava/binwhich contains javacand java

在 CentOS 5 linux 上使用一些基本的 java 应用程序,我classpath设置指向home/pathToJava/bin哪个包含javacjava

That's wrong. The classpath is used to find *.classfiles, not operating system specific executables. The bindirectory of your JDK does not belong in the classpath. Note that the classpath is also not for finding *.javasource files.

那是错误的。类路径用于查找*.class文件,而不是操作系统特定的可执行文件。在bin你的JDK的目录并不在classpath的归属。请注意,类路径也不用于查找*.java源文件。

When you run javacyou need to specify the path to the source file, if it isn't in the current directory.

当您运行时,javac您需要指定源文件的路径,如果它不在当前目录中。

回答by jwp

Without a listing of the directory "gen-java" and the exact command you're typing,my guess would be that you're trying to compile a file that doesn't exist. Linux is case sensitive, so maybe that's your problem. Or the file doesn't exist.

如果没有目录“gen-java”的列表和您输入的确切命令,我的猜测是您正在尝试编译一个不存在的文件。Linux 区分大小写,所以这可能是您的问题。或者文件不存在。

回答by user3063662

make sure that your file name contain no spaces

确保您的文件名不包含空格

Eg:

例如:

HelloWorld.java

usually the errors occur when you rename the file by copy past that will cause a space between the name and the dot (this is the mistake:HelloWorld .java).

通常当您通过复制过去重命名文件时会发生错误,这将导致名称和点之间出现空格(这是错误:)HelloWorld .java

and make sure you changed the directory to the same folder your file in

并确保您将目录更改为与您的文件相同的文件夹