简体   繁体   English

如何在mac上设置java环境变量

[英]How to set java environment variable on mac

How do I set a java environment variable that can be read with System.getenv() on Mac OS X Lion? 如何在Mac OS X Lion上设置可以使用System.getenv()读取的java环境变量? I developing in Eclipse. 我在Eclipse中开发。

I used this command in the terminal export VAR_NAME=Value but System.getEnv(VAR_NAME) returns null . 我在终端export VAR_NAME=Value使用了此命令,但System.getEnv(VAR_NAME)返回null

I tried java -DVAR_NAME=Value but nothing is executed. 我试过java -DVAR_NAME=Value但没有执行任何操作。 I only get the java arguments help. 我只获得java参数帮助。

I created in my current project folder the file .bash_profile with the content: export VAR_NAME=Value . 我在当前项目文件夹中创建了文件.bash_profile ,其内容为: export VAR_NAME=Value It's not working. 它不起作用。

I also created the folder .MacOSX with a single file inside environment.plist and in this file I wrote the property and its value using XCode. 我还创建该文件夹.MacOSX里面的单个文件environment.plist并在此文件中我写的性能和使用的XCode它的价值。 Nothing is working. 什么都行不通。

How do I do it? 我该怎么做?

It is not clear what you are actually doing here. 目前尚不清楚你在这里做了什么。 If you are trying to set an environment variable for a Java command you are launching from Eclipse , then the simple solution is to set it via the Eclipse command launcher configuration. 如果您正在尝试为从Eclipse启动的Java命令设置环境变量,那么简单的解决方案是通过Eclipse命令启动器配置来设置它。 Another alternative is to: 另一种选择是:

  1. exit Eclipse, 退出Eclipse,
  2. run export VAR_NAME=Value in a command shell instance, and run export VAR_NAME=Value命令shell实例中的值,和
  3. launch Eclipse from that same shell instance . 从同一个shell实例启动Eclipse。

Re the things you tried. 重新尝试的东西。

I used this command in the terminal export VAR_NAME=Value but System.getEnv(VAR_NAME) returns null . 我在终端export VAR_NAME=Value使用了此命令,但System.getEnv(VAR_NAME)返回null

If you run the export command from a command prompt, and then immediately launch the command from the same command prompt, that should work. 如果从命令提示符运行export命令,然后立即从同一命令提示符启动命令,那应该可以。 The export command is telling the shell instance used by the command prompt to add VAR_NAME to this list of environment variables it exports to child processes that started after running the export command. export命令告诉命令提示符使用的shell实例将VAR_NAME添加到它导出到运行export命令后启动的子进程的环境变量列表中。 Child processes that have all ready been started won't see the changes . 已经准备就绪的子进程将看不到更改

I strongly suspect that you ran the export after you launched Eclipse ... or that you didn't start Eclipse from that shell instance. 我强烈怀疑你在启动Eclipse 之后运行export ...或者你没有从那个shell实例启动Eclipse。 If either of those two is true, the export command wouldn't affect Eclipse's environment variables which it (by default) passes on to any Java program you launch from Eclipse. 如果这两个中的任何一个都为真,那么export命令不会影响Eclipse的环境变量,它(默认情况下)传递给从Eclipse启动的任何Java程序。

I tried java -DVAR_NAME=Value but nothing is executed. 我试过java -DVAR_NAME=Value但没有执行任何操作。 I only get the java arguments help. 我只获得java参数帮助。

That fails for a couple of reasons: 这失败了几个原因:

  • the -DVAR_NAME=... is setting a system property not an environment variable, and -DVAR_NAME=...正在设置系统属性而不是环境变量,和
  • you haven't told java the name of the class that you want to start! 你没有告诉java你想要开始的类的名称!

I created in my current project folder the file .bash_profile with the content: export VAR_NAME=Value 我在当前项目文件夹中创建了文件.bash_profile ,其内容为: export VAR_NAME=Value

That only affects new shell instances that are launched after you created the file. 这仅影响创建文件启动的新shell实例。 It doesn't affect the existing one. 它不会影响现有的。

I also created the folder .MacOSX with a single file inside environment.plist and in this file I wrote the property and its value using XCode. 我还用environment.plist中的单个文件创建了文件夹.MacOSX,在这个文件中我使用XCode编写了属性及其值。

I've no idea what that would do. 我不知道会做什么。 Where did you create that folder? 你在哪里创建该文件夹?

1.open the Terminal 1.打开终端

2.export VAR_NAME=Value 2.export VAR_NAME =值

3.open /Applications/Eclipse.app 3.open /Applications/Eclipse.app

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM