set windows PATH environment variable at runtime in Java

后端 未结 4 1709
广开言路
广开言路 2020-12-11 05:58

I have a java program that fires off an executable using the Runtime.exec() method. I\'m using the variant that takes in a set of command line params as one argument, and so

4条回答
  •  轮回少年
    2020-12-11 06:30

    One solution might be to add an additional command to "exec" where you set the path ... as in the example found here: http://www.neowin.net/forum/topic/620450-java-runtimegetruntimeexec-help/

    excerpt:

                cmd = new String[7];
                cmd[0] = "cmd"; 
                cmd[1] = "/C";
                cmd[2] = "set PATH=C:\\Program Files\\Java\\jdk1.6.0_04\bin";
                cmd[3] = "copy " + "\"" +path + "\\" +name+ "\"" + " C:\\java";
                cmd[4] = "chdir C:\\java";
                cmd[5] = "javac *.java";
                cmd[6] = "jar cmf mainClass.txt"+" name"+".jar *.class";
    
                try{
                Runtime.getRuntime().exec(cmd);
    

提交回复
热议问题