2019獨角獸企業重金招聘Python工程師標準>>>
先說結論:只需要配置JAVA_HOME和path路徑即可,無需配置classpath
參考Oracle官網的說明:
The class path tells JDK tools and applications where to find third-party and user-defined classes -- that is, classes that are not Java extensions or part of the Java platform.
不靠譜翻譯:jdk和應用通過classpath尋找第三方或用戶自定義的類所在的路徑,也就是說,那些不是java擴展或不屬于java平臺的類才需要被加入到classpath中。
The class path is the path that the Java runtime environment searches for classes and other resource files. The class search path (more commonly known by the shorter name, "class path") can be set using either the -classpath option when calling a JDK tool (the preferred method) or by setting the CLASSPATH environment variable. The -classpath option is preferred because you can set it individually for each application without affecting other applications and without other applications modifying its value.
不靠譜翻譯:classpath的路徑就是java運行環境尋找類和其他資源的路徑,classpath可以通過如下兩種方式設置
C:> sdkTool -classpath classpath1;classpath2...
或C:> set CLASSPATH=classpath1;classpath2...
但我們更推薦使用第一種設置方式,因為-classpath的設置方式允許你為每一個應用設置一個獨立的classpath,不會影響其他應用,也不必擔心被其他應用修改了classpath的值。
官網上說的非常明白:我們的rt.jar和tools.jar都是java平臺的jar包,根本不需要添加到classpath中。即使有些需要添加到classpath中的類,也不推薦使用設置classpath的方式。
但為什么有些同學不配置classpath編譯會報錯呢?
其實當你運行:
javac XXX.java
實際上是在運行 :
java -Classpath=%JAVA_HOME%\lib\tools.jar xx.xxx.Main XXX.java
javac就是對上述命令的封裝,報錯是因為沒有配置JAVA_HOME導致java命令找不到tools.jar引起的 !!!
所以一定要配置JAVA_HOME,它不僅作為變量名在path路徑中用到,很多其他的應用如IDE、tomcat默認會從JAVA_HOME中讀取jdk路徑。 每個javaer都配過的環境變量,你現在是否弄明白了呢?