[英]Basic questions about Java CLASSPATH
Suppose I just created a package "example" and have two classes inside it, "Main" and "Helper". 假设我刚刚创建了一个包“ example”,并在其中包含两个类:“ Main”和“ Helper”。
With the simplest possible compilation (eg, $javac Main.java Helper.java) I am already able to run it fine as long as I am in the directory containing the example package, by typing this in the command line: 使用最简单的编译方法(例如$ javac Main.java Helper.java),只要在包含示例包的目录中,我就可以在命令行上运行以下命令来运行它:
$java example.Main
Questions: 问题:
Why would I want to set a CLASSPATH given I can already run the program? 如果我已经可以运行该程序,为什么要设置CLASSPATH? I am guessing to be able to type "$java example.Main" from any directory on my machine, but I am not sure. 我猜想能够从我的计算机上的任何目录中键入“ $ java example.Main”,但我不确定。
What happens when I type "java -cp /path/to/your/java/class/file Main" on the command line? 当我在命令行上输入“ java -cp / path / to / your / java / class / file Main”时会发生什么? Right now I picture there's file containing all the different classpaths, and that command will just add another one to it. 现在我看到有一个包含所有不同类路径的文件,该命令将向其中添加另一个。 Is it the case? 是这样吗
Is there a difference between using "CLASSPATH=/path/to/your/java/class/file" and "java -cp /path/to/your/java/class/file Main" on the command line? 在命令行上使用“ CLASSPATH = / path / to / your / java / class / file”和“ java -cp / path / to / your / java / class / file Main”之间有区别吗? How come the second one has the name of the class (ie Main) in the end? 第二个为什么最后有类的名称(即Main)呢?
java
in your PATH
variable 那当然是假设您在PATH
变量中具有java
的PATH
CLASSPATH
-cp或-classpath将其选项(字符串)添加到CLASSPATH
的任何内容之前 CLASSPATH
is often more convenient as you tend to set your CLASSPATH
once. 使用CLASSPATH
通常更方便,因为您倾向于一次设置CLASSPATH
。 From then on, java Main
is enough to execute the main class. 从那时起, java Main
就足以执行主类。 With java -cp /path/to/your/java/class/file Main
you have to type the -cp /path/to/your/java/class/file
every time. 使用java -cp /path/to/your/java/class/file Main
,每次必须输入-cp /path/to/your/java/class/file
。 That being said, both CLASSPATH
and -cp
or -classpath
options usually contain entries pointing to directories containing java libraries used by your program, not the directory of your program itself. 话虽这么说, CLASSPATH
和-cp
或-classpath
选项通常都包含指向指向包含程序使用的Java库的目录的条目,而不是程序本身的目录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.