2

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

System.getenv("PATH");

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

> echo $PATH

Result using Java:

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

Result using the 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?

12
  • How do you run the program? Do you run it with the same user as the echo on the shell? Or ist it a cron job? Commented May 8, 2015 at 8:12
  • Did you run the java code and the command in shell, as the same user? Commented May 8, 2015 at 8:18
  • Yes, both are run using the same user. I even confirmed this in Java by running whoami using the ProcessBuilder. No cron job. The Java code runs as JUnit test case in Eclipse. Commented May 8, 2015 at 8:24
  • check using System.getProperty("user.name") Commented May 8, 2015 at 8:32
  • It sounds like it's an Eclipse issue rather than a shell issue. Might be worth tagging this question with "eclipse". Also you could try running the junit test from the command line. Commented May 8, 2015 at 8:33

3 Answers 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 ?

Sign up to request clarification or add additional context in comments.

2 Comments

I'm using MacOS X and I'm just trying to read the PATH variable, without any modifications applied on the shell.
Yes, Mac OS X works a bit different. You need to set up the PATH in ~/.MacOSX/environment.plist so that Eclipse can read it too
0

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

Comments

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 /etc/profile and ~/.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:

launchctl setenv PATH $PATH

How can I retrieve the full PATH information in Java?

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.