How to restart a Java program to get environment variables active -


in swing application button set environment variables via setx. have restart application variables active.

to restart application use code https://dzone.com/articles/programmatically-restart-java

it works fine, variables not set.

when restart application manually vars set.

here code link above:

/**   * sun property pointing main class , arguments.   * might not defined on non hotspot vm implementations.  */ public static final string sun_java_command = "sun.java.command";  /**  * restart current java application  * @param runbeforerestart custom code run before restarting  * @throws ioexception  */ public static void restartapplication(runnable runbeforerestart) throws ioexception{     try{         // java binary         string java = system.getproperty("java.home") + "/bin/java";         // vm arguments         list<string> vmarguments = managementfactory.getruntimemxbean().getinputarguments();         stringbuffer vmargsoneline = new stringbuffer();          (string arg : vmarguments){             // if it's agent argument : ignore otherwise             // address of old application , new 1 in conflict             if (!arg.contains("-agentlib")){                 vmargsoneline.append(arg);                 vmargsoneline.append(" ");             }         }          // init command execute, add vm args         final stringbuffer cmd = new stringbuffer("\"" + java + "\" " + vmargsoneline);          // program main , program arguments         string[] maincommand = system.getproperty(sun_java_command).split(" ");          // program main jar         if (maincommand[0].endswith(".jar")){             // if it's jar, add -jar mainjar             cmd.append("-jar " + new file(maincommand[0]).getpath());         }else{             // else it's .class, add classpath , mainclass             cmd.append("-cp \"" + system.getproperty("java.class.path") + "\" " + maincommand[0]);         }          // add program arguments         (int = 1; < maincommand.length; i++){             cmd.append(" ");             cmd.append(maincommand[i]);         }          // execute command in shutdown hook, sure         // resources have been disposed before restarting application         runtime.getruntime().addshutdownhook(new thread(){             @override             public void run(){                 try{                     runtime.getruntime().exec(cmd.tostring());                 }catch(ioexception e){                     e.printstacktrace();                 }             }         });          // execute custom code before restarting         if (runbeforerestart != null){             runbeforerestart.run();         }          // exit         system.exit(0);     }catch(exception e){         // went wrong         throw new ioexception("error while trying restart application", e);     } } 

now used apache exec , seem work.

executor exec = new defaultexecutor(); commandline cl = commandline.parse(command); map<string, string> env = environmentutils.getprocenvironment(); env.put("xyz", "xyz"); //own vars exec.execute(cl, env); 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -