Java-What is PATH and CLASSPATH?

4452 단어 Java
큰 소의 인공지능 튜 토리 얼 을 공유 하 다.제로 베이스!통속 적 이 고 알 기 쉽다!유머당신 도 인공지능 팀 에 합류 하 기 를 바 랍 니 다!클릭 하 세 요http://www.captainbed.net
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.

좋은 웹페이지 즐겨찾기