为什么会出现 javac:找不到文件错误?

发布于 2024-11-09 09:08:51 字数 644 浏览 5 评论 0原文

在 CentOS 5 linux 上使用一些基本的 java 应用程序,我将 classpath 设置为指向 home/pathToJava/bin,其中包含 javac 和 < code>java

中有 .java 文件

并且我在 home/pathToFolderA/srchome/pathToFolderB/gen-java

当我跑步home/pathToFolderA/src 中的 javacjava 一切正常

但是当我从 中运行 javac 时home/pathToFolderB/gen-java on fileName.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

念三年u 2024-11-16 09:08:51

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

javac /home/pathToFolderA/src/fileName.java

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

The classpath is used to find class files, not source files. (Nor is it used to find the java and javac binaries; those are found in your normal path.) You need to specify the files to compile explicitly:

javac /home/pathToFolderA/src/fileName.java

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

╰ゝ天使的微笑 2024-11-16 09:08:51

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

  • 源代码所在的所有路径(home/pathToFolderA/srchome/pathToFolderB/gen-java >)
  • 要生成的已编译 .class 文件的路径
  • 在类路径中指定您可能在源文件中使用的任何库

如下所示:

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

总而言之,编译:并运行已编译的程序

java -classpath /path/to/some.jar:/path/to/another.jar:/home/pathToFolderWithResultsOfCompilation full.name.of.your.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 in PATH and figure out that zip program is located under /usr/bin)
Secondly if you want to compile sources from both directory you need to specify:

  • all the paths where the sources are (both home/pathToFolderA/src and 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

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
倾`听者〃 2024-11-16 09:08:51

在 CentOS 5 linux 上使用一些基本的 java 应用程序,我将 classpath 设置为指向 home/pathToJava/bin,其中包含 javac > 和 java

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

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

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

That's wrong. The classpath is used to find *.class files, not operating system specific executables. The bin 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.

舟遥客 2024-11-16 09:08:51

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

例如:

HelloWorld.java

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

并确保您将目录更改为文件所在的同一文件夹

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).

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

姜生凉生 2024-11-16 09:08:51

如果没有目录“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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文