7

When i run ant through Eclipse some targets build fine, but ones that need a specific environment variable i.e. SOME_SDK that i set in my .profile file won't work. Even if i try to echo out ${env.JAVA_HOME}, Ant through eclipse will just print out that string. But if i run ant through the terminal (i.e. ant sometarget), it will find JAVA_HOME and SOME_SDK and echo those paths out. Ive checked eclipse that it's pointing to the same ant that is in my environment path (/usr/share/java/ant-1.8.1). Any ideas?

Thanks

5 Answers 5

19

When you open a terminal, the environment variables in your .profile get loaded. When you start Eclipse via the windowing system it is not being run under your user account, so your .profile is not available to Eclipse at runtime.

At least that's the case on my linux machine and I'm guessing its the same on a Mac. I put the environment variables that Eclipse needs to see in the /etc/profile file, which gets loaded at system startup.

3
  • 3
    On the Mac, that would be ~/.MacOSX/environment.plist, see also developer.apple.com/qa/qa2001/qa1067.html Feb 11, 2011 at 18:14
  • Great tip about windowing system reading the profile on start - couldn't figure out why things didn't work for me after change.
    – SyBer
    Aug 24, 2011 at 11:37
  • 1
    If you have set the env variables after starting the eclipse, then restart the eclipse. Eclipse will load new env profile just as you close and reopen cmd in windows.
    – sura2k
    Jun 29, 2017 at 11:30
12

You can specify variables in your Ant run configuration (Run As > Ant Build... > Environment tab).

2
  • Thanks I actually saw that and was able to get it to work. But do you know why eclipse doesn't pick up environment variables in OSX? Any way to get eclipse to see them?
    – czer
    Feb 11, 2011 at 16:28
  • Apps launched from the GUI, such as eclipse, inherit their variables from launchctl. You can set the launchctl environment by running, as root, "launchctl setenv <VAR>" (See stackoverflow.com/a/3756686/218823) Feb 25, 2012 at 8:52
1

I'm thinking that the Eclipse IDE environment handling is a bit spoiled, at least in *nix environments. Although similar to Kevin's case, what I'm finding is that although I do create a separate run profile and -successfully - run the test/install goals (maven instead of ant here, but that's negligible noise), and specify the JAVA_HOME environment variable for that run profile, the project does not validate the POM file.

All I'm left with is two separate choices:

  1. use the /etc/profile approach, or
  2. Edit eclipse.ini file including the system property there.

I think the latter is a cleaner workaround since it affects a configuration file that is only for the problematic application after all. Nevertheless, Eclipse should use the variables found in the user's path and leave us at ease to produce :)

2
  • Could you elaborate on how to modify eclipse.ini file to "include the system property there"? Jul 12, 2018 at 20:09
  • 1
    Sure. The issue back then was to make the intended value of JAVA_HOME env variable visible in Eclipse runtime (for example, when running the application from within the IDE). The 2nd method I mention there is basically setting the JAVA_HOME value directly in the eclipse.ini file (using the syntax you would use anywhere else for setting environment variables). Hope this helps.
    – RickB
    Aug 21, 2018 at 21:05
0

What you need to do is remove the java.exe from c:\Windows\System32.

The reason is, in your %PATH% environment variable, it includes c:\Windows\System32, which has a java.exe file. If you run c:> java -version from a command prompt, you'll see that the version you're running does not match the jdk version.

Once you've done this, you will need to add: %JAVA_HOME%/bin to the %PATH% environment variable, so you can execute the java.exe from the jdk when running apps. Be sure to restart any application (including the command prompt) before expecting it to pick up the change.

0

Add this line to ~/.bashrc (or you can use another profile file):

alias start-eclipse='open /Applications/eclipse/Eclipse.app'

(Don't forget to run source ~/.bashrc)

If you run start-eclipse from the command line, all environment variables will be picked up. This way, you only need to maintain a single set of environment variables across both command-line and eclipse environments.

NOTE: Stolen from Chris Fegley's answer at Launch mac eclipse with environment variables set

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.