问题描述
在 CentOS 5 linux 上使用一些基本的 java 应用程序,我将 classpath
设置为指向 home/pathToJava/bin
,其中包含 javac
和 java
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
并且我在 home/pathToFolderA/src
和home/pathToFolderB/gen-java
当我在 home/pathToFolderA/src
中运行 javac
和 java
时,一切正常
When I run javac
and java
in home/pathToFolderA/src
everything works perfectly
但是当我在 fileName.java
上的 home/pathToFolderB/gen-java
中运行 javac
时,我得到一个文件未找到错误,具体
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?
感谢大家的帮助
推荐答案
您不应该将 classpath 设置为指向您的 JDK bin 目录——而应该是 PATH 环境变量,它与类路径有不同的用途.(类路径定义了一个包含已编译 Java .class 代码的 jar 和目录列表;PATH 变量定义了一个路径列表,当在当前目录中找不到它们时,shell 需要在其中查找和定位要执行的程序——所以如果你键入例如 zip
-- 它会查看 PATH
中定义的所有目录并找出 zip
程序位于 /usr/bin
)其次,如果你想从两个目录编译源代码,你需要指定:
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:
- 来源所在的所有路径(
home/pathToFolderA/src
和home/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
并运行您编译的程序:
java -classpath /path/to/some.jar:/path/to/another.jar:/home/pathToFolderWithResultsOfCompilation full.name.of.your.Java
这篇关于为什么这个 javac: File Not Found 错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!