使用Java读取PATH变量(在Mac OS X上的Eclipse中)与shell不同

时间:2015-05-08 08:09:39

标签: java eclipse macos shell environment-variables

当我使用Java读取PATH环境变量时,通过:

System.getenv("PATH");

使用shell时结果不太全面,例如:

> echo $PATH

使用Java的结果:

/usr/bin:/bin:/usr/sbin:/sbin

使用shell的结果:

/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/usr/texbin

这是为什么?这是一些Java还是一些shell问题?如何在Java中检索完整的PATH信息?我需要更多地关注更多事情吗?

3 个答案:

答案 0 :(得分:1)

它们应该完全相同,特别是如果你说用户在两种情况下都被确认相同(Eclipse / Java JUnit函数+ shell)。但是,如果仅在shell中加载PATH并且尚未保存在常规系统路径中,则仍可能发生这种情况。您使用的操作系统是什么?

答案 1 :(得分:0)

检查.bashrc / .bashprofile文件。 它必须定义您在shell中看到的其他值。

答案 2 :(得分:0)

Why is this? Is this some Java or some shell issue?

No. Both are functioning correctly. It's because the two processes (the Java process, and the shell) have a different environment and in particular a different setting for the PATH variable.

You may ask why it is different. Most likely, the shell initialisation has modified its PATH. This is usually controlled by files such as LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so and httpd: Syntax error on line 158 of /opt/lampp/etc/httpd.conf: Cannot load /usr/lib/apache2/modules/mod_wsgi.so into server: /usr/lib/x86_64-linux-gnu/libpython3.4m.so.1.0: undefined symbol: XML_SetHashSalt .

The easiest way to get the same environment in your Java program (which you run from Eclipse) as in your shell is to run Eclipse from the shell instead of launching it via the Finder GUI. You also have the option of copying the shell PATH setting to the launchd settings, via the command:

.row6 {
  background: none;
  width: 100%;
  height: 130px;
  border: 0px salmon dotted;
  font: bold 1.7vw arial, sans-serif;
  margin: 20px auto;
}

.row6 > .bgImage {  
  width: 100%;
  height: 100%;
  margin: 0 auto;
  background: url(http://i.stack.imgur.com/amwBH.png) no-repeat;
  background-size: 100%
}

#dd7 {
  margin-left:44.7%;
  margin-top: 1.2%;
  float: left;
}

How can I retrieve the full PATH information in Java?

/etc/profile retrieves the full PATH for the Java process, which is inherited from (or otherwise set by) the parent process.