0

If I run echo $PATH, I get:

/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/go/bin

Amongst others.

If I make a simple application that does the same:

public class Test {
    public static void main(final String[] args) {
        System.out.println(System.getenv().get("PATH"));
    }
}

When I run this from the console (java -jar test.jar):

/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/go/bin

And yet when I run that same application in Eclipse:

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

Why isn't Eclipse picking up all the environment variables?

Jakg
  • 922
  • 12
  • 39
  • Is the java process behind Eclipse running as the same user? – g00se Aug 09 '23 at 09:42
  • @g00se yes, both are running under my user account (verified in `Activity Monitor` using PID). – Jakg Aug 09 '23 at 09:46
  • 1
    Applications like Eclipse don't get the path set up for Terminal. See [here](https://stackoverflow.com/q/135688/2670892) for lots of discussion – greg-449 Aug 09 '23 at 11:47
  • Does this answer your question? [Environment variables in Eclipse](https://stackoverflow.com/questions/7048216/environment-variables-in-eclipse) – aled Aug 09 '23 at 11:48
  • @aled no - I don't want to manually copy my `$PATH` to my IDE, and that's not portable. – Jakg Aug 09 '23 at 11:52
  • By default, applications are launched from within Eclipse without any environment variables set (for reasons of portability). [If you want to pass or local environment variable `PATH` to your application, in your launch configuration in the tab _Environment_ set `PATH` to `${env_var:PATH}`](https://stackoverflow.com/a/55698254/6505250). – howlger Aug 10 '23 at 11:18

1 Answers1

0

Coming from Linux. I would try:

Understanding environment variables

If you run java from your console it executes in the context of the current console session.

  1. If you change $PATH in your current session it doesn´t mean that the value is available system wide. e.g. when you start a different session (eclipse) the content of the variable can differ.

  2. Different shells behave differently depending on where you have defined your variables. e.g ~/.profile is only loaded when running zsh in sh compatibility mode, otherwise it reads .zprofile, as well as .zshrc for interactive and .zlogin for login shells.

Eclipse might have problems with env vars on the user level. See: Eclipse and Bash See: Zsh invocation order

Persist $PATH

This is typically done by initializing the variable during logging via a config file on user or system level. For macos see Setting PATH environment variable in OSX permanently .

Logout and login again

After adding the variable to the system configuration you have to logout and login again to make its contents available through all sessions.

Verify user

If the $PATH is persistent it should be the same in all console sessions. Print System.getProperty("user.name"); to verify that the expected user is calling java -jar. [1]

[1] Howto print all environment vars and properties

jschnasse
  • 8,526
  • 6
  • 32
  • 72
  • I haven't changed the `$PATH` in this session, and a reboot hasn't helped. – Jakg Aug 09 '23 at 10:06
  • Where do you set the $PATH? If you use `.bashrc` or some other user specific profile config it might be ignored by eclipse, like stated here: See: https://stackoverflow.com/a/7048245/1485527 . – jschnasse Aug 09 '23 at 10:14
  • Also see: https://superuser.com/questions/457889/order-of-execution-and-purpose-of-profile-vs-zshrc – jschnasse Aug 09 '23 at 10:17