简体   繁体   English

从Java修改环境变量

[英]modify environment variables from java

Have you succeses in JNI approach in How to run Unix shell script from Java code? 如何通过Java代码运行Unix shell脚本中的JNI方法成功了 ?? ?? If yes, can you please provide me (or publish) sourcecodes for c and java? 如果是,请您提供(或发布)C和Java的源代码吗?

如果您知道系统调用OS库来设置环境变量,那么我建议使用JNA-它提供了本机访问,而无需编写JNI库。

Did you read the link to ProcessBuilder in the link you supplied in your question ? 您是否已阅读问题中提供的链接中的ProcessBuilder链接?
If not look at ProcessBuilder docs with an example. 如果没有,请看带有示例的ProcessBuilder文档

 ProcessBuilder pb = new ProcessBuilder("myCommand", "myArg1", "myArg2");
 Map<String, String> env = pb.environment();
 env.put("VAR1", "myValue");
 env.remove("OTHERVAR");
 env.put("VAR2", env.get("VAR1") + "suffix");
 pb.directory("myDir");
 Process p = pb.start();

In the above example you can easily modify the environment as you can see. 在上面的示例中,您可以轻松地修改环境,如您所见。

Once you have the Process you can get to all the streams you need from there (getOutputStream(), getInputStream(), getErrorStream()). 一旦有了Process,就可以从那里获得所需的所有流(getOutputStream(),getInputStream(),getErrorStream())。

You can get an example from her: http://www.java-tips.org/java-se-tips/java.util/from-runtime.exec-to-processbuilder.html 您可以从她那里得到一个例子: http : //www.java-tips.org/java-se-tips/java.util/from-runtime.exec-to-processbuilder.html

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

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