简体   繁体   English

在Java中设置CLASSPATH的目的是什么?

[英]What is the purpose of setting a CLASSPATH in Java?

I just installed Java on a Mac for the first time (jdk 10.0.2 SE) and I'm on macOS 10.13.6. 我是第一次在Mac(jdk 10.0.2 SE)上安装Java,然后在macOS 10.13.6上安装了Java。 I just created a basic Hello World program in my home directory, compiled it and ran it, but I have nothing set on my CLASSPATH environment variable except for the current directory "." 我只是在主目录中创建了一个基本的Hello World程序,对其进行了编译并运行了该程序,但是除了当前目录“”之外,我的CLASSPATH环境变量上没有任何设置。 My question is how am I able to compile and run this program? 我的问题是我如何能够编译和运行该程序? How is it finding the classes from the Java library? 如何从Java库中找到类? I've always assumed I need to set this variable, and I have it set to my jdk installation directory in Windows. 我一直认为我需要设置此变量,并将其设置为Windows中的jdk安装目录。

WHY DO WE SET THE CLASS-PATH? 我们为什么要设置班级路径?

well this is an riveting question. 好吧,这是一个吸引人的问题。

it is self explanatory as 这是不言而喻的

CLASSPATH is an environment variable and contain CLASSPATH是环境变量,包含

paths to the jar files and path to various packages. jar文件的路径以及各种包的路径。

KISS ANSWER TO YOUR QUERY: 接吻您的查询:

Perhaps ,the reason behind successful execution of your code is that the JVM checks the current directory first for the perception of jar files and then follows the classpath .So your current directory has those files. 也许,成功执行代码的原因在于JVM首先检查当前目录以了解jar文件,然后遵循类路径。因此,当前目录中包含这些文件。

OH!You may wonder then why would we set the classpath .Setting the classpath overrides that by default path. 哦,您可能想知道为什么我们要设置classpath。设置classpath会覆盖默认路径。

Happy Learning :) 快乐学习:)

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

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