为什么会出现 javac:找不到文件错误?
在 CentOS 5 linux 上使用一些基本的 java 应用程序,我将 classpath
设置为指向 home/pathToJava/bin
,其中包含 javac
和 < code>java
中有 .java
文件
并且我在 home/pathToFolderA/src
和 home/pathToFolderB/gen-java
当我跑步home/pathToFolderA/src
中的 javac
和 java
一切正常
但是当我从 中运行
on javac
时home/pathToFolderB/gen-javafileName.java
我收到文件未找到错误,具体来说
javac: file Not found: fileName.java
Usage: javac <options> <source files>
为什么会发生这种情况?
感谢所有帮助
Working with some basic java apps on CentOS 5 linux and I have my classpath
set to point to home/pathToJava/bin
which contains javac
and java
and I have .java
files in home/pathToFolderA/src
and home/pathToFolderB/gen-java
When I run javac
and java
in home/pathToFolderA/src
everything works perfectly
But when I run javac
from within home/pathToFolderB/gen-java
on fileName.java
I get a file not found error, specifically
javac: file Not found: fileName.java
Usage: javac <options> <source files>
Why could this be happening?
Thanks for all help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
类路径用于查找类文件,而不是源文件。 (它也不用于查找
java
和javac
二进制文件;这些二进制文件可以在您的正常路径中找到。)您需要指定要显式编译的文件:显然,如果您'如果已经在
/home/pathToFolderA/src
中,那么您可以只使用fileName.java
,因为它被视为相对于您当前的目录。The classpath is used to find class files, not source files. (Nor is it used to find the
java
andjavac
binaries; those are found in your normal path.) You need to specify the files to compile explicitly:Obviously if you're already in
/home/pathToFolderA/src
then you can just usefileName.java
because that's treated as being relative to your current directory.您不应该将 classpath 设置为指向 JDK bin 目录 - 相反,它应该是 PATH 环境变量,它与类路径有不同的用途。 (类路径定义了包含已编译的 Java .class 代码的 jar 和目录的列表;PATH 变量定义了一个路径列表,当在当前目录中找不到程序时,shell 需要在这些路径中查找和定位要执行的程序 - 因此,如果您例如键入
zip
——它将查找PATH
中定义的所有目录,并找出zip
程序位于/ 下usr/bin
)其次,如果您想从两个目录编译源代码,则需要指定:
home/pathToFolderA/src
和home/pathToFolderB/gen-java
>)如下所示:
总而言之,编译:并运行已编译的程序
You shouldn't set your classpath to point to your JDK bin directory -- instead it should be the PATH environment 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 inPATH
and figure out thatzip
program is located under/usr/bin
)Secondly if you want to compile sources from both directory you need to specify:
home/pathToFolderA/src
andhome/pathToFolderB/gen-java
)To sum it up, it would be something like this to compile:
and to run your compiled programs:
这是错误的。类路径用于查找 *.class 文件,而不是特定于操作系统的可执行文件。 JDK 的
bin
目录不属于类路径。请注意,类路径也不是用于查找 *.java 源文件。当您运行
javac
时,您需要指定源文件的路径(如果它不在当前目录中)。That's wrong. The classpath is used to find
*.class
files, not operating system specific executables. Thebin
directory of your JDK does not belong in the classpath. Note that the classpath is also not for finding*.java
source files.When you run
javac
you need to specify the path to the source file, if it isn't in the current directory.确保您的文件名不包含空格
例如:
通常当您通过复制过去重命名文件时会发生错误,这会导致名称和点之间出现空格(这是错误:
HelloWorld .java
)。并确保您将目录更改为文件所在的同一文件夹
make sure that your file name contain no spaces
Eg:
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
).and make sure you changed the directory to the same folder your file in
如果没有目录“gen-java”的列表和您正在键入的确切命令,我的猜测是您正在尝试编译一个不存在的文件。 Linux 区分大小写,所以也许这就是你的问题。或者该文件不存在。
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.