简体   繁体   English

在OS X El Capitan中设置环境变量,以便Java可以读取?

[英]Set environment variable in OS X El Capitan so can be read by java?

I need to set an environment variable in OS X El Capitan so it can be read by java with System.getenv(myVar) . 我需要在OS X El Capitan中设置一个环境变量,以便Java可以使用System.getenv(myVar)读取它。 How can I do it? 我该怎么做? Of course the variable must be only readable by the user who created it (and the admin probably). 当然,该变量只能由创建它的用户(可能还有管理员)可读。

The default shell on OS X is bash . OS X上的默认shell是bash

export FRED="Hello"

and then you can use something like 然后您可以使用类似

String fred = System.getenv("FRED");
System.out.println(fred);

I know why it happened. 我知道为什么会这样。 There's a bug in Oracle java where SecurityManager sm = getSecurityManager(); Oracle Java中存在一个错误,其中SecurityManager sm = getSecurityManager(); returns always a null string (sm = null). 始终返回一个空字符串(sm = null)。 After updating java 8.0.51 to 8.0.65 it worked perfectly. 将Java 8.0.51更新为8.0.65之后,它可以完美运行。 The same happens on some version of Oracle java for linux. 在某些版本的Oracle Java for Linux中也会发生相同的情况。

The affected routine is this: public static String getenv(String name) 受影响的例程是: public static String getenv(String name)

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

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