Running ant through eclipse it doesn't find en

2020-07-10 11:32发布

问题:

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

回答1:

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.



回答2:

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



回答3:

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 :)



回答4:

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.



回答5:

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