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?
Asked
Active
Viewed 488 times
0
-
I don't see a JNI approach in the linked question. Can you elaborate? – Michael Myers Jun 04 '10 at 17:25
-
sorry bad link in question! http://stackoverflow.com/questions/1945713/set-windows-system-variables-with-java – JIri Vanek Jun 04 '10 at 17:30
2 Answers
0
If you know the systems calls to OS libraries for setting environment variables, then I recommend JNA - it provides native access without the hassle of writing a JNI library.

mdma
- 56,943
- 12
- 94
- 128
-
im afraid i dont....I wanted to use http://www.opengroup.org/onlinepubs/009695399/functions/putenv.html – JIri Vanek Jun 04 '10 at 17:34
0
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

Romain Hippeau
- 24,113
- 5
- 60
- 79
-
as explained eg here, it will never work. I need to chhange PATH of currently running process:(( – JIri Vanek Jun 04 '10 at 17:49
-
here... http://stackoverflow.com/questions/580085/is-it-possible-to-set-an-environment-variable-at-runtime-from-java – JIri Vanek Jun 04 '10 at 17:58
-
1@Jlri Vanek and the post you mentioned says that you can modify the environment of a child process before it has started, but you cannot change the environment of the current process. – Romain Hippeau Jun 04 '10 at 18:15
-
and this http://stackoverflow.com/questions/1945713/set-windows-system-variables-with-java says that it is possibe. The mentioned code judst doesnt work;) – JIri Vanek Jun 05 '10 at 20:18
-
@Jlri Vanek - You cannot change it for the current process, only for child processes. – Romain Hippeau Jun 06 '10 at 01:24