Java 类路径 - Linux

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9329650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:38:26  来源:igfitidea点击:

Java classpath - Linux

javalinuxubuntuclasspath

提问by tomatoeggs

I am trying to understand how classpath really works. After searching around the web this is where I have reached so far:

我试图了解类路径是如何真正工作的。在网上搜索之后,这是我到目前为止到达的地方:

I have added

我已经添加了

export CLASSPATH="/home/foo:/home/foo/Java_code/my_code"

at /etc/environment. I am running Ubuntu by the way.

/etc/environment。顺便说一下,我正在运行 Ubuntu。

Java finds the path and compiles without problem.

Java 找到路径并编译没有问题。

The problem is that if I change the CLASSPATH and then I do: source /etc/environment, the new CLASSPATH is not applied. It is applied if and only if I restart the system. For example if I delete the

问题是,如果我更改了 CLASSPATH 然后我做了:source /etc/environment,则不会应用新的 CLASSPATH。当且仅当我重新启动系统时才应用它。例如,如果我删除

export CLASSPATH="/home/foo:/home/foo/Java_code/my_code"

line, then I do source /etc/environment and I finally do echo $CLASSPATH, what I get is /home/foo:/home/foo/Java_code/my_code. I think I should get an empty line, shouldn't I?

行,然后我做 source /etc/environment ,我终于做了echo $CLASSPATH,我得到的是/home/foo:/home/foo/Java_code/my_code. 我想我应该得到一个空行,不是吗?

Is there a way to apply the changes in PATH or CLASSPATH variables immediately without having to restart the system?

有没有办法立即应用 PATH 或 CLASSPATH 变量中的更改而无需重新启动系统?

It might help you know that the /etc/environment file originally contained only the following line:

它可能有助于您了解 /etc/environment 文件最初仅包含以下行:

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games"

Thank you for your time.

感谢您的时间。

采纳答案by Mithrandir

I think you should not put any paths that a local to your home directory in a system wide file. I would leave /etc/environmentwell alone, unless you provide some changes, that are necessary or beneficial to all users. Put any changes to the CLASSPATH in your .bashrcin your home directory.

我认为您不应该在系统范围的文件中放置任何您的主目录的本地路径。/etc/environment除非您提供一些对所有用户都必要或有益的更改,否则我将不理会。将任何对 CLASSPATH 的更改.bashrc放在您的主目录中。

  CLASSPATH=$CLASSPATH:/home/foo:/home/foo/Java_code/my_code
  export CLASSPATH

This way you can source it and any newly started bash will have the settings right at once.

通过这种方式,您可以获取它,并且任何新启动的 bash 都将立即拥有正确的设置。

回答by Sapan Diwakar

When you remove

当你删除

export CLASSPATH="/home/foo:/home/foo/Java_code/my_code"

line and then source ...it wouldn't remove the CLASSPATH because it has already been set. It doesn't clear the variables which have already been defined.

行,然后source ...它不会删除 CLASSPATH,因为它已经被设置。它不会清除已经定义的变量。

回答by Jesper

The /etc/environmentfile is not a normal shell script; it is not executed as a shell script when you boot or login into your system. So running it with source /etc/environmentdoes not the same as when you normally boot your system.

/etc/environment文件不是普通的 shell 脚本;当您启动或登录系统时,它不会作为 shell 脚本执行。因此,运行它与source /etc/environment正常启动系统时的运行方式不同。

See: Ubuntu environment variables

请参阅:Ubuntu 环境变量

/etc/environment- This file is specifically meant for system-wide environment variable settings. It is not a script file, but rather consists of assignment expressions, one per line. Specifically, this file stores the system-wide locale and path settings.

/etc/environment- 此文件专门用于系统范围的环境变量设置。它不是一个脚本文件,而是由赋值表达式组成,每行一个。具体来说,此文件存储系统范围的区域设置和路径设置。

回答by user unknown

 export CLASSPATH=""

or better

或更好

 unset CLASSPATH

will delete an existing Classpath. There are multiple locations where you can set or unset the classpath - a missing entry will not unset it.

将删除现有的类路径。您可以在多个位置设置或取消设置类路径 - 丢失的条目不会取消设置。