Java 什么是类路径以及如何设置它?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2396493/
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
What is a classpath and how do I set it?
提问by Blankman
I was just reading this line:
我只是在读这一行:
The first thing the format() method does is load a Velocity template from the classpath named output.vm
format() 方法做的第一件事是从名为 output.vm 的类路径加载 Velocity 模板
Please explain what was meant by classpath in this context, and how I should set the classpath.
请解释在这种情况下类路径的含义,以及我应该如何设置类路径。
采纳答案by bokmann
When programming in Java, you make other classes available to the class you are writing by putting something like this at the top of your source file:
使用 Java 编程时,您可以通过在源文件的顶部放置如下内容来使其他类可用于您正在编写的类:
import org.javaguy.coolframework.MyClass;
Or sometimes you 'bulk import' stuff by saying:
或者有时你通过说“批量导入”:
import org.javaguy.coolframework.*;
So later in your program when you say:
所以稍后在你的程序中,当你说:
MyClass mine = new MyClass();
The Java Virtual Machine will know where to find your compiled class.
Java 虚拟机将知道在哪里可以找到已编译的类。
It would be impractical to have the VM look through every folder on your machine, so you have to provide the VM a list of places to look. This is done by putting folder and jar files on your classpath.
让虚拟机查看您机器上的每个文件夹是不切实际的,因此您必须为虚拟机提供要查看的位置列表。这是通过将文件夹和 jar 文件放在类路径上来完成的。
Before we talk about how the classpath is set, let's talk about .class files, packages, and .jar files.
在讲如何设置classpath之前,我们先讲一下.class文件、包和.jar文件。
First, let's suppose that MyClass is something you built as part of your project, and it is in a directory in your project called output
. The .class file would be at output/org/javaguy/coolframework/MyClass.class
(along with every other file in that package). In order to get to that file, your path would simply need to contain the folder 'output', not the whole package structure, since your import statement provides all that information to the VM.
首先,让我们假设 MyClass 是您作为项目的一部分构建的,它位于项目中名为output
. .class 文件将位于output/org/javaguy/coolframework/MyClass.class
(以及该包中的所有其他文件)。为了访问该文件,您的路径只需要包含文件夹“输出”,而不是整个包结构,因为您的导入语句向 VM 提供了所有这些信息。
Now let's suppose that you bundle CoolFramework up into a .jar file, and put that CoolFramework.jar into a lib directory in your project. You would now need to put lib/CoolFramework.jar
into your classpath. The VM will look inside the jar file for the org/javaguy/coolframework
part, and find your class.
现在让我们假设您将 CoolFramework 捆绑到一个 .jar 文件中,并将该 CoolFramework.jar 放入您项目的 lib 目录中。您现在需要放入lib/CoolFramework.jar
类路径。VM 将查看该org/javaguy/coolframework
部件的 jar 文件,并找到您的类。
So, classpaths contain:
因此,类路径包含:
- JAR files, and
- Paths to the top of package hierarchies.
- JAR 文件,以及
- 包层次结构顶部的路径。
How do you set your classpath?
你如何设置你的类路径?
The first way everyone seems to learn is with environment variables. On a unix machine, you can say something like:
每个人似乎都学习的第一种方法是使用环境变量。在 unix 机器上,你可以这样说:
export CLASSPATH=/home/myaccount/myproject/lib/CoolFramework.jar:/home/myaccount/myproject/output/
On a Windows machine you have to go to your environment settings and either add or modify the value that is already there.
在 Windows 机器上,您必须转到环境设置并添加或修改已经存在的值。
The second way is to use the -cp
parameter when starting Java, like this:
第二种方式是-cp
在启动Java时使用参数,像这样:
java -cp "/home/myaccount/myproject/lib/CoolFramework.jar:/home/myaccount/myproject/output/" MyMainClass
A variant of this is the third way which is often done with a .sh
or .bat
file that calculates the classpath and passes it to Java via the -cp
parameter.
其变体是第三种方式,通常使用.sh
or.bat
文件来计算类路径并通过-cp
参数将其传递给 Java 。
There is a "gotcha" with all of the above. On most systems (Linux, Mac OS, UNIX, etc) the colon character (':') is the classpath separator. In windowsm the separator is the semicolon (';')
以上所有内容都有一个“陷阱”。在大多数系统(Linux、Mac OS、UNIX 等)上,冒号字符 (':') 是类路径分隔符。在 windowsm 中,分隔符是分号 (';')
So what's the best way to do it?
那么最好的方法是什么?
Setting stuff globally via environment variables is bad, generally for the same kinds of reasons that global variables are bad. You change the CLASSPATH environment variable so one program works, and you end up breaking another program.
通过环境变量全局设置东西是不好的,通常与全局变量不好的原因相同。您更改 CLASSPATH 环境变量以使一个程序运行,但最终破坏了另一个程序。
The -cp is the way to go. I generally make sure my CLASSPATH environment variable is an empty string where I develop, whenever possible, so that I avoid global classpath issues (some tools aren't happy when the global classpath is empty though - I know of two common, mega-thousand dollar licensed J2EE and Java servers that have this kind of issue with their command-line tools).
-cp 是要走的路。我通常确保我的 CLASSPATH 环境变量是我开发的空字符串,只要有可能,这样我就可以避免全局类路径问题(尽管当全局类路径为空时,某些工具并不满意 - 我知道有两个常见的,数以千计的美元许可的 J2EE 和 Java 服务器,它们的命令行工具存在这种问题)。
回答by Romain
The classpath in this context is exactly what it is in the general context: anywhere the VM knows it can find classes to be loaded, and resources as well (such as output.vm in your case).
此上下文中的类路径与一般上下文中的类路径完全相同:VM 知道它可以在任何地方找到要加载的类以及资源(例如 output.vm 在您的情况下)。
I'd understand Velocity expects to find a file named output.vm anywhere in "no package". This can be a JAR, regular folder, ... The root of any of the locations in the application's classpath.
我明白 Velocity 希望在“无包”中的任何地方找到一个名为 output.vm 的文件。这可以是 JAR、常规文件夹、... 应用程序类路径中任何位置的根目录。
回答by Desintegr
The classpath is the path where the Java Virtual Machine look for user-defined classes, packages and resources in Java programs.
类路径是 Java 虚拟机在 Java 程序中查找用户定义的类、包和资源的路径。
In this context, the format()
method load a template file from this path.
在此上下文中,该format()
方法从此路径加载模板文件。
回答by Desintegr
Think of it as Java's answer to the PATH environment variable - OSes search for EXEs on the PATH, Java searches for classes and packages on the classpath.
将其视为 Java 对 PATH 环境变量的回答——操作系统在 PATH 上搜索 EXE,Java 在类路径上搜索类和包。
回答by Bimalendu nath
Classpath is an environment variable of system. The setting of this variable is used to provide the root of any package hierarchy to java compiler.
Classpath 是系统的环境变量。此变量的设置用于向 java 编译器提供任何包层次结构的根。
回答by Unnati Solanki
CLASSPATH is an environment variable (i.e., global variables of the operating system available to all the processes) needed for the Java compiler and runtime to locate the Java packages used in a Java program. (Why not call PACKAGEPATH?) This is similar to another environment variable PATH, which is used by the CMD shell to find the executable programs.
CLASSPATH 是 Java 编译器和运行时定位 Java 程序中使用的 Java 包所需的环境变量(即操作系统的全局变量,可供所有进程使用)。(为什么不调用 PACKAGEPATH?)这类似于另一个环境变量 PATH,CMD shell 使用它来查找可执行程序。
CLASSPATH can be set in one of the following ways:
可以通过以下方式之一设置 CLASSPATH:
CLASSPATH can be set permanently in the environment: In Windows, choose control panel ? System ? Advanced ? Environment Variables ? choose "System Variables" (for all the users) or "User Variables" (only the currently login user) ? choose "Edit" (if CLASSPATH already exists) or "New" ? Enter "CLASSPATH" as the variable name ? Enter the required directories and JAR files (separated by semicolons) as the value (e.g., ".;c:\javaproject\classes;d:\tomcat\lib\servlet-api.jar"). Take note that you need to include the current working directory (denoted by '.') in the CLASSPATH.
To check the current setting of the CLASSPATH, issue the following command:
> SET CLASSPATH
CLASSPATH can be set temporarily for that particular CMD shell session by issuing the following command:
> SET CLASSPATH=.;c:\javaproject\classes;d:\tomcat\lib\servlet-api.jar
Instead of using the CLASSPATH environment variable, you can also use the command-line option -classpath or -cp of the javac and java commands, for example,
> java –classpath c:\javaproject\classes com.abc.project1.subproject2.MyClass3
回答by lft93ryt
Setting the CLASSPATH System Variable
设置 CLASSPATH 系统变量
To display the current CLASSPATH variable, use these commands in Windows and UNIX (Bourne shell):
In Windows: C:\> set CLASSPATH
In UNIX: % echo $CLASSPATH
要显示当前的 CLASSPATH 变量,请在 Windows 和 UNIX(Bourne shell)中使用这些命令: 在 Windows 中: C:\> set CLASSPATH
在 UNIX 中: % echo $CLASSPATH
To delete the current contents of the CLASSPATH variable, use these commands:
In Windows: C:\> set CLASSPATH=
In UNIX: % unset CLASSPATH; export CLASSPATH
要删除 CLASSPATH 变量的当前内容,请使用以下命令: 在 Windows 中: C:\> set CLASSPATH=
在 UNIX 中: % unset CLASSPATH; export CLASSPATH
To set the CLASSPATH variable, use these commands (for example):
In Windows: C:\> set CLASSPATH=C:\users\george\java\classes
In UNIX: % CLASSPATH=/home/george/java/classes; export CLASSPATH
要设置 CLASSPATH 变量,请使用以下命令(例如): 在 Windows 中: C:\> set CLASSPATH=C:\users\george\java\classes
在 UNIX 中: % CLASSPATH=/home/george/java/classes; export CLASSPATH
回答by lft93ryt
Static member of a class can be called directly without creating object instance. Since the main method is static Java virtual Machine can call it without creating any instance of a class which contains the main method, which is start point of program.
可以直接调用类的静态成员,无需创建对象实例。由于 main 方法是静态的 Java 虚拟机可以在不创建包含 main 方法的类的任何实例的情况下调用它,这是程序的起点。
回答by Adam Winter
For linux users, and to sum up and add to what others have said here, you should know the following:
对于linux用户,总结并补充其他人在这里所说的,你应该知道以下几点:
$CLASSPATH is what Java uses to look through multiple directories to find all the different classes it needs for your script (unless you explicitly tell it otherwise with the -cp override). Using -cp requires that you keep track of all the directories manually and copy-paste that line every time you run the program (not preferable IMO).
The colon (":") character separates the different directories. There is only one $CLASSPATH and it has all the directories in it. So, when you run "export CLASSPATH=...." you want to include the current value "$CLASSPATH" in order to append to it. For example:
export CLASSPATH=. export CLASSPATH=$CLASSPATH:/usr/share/java/mysql-connector-java-5.1.12.jar
In the first line above, you start CLASSPATH out with just a simple 'dot' which is the path to your current working directory. With that, whenever you run java it will look in the current working directory (the one you're in) for classes. In the second line above, $CLASSPATH grabs the value that you previously entered (.) and appends the path to a mysql dirver. Now, java will look for the driver AND for your classes.
echo $CLASSPATH
is super handy, and what it returns should read like a colon-separated list of all the directories, and .jar files, you want java looking in for the classes it needs.
Tomcat does not use CLASSPATH. Read what to do about that here: https://tomcat.apache.org/tomcat-8.0-doc/class-loader-howto.html
$CLASSPATH 是 Java 用来查看多个目录以查找脚本所需的所有不同类的工具(除非您使用 -cp 覆盖明确告诉它其他情况)。使用 -cp 要求您手动跟踪所有目录并在每次运行程序时复制粘贴该行(IMO 不推荐)。
冒号 (":") 字符分隔不同的目录。只有一个 $CLASSPATH 并且其中包含所有目录。因此,当您运行“export CLASSPATH=....”时,您希望包含当前值“$CLASSPATH”以便附加到它。例如:
export CLASSPATH=. export CLASSPATH=$CLASSPATH:/usr/share/java/mysql-connector-java-5.1.12.jar
在上面的第一行中,您只用一个简单的“点”开始 CLASSPATH,它是您当前工作目录的路径。这样,无论何时运行 java,它都会在当前工作目录(您所在的目录)中查找类。在上面的第二行中,$CLASSPATH 获取您之前输入的值 (.) 并将路径附加到 mysql 驱动程序。现在,java 将查找驱动程序和您的类。
echo $CLASSPATH
非常方便,它返回的内容应该像所有目录和 .jar 文件的冒号分隔列表,您希望 java 查找它需要的类。
Tomcat 不使用 CLASSPATH。在此处阅读如何处理:https: //tomcat.apache.org/tomcat-8.0-doc/class-loader-howto.html