简体   繁体   中英

modify environment variables from java

Have you succeses in JNI approach in How to run Unix shell script from Java code? ?? If yes, can you please provide me (or publish) sourcecodes for c and java?

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

Did you read the link to ProcessBuilder in the link you supplied in your question ?
If not look at ProcessBuilder docs with an example.

 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()).

You can get an example from her: http://www.java-tips.org/java-se-tips/java.util/from-runtime.exec-to-processbuilder.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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