Java-What is PATH and CLASSPATH?
4452 단어 Java
The PATH and CLASSPATH are two most important environment variables of Java environment which is used to find the JDK binaries used to compile and run Java in windows and Linux and class files which are compiled Java bytecodes. From my personal experience I can say that PATH and CLASSPATH are two most problematic thing for beginners in Java programming language due to the following reasons:
First because in most of Java courses nobody tell details of what is a PATH and CLASSPATH, what do PATH and CLASSPATH do, what is meaning of setting PATH and CLASSPATH, what happens if we do not set them, differences between PATH vs CLASSPATH in Java or simply how CLASSPATH works in java etc. These basic question which answers most of the details about PATH and CLASSPATH in Java are mostly not answered until Java programmer itself acquire these knowledge. Things may be changed nowadays but important of PATH and CLASSPATH is still high. Most common cause of dreaded error like java.lang.NoClassDefFoundError and java.lang.ClassNotFoundException is either incorrect or misconfigured CLASSPATH in Java. In this article, I'll tell you about practical difference between PATH and CLASSPATH environment variable, where are they located and how exactly they are used by Java compiler and JVM. Once you know this basic detail, you would be able to solve most of the classpath related problem by yourself.
Difference between PATH and CLASSPATH in Java
Here are some of the common difference between PATH vs CLASSPATH in Java:
1)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.
2) In order to set PATH in Java, you need to include JDK_HOME/bin directory in PATH environment variable while in order to set CLASSPATH in Java you need to include all those directories where you have put either your .class file or JAR file which is required by your Java application.
3) Another significant difference between PATH and CLASSPATH is that PATH can not be overridden by any Java settings but CLASSPATH can be overridden by providing command line option -classpath or -cp to both "java" and "javac" commands or by using Class-Path attribute in Manifest file inside JAR archive.
4) PATH environment variable is used by operating system to find any binary or command typed in the shell, this is true for both Windows and Linux environment while CLASSPATH is only used by Java ClassLoaders to load class files.
These were some notable difference between PATH vs CLASSPATH in Java and they are worth remembering to debug and troubleshoot Java-related issues. Though, I highly recommend you to read Core Java Volume 1 - Fundamentals by Cay S. Horstmann to build your fundamentals in Java.
How to set PATH and CLASSPATH in Windows and Unix
If you are familiar with DOS operating system and how to use command prompt in Windows or shell in Linux setting PATH and CLASSPATH is a trivial exercise. Both PATH and CLASSPATH are environment variable and can be set using export command in Linux and using set keyword in DOS and Windows as shown below: Command to set PATH in Windows
set PATH=%PATH%;C:\Program Files\Java\JDK1.6.20\bin
Command to set PATH in UNIX/Linux
export PATH = ${PATH}:/opt/Java/JDK1.6.18/bin
Look at the difference between two commands, in Linux use a colon(:) as a separator and in Windows use semi-colon(;) as a separator. Command to set CLASSPATH in windows
set CLASSPATH=%CLASSPATH%;C:\Program Files\Java\JDK1.6.20\lib
Command to set CLASSPATH in Unix/Linux
export CLASSPATH= ${CLASSPATH}:/opt/Java/JDK1.6.18/lib
Also, don't forget to include current directory, denoted by a dot(.) to include in CLASSPATH, this will ensure that it will look first in the current directory and if it found the class it will use that even if that class also exists in another directory which exists in CLASSPATH.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
JPA + QueryDSL 계층형 댓글, 대댓글 구현(2)이번엔 전편에 이어서 계층형 댓글, 대댓글을 다시 리팩토링해볼 예정이다. 이전 게시글에서는 계층형 댓글, 대댓글을 구현은 되었지만 N+1 문제가 있었다. 이번에는 그 N+1 문제를 해결해 볼 것이다. 위의 로직은 이...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.