Search code examples
javabatch-filedoswindows-console

set local dos variables from java code


If "java -jar" is run from a command line, is there a way to set local dos variable from java program so that after java is exited, it can still be present in the same session?

example

(cmd)
c:\java package.Class 

/*then in program you do something like 
'System.setVariable("name","value");'
*/

// java exited

echo %name%

value

Solution

  • No. Processes cannot modify their parents' environment.

    The best thing you can do is cheat a little and do either of the following:

    • Let the Java program write out a batch file in some known location and call it afterwards to set variables. Since batch files run in the current cmd process they can alter environment variables there.
    • Let the program output name/value pairs or complete set commands, catch the output and set the variables yourself afterwards. Goes wrong as soon as you want or have other output, I guess.