通过 eclipse 运行 ant 找不到环境变量,但是通过终端运行 ant 很好
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4970798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Running ant through eclipse it doesn't find environment variables, but running ant through terminal is fine
提问by czer
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?
当我通过 Eclipse 运行 ant 时,一些目标构建良好,但那些需要特定环境变量的目标,即我在 .profile 文件中设置的 SOME_SDK 将无法工作。即使我尝试回显 ${env.JAVA_HOME},Ant 通过 Eclipse 也会打印出该字符串。但是如果我通过终端运行 ant(即 ant sometarget),它会找到 JAVA_HOME 和 SOME_SDK 并将这些路径回显出来。我已经检查过 eclipse 它指向我的环境路径中的同一个 ant (/usr/share/java/ant-1.8.1)。有任何想法吗?
Thanks
谢谢
回答by Kevin Stembridge
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.
当您打开终端时,您的 .profile 中的环境变量将被加载。当您通过窗口系统启动 Eclipse 时,它不会在您的用户帐户下运行,因此您的 .profile 在运行时对 Eclipse 不可用。
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.
至少在我的 linux 机器上是这样,我猜在 Mac 上也是如此。我将 Eclipse 需要查看的环境变量放在 /etc/profile 文件中,该文件在系统启动时加载。
回答by Fabian Steeg
You can specify variables in your Ant run configuration (Run As> Ant Build...> Environmenttab).
您可以在 Ant 运行配置中指定变量(Run As> Ant Build...> Environment选项卡)。
回答by RickB
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.
我认为 Eclipse IDE 环境处理有点被破坏,至少在 *nix 环境中是这样。尽管与 Kevin 的情况类似,但我发现尽管我确实创建了一个单独的运行配置文件并且 - 成功地 - 运行测试/安装目标(这里是 maven 而不是 ant,但噪音可以忽略不计),并指定 JAVA_HOME 环境变量对于该运行配置文件,项目不会验证 POM 文件。
All I'm left with is two separate choices:
我只剩下两个不同的选择:
- use the /etc/profile approach, or
- Edit eclipse.ini file including the system property there.
- 使用 /etc/profile 方法,或
- 编辑 eclipse.ini 文件,包括那里的系统属性。
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 :)
我认为后者是一种更简洁的解决方法,因为它影响的配置文件毕竟只适用于有问题的应用程序。尽管如此,Eclipse 应该使用在用户路径中找到的变量,让我们轻松生成:)
回答by mannyglover
Add this line to ~/.bashrc
(or you can use another profile file):
将此行添加到~/.bashrc
(或者您可以使用另一个配置文件):
alias start-eclipse='open /Applications/eclipse/Eclipse.app'
(Don't forget to run source ~/.bashrc
)
(别忘了跑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.
如果你从命令行运行 start-eclipse,所有的环境变量都会被选中。这样,您只需在命令行和 Eclipse 环境中维护一组环境变量。
NOTE: Stolen from Chris Fegley's answer at Launch mac eclipse with environment variables set
注意:从 Chris Fegley's answer at Launch mac eclipse with environment variables set窃取
回答by Ben Carlson
What you need to do is remove the java.exe from c:\Windows\System32.
您需要做的是从 c:\Windows\System32 中删除 java.exe。
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.
原因是,在您的 %PATH% 环境变量中,它包含 c:\Windows\System32,其中包含一个 java.exe 文件。如果您从命令提示符运行 c:> java -version,您将看到您运行的版本与 jdk 版本不匹配。
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.
完成此操作后,您需要将: %JAVA_HOME%/bin 添加到 %PATH% 环境变量,以便在运行应用程序时从 jdk 执行 java.exe。在期望应用程序接受更改之前,请务必重新启动任何应用程序(包括命令提示符)。