简体   繁体   English

Java类路径 - Linux

[英]Java classpath - Linux

I am trying to understand how classpath really works. 我试图了解classpath如何真正起作用。 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 . /etc/environment I am running Ubuntu by the way. 我顺便运行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. 问题是如果我更改CLASSPATH然后我执行: source /etc/environment ,则不会应用新的CLASSPATH。 It is applied if and only if I restart the system. 当且仅当我重新启动系统时才应用它。 For example if I delete the 例如,如果我删除

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 . 行,然后我做源/ etc / environment,我终于echo $CLASSPATH ,我得到的是/home/foo:/home/foo/Java_code/my_code I think I should get an empty line, shouldn't I? 我想我应该得到一个空行,不是吗?

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. 感谢您的时间。

I think you should not put any paths that a local to your home directory in a system wide file. 我认为你不应该把你的主目录的本地路径放在系统范围的文件中。 I would leave /etc/environment well alone, unless you provide some changes, that are necessary or beneficial to all users. 除非你提供一些必要或有益于所有用户的更改,否则我会单独留下/etc/environment Put any changes to the CLASSPATH in your .bashrc in your home directory. 在主目录的.bashrc中对CLASSPATH进行任何更改。

  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都会立即进行设置。

 export CLASSPATH=""

or better 或更好

 unset CLASSPATH

will delete an existing Classpath. 将删除现有的Classpath。 There are multiple locations where you can set or unset the classpath - a missing entry will not unset it. 您可以在多个位置设置或取消设置类路径 - 缺少的条目不会取消设置。

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. line然后source ...它不会删除CLASSPATH,因为它已经设置好了。 It doesn't clear the variables which have already been defined. 它不清楚已经定义的变量。

The /etc/environment file is not a normal shell script; /etc/environment文件不是普通的shell脚本; it is not executed as a shell script when you boot or login into your system. 启动或登录系统时,它不会作为shell脚本执行。 So running it with source /etc/environment does not the same as when you normally boot your system. 因此,使用source /etc/environment运行它与正常启动系统时不一样。

See: Ubuntu environment variables 请参阅: Ubuntu环境变量

/etc/environment - This file is specifically meant for system-wide environment variable settings. /etc/environment - 此文件专门用于系统范围的环境变量设置。 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. 具体来说,此文件存储系统范围的区域设置和路径设置。

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

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