启动 mac eclipse 并设置环境变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/829749/
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
Launch mac eclipse with environment variables set
提问by sakra
My company provides an eclipse based development environment which needs some environment variables setting up for the underlying toolchain so multiple versions can be installed concurrently and not take over the system.
我公司提供了一个基于 Eclipse 的开发环境,它需要为底层工具链设置一些环境变量,以便可以同时安装多个版本而不接管系统。
I want to provide an icon in finder or the dock which sets these then launches eclipse so customers cannot accidentally launch eclipse without the environment being set. This is what I have tried so far:
我想在 finder 或 Dock 中提供一个图标,设置这些图标然后启动 eclipse,这样客户就不会在没有设置环境的情况下意外启动 eclipse。这是我迄今为止尝试过的:
Setting environment in Info.plist for eclipse:
This should be a nice way to do it but I cannot make it add to the existing path (like export PATH=/myapp/bin:$PATH).
bash script wrapping eclipse:
I created a bash script called eclipse.command to set the environment then launch eclipse. This opens a terminal window as well as the eclipse icon and allows people to "Keep on dock" for the bare eclipse. I cannot put eclipse.command on the dock as it is not an application.
Applescript wrapping eclipse.command:
An Applescript wrapper around eclipse.command makes it look like an app and prevents the terminal window appearing. Unfortunately I now get a dock icon for the applescript and one for eclipse so can still keep the bare eclipse on the dock.
在 Info.plist 中为 Eclipse 设置环境:
这应该是一个很好的方法,但我无法将其添加到现有路径中(例如 export PATH=/myapp/bin:$PATH)。
包装 eclipse 的 bash 脚本:
我创建了一个名为 eclipse.command 的 bash 脚本来设置环境然后启动 eclipse。这将打开一个终端窗口以及日食图标,并允许人们为裸日食“保持停靠”。我无法将 eclipse.command 放在扩展坞上,因为它不是应用程序。
Applescript 包装 eclipse.command:
eclipse.command 周围的 Applescript 包装器使它看起来像一个应用程序,并防止终端窗口出现。不幸的是,我现在得到了一个用于 Applescript 的停靠栏图标和一个用于 eclipse 的停靠栏图标,因此仍然可以将裸露的日食保留在停靠栏上。
Any suggestions? Am I going about this in completely the wrong way?
有什么建议?我是否以完全错误的方式处理这个问题?
回答by sakra
There is an alternate solution which involves replacing the executable that is run by MacOS X when the user launches the Eclipse application with a shell wrapper that sets up the environment.
有一种替代解决方案,它涉及在用户启动 Eclipse 应用程序时替换由 MacOS X 运行的可执行文件,使用设置环境的 shell 包装器。
Create an empty text file called "eclipse.sh" in the Eclipse application bundle directory /Applications/eclipse/Eclipse.app/Contents/MacOS.
在 Eclipse 应用程序包目录 /Applications/eclipse/Eclipse.app/Contents/MacOS 中创建一个名为“eclipse.sh”的空文本文件。
Open the eclipse.sh in a text editor an enter the following contents:
在文本编辑器中打开 eclipse.sh 并输入以下内容:
#!/bin/sh
export ENV_VAR1=value
export ENV_VAR2=value
logger "`dirname \"chmod +x /Applications/eclipse/Eclipse.app/Contents/MacOS/eclipse.sh
\"`/eclipse"
exec "`dirname \"/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -v -f /Applications/eclipse/Eclipse.app
\"`/eclipse" $@
In the example ENV_VAR1 and ENV_VAR2 are the environment variables being set up. These variables will be visible to processes launched from within Eclipse. The logger command will just log the path of the eclipse executable to the system.log as a debugging aid.
在示例中 ENV_VAR1 和 ENV_VAR2 是正在设置的环境变量。这些变量对从 Eclipse 内启动的进程可见。logger 命令只会将 eclipse 可执行文件的路径记录到 system.log 作为调试帮助。
In the Terminal set the executable flag of the shell script eclipse.sh, i.e.:
在终端中设置 shell 脚本 eclipse.sh 的可执行标志,即:
source ~/.bash_profile && /Applications/eclipse/Eclipse.app/Contents/MacOS/eclipse
Open the Eclipse.app Info.plist and change the value for the key CFBundleExecutable from eclipse to eclipse.sh.
打开 Eclipse.app Info.plist 并将键 CFBundleExecutable 的值从 eclipse 更改为 eclipse.sh。
MacOS X does not automatically detect that the Eclipse.app's Info.plist has changed. Therefore you need to force update the LaunchService database in the Terminal by using the lsregister command:
MacOS X 不会自动检测到 Eclipse.app 的 Info.plist 已更改。因此,您需要使用 lsregister 命令强制更新终端中的 LaunchService 数据库:
source ~/.bash_profile && /Applications/eclipse/Eclipse.app/Contents/MacOS/eclipse
The next time you launch Eclipse.app from the Dock or from the Finder the environment variables should be set.
下次从 Dock 或 Finder 启动 Eclipse.app 时,应设置环境变量。
回答by Chris Fregly
I created the following:
我创建了以下内容:
alias start-eclipse='open /Applications/eclipse/Eclipse.app'
别名 start-eclipse='open /Applications/eclipse/Eclipse.app'
If you run start-eclipse from the command line, all env vars will be picked up. This way, you only need to maintain a single set of env vars across both command-line and eclipse environments.
如果你从命令行运行 start-eclipse,所有的环境变量都会被选中。这样,您只需要在命令行和 Eclipse 环境中维护一组 env vars。
回答by mouviciel
Take a look at a related question: Environment variables in Mac OS X.
看看一个相关的问题:Mac OS X 中的环境变量。
Basically, this involves the creation of a ~/.MacOSX/environment.plist
file.
基本上,这涉及创建~/.MacOSX/environment.plist
文件。
Log out and Log in for the environment.plist to get picked up by .App's
注销并登录 environment.plist 以被 .App 获取
回答by ayrton04
This worked perfectly in OS X Yosemite:
这在 OS X Yosemite 中完美运行:
- Open /Applications/Automator.
- When the drop-down appears asking you what kind of document you want to create, choose "Application."
- In the second-from-the-left list, double-click "Run Shell Script."
In the right side delete the "cat" that gets put there automatically, and replace it with this:
#!/bin/bash source ~/.bash_profile logger "`dirname \"
\"`/eclipse" exec "`dirname \"#!/usr/bin/env bash -l
\"`/eclipse" $@#!/bin/bash source ~/.bash_profile
- 打开/应用程序/Automator。
- 当出现下拉列表询问您要创建哪种文档时,选择“应用程序”。
- 在左起第二个列表中,双击“运行 Shell 脚本”。
在右侧删除自动放置在那里的“猫”,并将其替换为:
exec "`dirname \"
\"`/eclipse" $@#!/bin/bash source /home/user/.environment_variables /home/user/eclipse_cpp/eclipse -Duser.name="My Name"
Now go to File->Save, and save the application to your Applications directory. I named it "Eclipse" with a capital 'E' so as not to conflict with the "eclipse" directory I already had. For good measure, you can even give it the Eclipse icon by selecting the real eclipse app, pressing command-i, selecting the icon, pressing command-c, then selecting the automator "Eclipse" app, pressing command-i, selecting the icon, and pressing command-v.
现在转到 File->Save,并将应用程序保存到您的 Applications 目录。我用大写的“E”将其命名为“Eclipse”,以免与我已有的“eclipse”目录冲突。为了更好的衡量,您甚至可以通过选择真正的 Eclipse 应用程序,按 command-i,选择图标,按 command-c,然后选择自动机“Eclipse”应用程序,按 command-i,选择图标来给它 Eclipse 图标,然后按 command-v。
Now you can open the app, or even drag it to your dock. Note that if you start it, the "real" eclipse will still show up in your dock as a separate icon, but you can't have everything. :)
现在您可以打开该应用程序,甚至可以将其拖到 Dock 中。请注意,如果您启动它,“真正的”日食仍将作为单独的图标显示在您的 Dock 中,但您无法拥有一切。:)
回答by Andy Carroll
sakra's answer above is awesome, except is doesn't automatically inherit your existing bash environment. To ensure eclipse.sh picks up your existing bash environment, modify eclipse.sh to use bash instead of sh and add a line to source your existing ~/.bash_profile thus:
sakra 上面的回答很棒,只是它不会自动继承您现有的 bash 环境。为确保 eclipse.sh 获取您现有的 bash 环境,请修改 eclipse.sh 以使用 bash 而不是 sh 并添加一行以获取现有的 ~/.bash_profile ,因此:
#!/bin/bash
source /home/user/.environment_variables
/home/user/eclipse_cpp/eclipse -Duser.name="My Name"
回答by Ahmed Hammad
None of the above worked for me. you have to set Eclipse -> Preferences -> Terminal -> Arguments set to --login That will instruct Eclipse to login with your account just after opening Terminal.
以上都不适合我。您必须将 Eclipse -> Preferences -> Terminal -> Arguments 设置为 --login 这将指示 Eclipse 在打开终端后使用您的帐户登录。
See screenshot:
看截图:
Reference: https://marketplace.eclipse.org/comment/4259#comment-4259
参考:https: //marketplace.eclipse.org/comment/4259#comment-4259
回答by Philipp Kunz
As pointed out in https://github.com/atom/atom/issues/7045, the environment variables can be loaded automatically, without explicit source ~/.bash_profile
by using
正如https://github.com/atom/atom/issues/7045所指出的,环境变量可以自动加载,无需source ~/.bash_profile
使用显式
export COCOS_ROOT=/home/user/Projects/edukoala
export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64/
instead of
代替
source /home/user/.environment_variables
after that, in both cases, follows
之后,在这两种情况下,遵循
##代码##It works great for me, thanks for all previous work.
它对我很有用,感谢以前的所有工作。
回答by Dawid Drozd
Link to Eclipse doesn't use the path set in .bashrc
- Create simple script
- 创建简单脚本
##代码##
##代码##
2. Next put your all system variables in file /home/user/.environment_variables (any file you want)
2.接下来将所有系统变量放在文件/home/user/.environment_variables(您想要的任何文件)中
My looks like:
我的样子:
##代码##3. Now you can delete your variables in .bashrc and put line
3. 现在您可以删除 .bashrc 中的变量并放置行
##代码##Everything works fine :)
一切正常:)