简体   繁体   English

如何重新启动Java程序以使环境变量处于活动状态

[英]How to restart a Java program to get environment variables active

In my swing application is a Button to set some Environment variables via setx. 在我的swing应用程序中是一个按钮,用于通过setx设置一些环境变量。 I have to restart the application to get the variables active. 我必须重新启动应用程序才能激活变量。

To restart the application I use the code from https://dzone.com/articles/programmatically-restart-java 要重新启动应用程序,我使用来自https://dzone.com/articles/programmatically-restart-java的代码

It works fine, but the variables are not set. 它工作正常,但未设置变量。

When I restart the application manually the vars are set. 当我手动重新启动应用程序时,设置了变量。

Here is the code from the link above: 这是上面链接中的代码:

/** 
 * Sun property pointing the main class and its arguments. 
 * Might not be defined on non Hotspot VM implementations.
 */
public static final String SUN_JAVA_COMMAND = "sun.java.command";

/**
 * Restart the current Java application
 * @param runBeforeRestart some custom code to be 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();

        for (String arg : vmArguments){
            // if it's the agent argument : we ignore it otherwise the
            // address of the old application and the new one will be in conflict
            if (!arg.contains("-agentlib")){
                vmArgsOneLine.append(arg);
                vmArgsOneLine.append(" ");
            }
        }

        // init the command to execute, add the vm args
        final StringBuffer cmd = new StringBuffer("\"" + java + "\" " + vmArgsOneLine);

        // program main and program arguments
        String[] mainCommand = System.getProperty(SUN_JAVA_COMMAND).split(" ");

        // program main is a jar
        if (mainCommand[0].endsWith(".jar")){
            // if it's a jar, add -jar mainJar
            cmd.append("-jar " + new File(mainCommand[0]).getPath());
        }else{
            // else it's a .class, add the classpath and mainClass
            cmd.append("-cp \"" + System.getProperty("java.class.path") + "\" " + mainCommand[0]);
        }

        // finally add program arguments
        for (int i = 1; i < mainCommand.length; i++){
            cmd.append(" ");
            cmd.append(mainCommand[i]);
        }

        // execute the command in a shutdown hook, to be sure that all the
        // resources have been disposed before restarting the application
        Runtime.getRuntime().addShutdownHook(new Thread(){
            @Override
            public void run(){
                try{
                    Runtime.getRuntime().exec(cmd.toString());
                }catch(IOException e){
                    e.printStackTrace();
                }
            }
        });

        // execute some custom code before restarting
        if (runBeforeRestart != null){
            runBeforeRestart.run();
        }

        // exit
        System.exit(0);
    }catch(Exception e){
        // something went wrong
        throw new IOException("Error while trying to restart the application", e);
    }
}

Now I used Apache exec and it seem to work. 现在,我使用了Apache exec,它似乎可以工作。

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);

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

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