为什么不设置classpath java也没什么异常,java开发环境:还在配classpath?你out啦!...

先说结论:只需要配置JAVA_HOME和path路径即可,无需配置classpath

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都配过的环境变量,你现在是否弄明白了呢?

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值