简体   繁体   English

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

[英]What is the difference between Path and ClassPath in Java?

Why do we need Path and ClassPath?为什么我们需要 Path 和 ClassPath? When using IDE's like eclipse we still need to add path ?当使用 IDE 之类的 Eclipse 时,我们还需要添加路径吗?

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

We don't need to set PATH and CLASSPATH to compile and run java program while using IDE like Eclipse .我们不需要设置PATHCLASSPATH来编译和运行 java 程序,而使用Eclipse这样的 IDE。
These environment variables are required to compile and run java program using CMD .使用CMD编译运行java 程序需要这些环境变量。

Example-: Here is the screen shot of console to understand PATH and CLASSPATH quickly示例-:这是控制台的屏幕截图,可以快速了解PATHCLASSPATH

在此处输入图片说明

Explanation-:解释-:

Compiling the program- I have java program file Demo.java stored at location D:\\Programs\\Classes .编译程序 -我将 java 程序文件Demo.java存储在D:\\Programs\\Classes Now I pointed location to D:\\Programs\\Classes in CMD and executed javac Demo.java command.现在我将位置指向 CMD 中的D:\\Programs\\Classes并执行javac Demo.java命令。 System will understand javac with the help of PATH variable.系统将在PATH变量的帮助下理解javac Java program Demo.java is complied successfully because PATH is set correctly to %JAVA_HOME%\\bin . 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 Demo as displayed in second line in the screenshot.运行程序(类文件)-由于类文件已在同一位置生成D:\\Programs\\Classes ,因此我们可以通过键入命令java Demo来运行此类文件,如屏幕截图第二行所示。 Now system will find the class file with the help of CLASSPATH since my CLASSPATH variable has D:\\Programs\\Classes path.现在系统将在CLASSPATH的帮助下找到类文件,因为我的CLASSPATH变量具有D:\\Programs\\Classes路径。

It's not required to point class file location in CMD to run it.不需要在 CMD 中指向类文件位置来运行它。 System will understand java command with the help of PATH variable and find that class using CLASSPATH variable to run it.系统将在PATH变量的帮助下理解java命令,并使用CLASSPATH变量找到该类来运行它。

The path points to the location of the jre ie the java binary files such as the jvm and necessary libraries.路径指向jre 的位置,即java 二进制文件,例如jvm 和必要的库。 The classpath points to the classes you developed so that the jvm can find them and load them when you run your product.类路径指向您开发的类,以便 jvm 可以在您运行产品时找到它们并加载它们。

So essentially you need the path to find java so it can then find your classes and run them from the classpath所以本质上你需要找到java的路径,这样它就可以找到你的类并从类路径中运行它们

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. b) PATH 只不过是为操作系统设置环境。 Operating System will look in this PATH for executables.操作系统将在此 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 ie in J2EE we give the path of jar files. a) Java 编译器使用的一个环境变量来查找类的路径,即在 J2EE 中,我们给出了 jar 文件的路径。

b) Classpath is nothing but setting up the environment for Java. b) Classpath 只不过是为 Java 设置环境。 Java will use to find compiled classes. Java 将用于查找已编译的类。

c) Refers to the Developing Enviornment. c) 指发展环境。

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. PATH 和 CLASSPATH 之间的主要区别在于,PATH 是一个环境变量,用于定位 JDK 二进制文件,如用于运行 java 程序和编译 java 源文件的“java”或“javac”命令。 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.另一方面,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

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 源文件后创建)

Path and Classpath both are operating system level environment variales. Path 和 Classpath 都是操作系统级别的环境变量。 Path is used define where the system can find the executables(.exe) files and classpath is used to specify the location .class files.路径用于定义系统可以找到可执行文件(.exe)文件的位置,类路径用于指定 .class 文件的位置。

PATH is the environment variable where we specify the locations of binaries. PATH 是我们指定二进制文件位置的环境变量。
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.示例:我们添加JDK或JRE的bin目录路径,这样目录下的任何二进制文件都可以直接访问,无需指定绝对路径。

CLASSPATH is the path for Java application where the classes you compiled will be available. CLASSPATH 是 Java 应用程序的路径,您编译的类将在其中可用。

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.路径设置为在您的 java 程序中使用 java 工具,如 java、javac、javap。 javac are used for compile the code. javac 用于编译代码。 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.和类路径用于在您的程序中使用预定义的类,例如在您的程序中使用扫描仪类,为此您需要设置类路径。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM