一个关于ubuntu中java和环境变量的简单问题
我的 Ubuntu 10.10 已经安装了 java。我可以从任何文件夹执行java命令。我想这可能是因为我设置了 java 类路径。但JAVA_HOME和CLASSPATH都没有设置。
如果我查看 /etc/environment 内容,我可以看到 PATH 设置为 /usr/bin/ (以及其他)。由于 'which java
' 返回 /usr/bin/java,这就是我可以从任何地方执行 java 的原因吗?如果不是,为什么呢?
I have Ubuntu 10.10 with java already installed. I am able to execute java command from any folder. I supposed that could be because I had java Classpath setted. But neither JAVA_HOME nor CLASSPATH are setted.
If I look at /etc/environment content I can see that PATH is setted to /usr/bin/ (among others). As 'which java
' returns /usr/bin/java, is that the reason why I can execute java from anywhere? If not, why is it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以执行 java,因为该命令位于您的路径上。
将显示搜索哪些目录,以及以何种顺序查找特定程序。由于
/usr/bin
在您的路径上,因此当您输入java
时,它最终会查看/usr/bin/java
。请注意,在许多系统中,这是一个符号链接(指向另一个文件的文件),因此/usr/bin/java
通常指向/etc/alternatives/java
(这也是指向真正的可执行文件的符号链接)。环境变量
JAVA_HOME
发挥作用的地方是在检查JAVA_HOME
并对其执行操作而不是依赖于路径的工具和程序中。在大多数现代 Linux 系统中,替代子系统完成的工作取代了早期(问题较多)的JAVA_HOME
技术。也就是说,如果您遇到需要设置 JAVA_HOME 的工具,您可能还是想设置它。JAVA_HOME
没有那么流行的原因之一是,要访问JAVA_HOME
,您需要运行 shell,并且并不是每个人都希望将每个 Java 项都包装在一个外壳命令。You can execute java because the command is on your path.
will show you which directories are searched, in which order to find a particular program. Since
/usr/bin
is on your path, when you typejava
it will eventually look at/usr/bin/java
. Note that in many systems this is a symbolic link (a file that points to another file) so/usr/bin/java
often points to/etc/alternatives/java
(which is also a symbolic link that points to the real executable).Where the environmental variable
JAVA_HOME
comes into play is in tools and programs that check forJAVA_HOME
and act on it instead of relying on the path. In most modern Linux systems, the work done by the alternatives subsystem replaces the earlier (more problematic)JAVA_HOME
technique. That said, you might want to setJAVA_HOME
anyway, should you encounter a tool that demands it.One reason why
JAVA_HOME
is not as popular as it could be is that to accessJAVA_HOME
you need to run a shell, and not everyone wants to wrap every single Java item in a shell command.是的,如果 java 二进制文件(或其链接)位于路径上列出的文件夹中,那么您可以执行 java,而无需指定它的路径(例如
/usr/local/java/latest/bin/ java -jar x.jar
)JAVA_HOME和CLASSPATH与系统路径无关。
JAVA_HOME 允许其他软件(或脚本)知道在哪里寻找 java 安装。
CLASSPATH 告诉 java 在哪里查找类(编译 .java 文件产生的 .class 文件)。
Yes, if java binary (or a link to it) is on a folder that is listed on the path then you can execute java without specifying the path to it (for example
/usr/local/java/latest/bin/java -jar x.jar
)JAVA_HOME and CLASSPATH have nothing to do with system path.
JAVA_HOME allow other software (or scripts) to know where to look for java installation.
CLASSPATH tells java where to look for classes (.class files resulting of compiling .java files).