Java中的Path和ClassPath有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33062443/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 14:15:31  来源:igfitidea点击:

What is the difference between Path and ClassPath in Java?

javapathclasspath

提问by Junaid

Why do we need Path and ClassPath? When using IDE's like eclipse we still need to add path ?

为什么我们需要 Path 和 ClassPath?当使用 IDE 之类的 Eclipse 时,我们还需要添加路径吗?

回答by triadiktyo

The path points to the location of the jre i.e. the java binary files such as the jvm and necessary libraries. The classpath points to the classes you developed so that the jvm can find them and load them when you run your product.

路径指向jre 的位置,即java 二进制文件,例如jvm 和必要的库。类路径指向您开发的类,以便 jvm 可以在您运行产品时找到它们并加载它们。

So essentially you need the path to find java so it can then find your classes and run them from the classpath

所以本质上你需要找到java的路径,这样它就可以找到你的类并从类路径中运行它们

回答by Saravana

PATH is the environment variable where we specify the locations of binaries.
Example: We add bin directory path of JDK or JRE, so that any binaries under the directory can be accessed directly without specifying absolute path.

CLASSPATH is the path for Java application where the classes you compiled will be available.

PATH 是我们指定二进制文件位置的环境变量。
示例:我们添加JDK或JRE的bin目录路径,这样目录下的任何二进制文件都可以直接访问,无需指定绝对路径。

CLASSPATH 是 Java 应用程序的路径,您编译的类将在其中可用。

回答by hitesh

Difference between path and classpath

路径和类路径的区别

Difference between path and classpath in Java

Java中路径和类路径的区别

path is set for use java tool in your java program like java, javac, javap. javac are used for compile the code. and classpath are used for use predefined class in your program for example use scanner class in your program for this you need to set classpath.

路径设置为在您的 java 程序中使用 java 工具,如 java、javac、javap。javac 用于编译代码。和类路径用于在您的程序中使用预定义的类,例如在您的程序中使用扫描仪类,为此您需要设置类路径。

http://www.tutorial4us.com/java/difference-between-path-and-classpath

http://www.tutorial4us.com/java/difference-between-path-and-classpath

回答by Sangram Shivankar

path : it is location of bin files(binary executable files) example- java.exe,javac.exe

路径:它是 bin 文件(二进制可执行文件)的位置,例如 - java.exe,javac.exe

classPath: it is location of your .class file(which is created after compile your java source file)

classPath:它是您的 .class 文件的位置(在编译您的 java 源文件后创建)

回答by Dharmendra Barad

Path and Classpath both are operating system level environment variales. Path is used define where the system can find the executables(.exe) files and classpath is used to specify the location .class files.

Path 和 Classpath 都是操作系统级别的环境变量。路径用于定义系统可以找到可执行文件(.exe)文件的位置,类路径用于指定 .class 文件的位置。

回答by Pravas Kumar

path is a mediator between developer and operating system to inform binary file path where as Classpath is a mediator between developer and compiler to inform the library file path those are used in our source code

path 是开发人员和操作系统之间的中介,用于通知二进制文件路径,而 Classpath 是开发人员和编译器之间的中介,用于通知我们源代码中使用的库文件路径

回答by Ashish Kumar

We don't need to set PATHand CLASSPATHto compile and run java program while using IDE like Eclipse.
These environment variables are required to compileand runjava program using CMD.

Example-:Here is the screen shot of console to understand PATHand CLASSPATHquickly

我们不需要设置PATHCLASSPATH来编译和运行 java 程序,而使用Eclipse这样的 IDE 。使用CMD编译运行java 程序
需要这些环境变量。示例-:这是控制台的屏幕截图,可以快速了解PATHCLASSPATH

enter image description here

在此处输入图片说明

Explanation-:

Compiling the program-I have java program file Demo.javastored at location D:\Programs\Classes. Now I pointed location to D:\Programs\Classesin CMD and executed javac Demo.javacommand. System will understand javacwith the help of PATHvariable. Java program Demo.javais complied successfully because PATHis set correctly to %JAVA_HOME%\bin.

说明-:

编译程序-我将java程序文件Demo.java存储在位置D:\Programs\Classes。现在我D:\Programs\Classes在 CMD 中指向 location并执行javac Demo.java命令。系统将javacPATH变量的帮助下理解。Java 程序Demo.java编译成功,因为PATH正确设置为%JAVA_HOME%\bin.

Running the program (class file)-Since class file has been generated at the same location D:\Programs\Classes, so we can run this class file by typing command java Demoas displayed in second line in the screenshot. Now system will find the class file with the help of CLASSPATHsince my CLASSPATHvariable has D:\Programs\Classespath.

运行程序(类文件)-由于类文件已在同一位置生成D:\Programs\Classes,因此我们可以通过键入命令java Demo来运行此类文件,如屏幕截图第二行所示。现在系统将在CLASSPATH的帮助下找到类文件,因为我的CLASSPATH变量有D:\Programs\Classes路径。

It's not required to point class file location in CMD to run it.System will understand javacommand with the help of PATHvariable and find that class using CLASSPATHvariable to run it.

不需要在 CMD 中指向类文件位置来运行它。系统将在PATH变量的帮助下理解java命令,并使用CLASSPATH变量找到该类来运行它。

回答by Yash P Shah

The main difference between PATH and CLASSPATH is that PATH is an environment variable which is used to locate JDK binaries like "java" or "javac" command used to run java program and compile java source file. On the other hand, CLASSPATH, an environment variable is used by System or Application ClassLoader to locate and load compile Java bytecodes stored in the .class file.

PATH 和 CLASSPATH 之间的主要区别在于,PATH 是一个环境变量,用于定位 JDK 二进制文件,如用于运行 java 程序和编译 java 源文件的“java”或“javac”命令。另一方面,CLASSPATH,系统或应用程序类加载器使用环境变量来定位和加载存储在 .class 文件中的编译 Java 字节码。

For more info: http://www.java67.com/2012/08/what-is-path-and-classpath-in-java-difference.html

更多信息:http: //www.java67.com/2012/08/what-is-path-and-classpath-in-java-difference.html

回答by N.Neupane

let us clear the difference in points:

让我们明确点的差异:

PATH

小路

a) An environment variable which is used by the operating system to find the executables.

a) 操作系统用来查找可执行文件的环境变量。

b) PATH is nothing but setting up an environment for operating system. Operating System will look in this PATH for executables.

b) PATH 只不过是为操作系统设置环境。操作系统将在此 PATH 中查找可执行文件。

c) Refers to the system

c) 指系统

CLASSPATH

类路径

a) An environment variable which is used by the Java compiler to find the path, of classes i.e in J2EE we give the path of jar files.

a) Java 编译器使用的一个环境变量来查找类的路径,即在 J2EE 中,我们给出了 jar 文件的路径。

b) Classpath is nothing but setting up the environment for Java. Java will use to find compiled classes.

b) Classpath 只不过是为 Java 设置环境。Java 将用于查找已编译的类。

c) Refers to the Developing Enviornment.

c) 指发展环境。