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

时间:2022-09-29 23:22:09

When I read the PATH environment variable using Java, via:

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

System.getenv("PATH");

The result is less comprehensive then when using the shell, like:

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

> echo $PATH

Result using Java:

使用Java的结果:

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

Result using the shell:

结果使用shell:

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

Why is this? Is this some Java or some shell issue? How can I retrieve the full PATH information in Java? Do I need to take care about something more?

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

3 个解决方案

#1


They should be absolutely the same especially if you say the user has been confirmed the same in both situations ( Eclipse/Java JUnit function + the shell ). However, it could still happen if you load the PATH only in the shell and was not saved yet in the general System PATH. What OS are you using ?

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

#2


Check your .bashrc/.bashprofile file. It must have defined additional values you see in shell.

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

#3


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

为什么是这样?这是一些Java还是一些shell问题?

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.

两者都正常运作。这是因为两个进程(Java进程和shell)具有不同的环境,特别是PATH变量的不同设置。

You may ask why it is different. Most likely, the shell initialisation has modified its PATH. This is usually controlled by files such as /etc/profile and ~/.profile.

你可能会问为什么它不同。最有可能的是,shell初始化已经修改了它的PATH。这通常由/ etc / profile和〜/ .profile等文件控制。

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:

在shell中获得与Java程序相同的环境(从Eclipse运行)的最简单方法是从shell运行Eclipse,而不是通过Finder GUI启动它。您还可以通过以下命令将shell PATH设置复制到launchd设置:

launchctl setenv PATH $PATH

How can I retrieve the full PATH information in Java?

如何在Java中检索完整的PATH信息?

System.getenv("PATH") retrieves the full PATH for the Java process, which is inherited from (or otherwise set by) the parent process.

System.getenv(“PATH”)检索Java进程的完整PATH,该进程从父进程继承(或以其他方式设置)。

#1


They should be absolutely the same especially if you say the user has been confirmed the same in both situations ( Eclipse/Java JUnit function + the shell ). However, it could still happen if you load the PATH only in the shell and was not saved yet in the general System PATH. What OS are you using ?

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

#2


Check your .bashrc/.bashprofile file. It must have defined additional values you see in shell.

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

#3


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

为什么是这样?这是一些Java还是一些shell问题?

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.

两者都正常运作。这是因为两个进程(Java进程和shell)具有不同的环境,特别是PATH变量的不同设置。

You may ask why it is different. Most likely, the shell initialisation has modified its PATH. This is usually controlled by files such as /etc/profile and ~/.profile.

你可能会问为什么它不同。最有可能的是,shell初始化已经修改了它的PATH。这通常由/ etc / profile和〜/ .profile等文件控制。

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:

在shell中获得与Java程序相同的环境(从Eclipse运行)的最简单方法是从shell运行Eclipse,而不是通过Finder GUI启动它。您还可以通过以下命令将shell PATH设置复制到launchd设置:

launchctl setenv PATH $PATH

How can I retrieve the full PATH information in Java?

如何在Java中检索完整的PATH信息?

System.getenv("PATH") retrieves the full PATH for the Java process, which is inherited from (or otherwise set by) the parent process.

System.getenv(“PATH”)检索Java进程的完整PATH,该进程从父进程继承(或以其他方式设置)。