Java-使用Runtime.getRuntime()。exec(无法启动临时文件启动的进程
摘要: envp;\\Progs\\springsource\\apache-maven-3.0.4\\bin\\mvn.bat";//cmdLine.add(mavenOptions+" compile exec;String;String userDir = System.getProperty("user.dir");"user.home" User home directory;
如何解决Java-使用Runtime.getRuntime()。exec(无法启动临时文件启动的进程?
通过传递一组环境变量来解决。
解决方法
启动的进程Runtime.getRuntime().exec(cmdLine,envp,workingDirectory);
无法创建临时文件。
在Eclipse的Maven插件中使用
从mvn
启动报价:
Caused by: java.io.IOException: �ܾ���ʡ�
at java.io.WinNTFileSystem.createFileExclusively(Native Method)
at java.io.File.createTempFile(File.java:1879)
完整日志
演示代码段遇到相同的错误。
String mavenPath = "D:\\Progs\\springsource\\apache-maven-3.0.4\\bin\\mvn.bat";
String mavenOptions = "-X compile exec:java -Dexec.mainClass=runclass.Runme";
String[] cmdLine = new String[2];
cmdLine[0] = mavenPath; //cmdLine.add(mavenPath);
cmdLine[1] = mavenOptions; //cmdLine.add(mavenOptions+" compile exec:java -Dexec.mainClass="+packageClass);
String[] envp = new String[2];
//Map envm = new HashMap();
envp[0] = "JAVA_HOME=" + System.getProperty("java.home"); //System.getenv("JAVA_HOME");
envp[1] = "M2_HOME=" + System.getenv("MAVEN_HOME");
File workingDirectory = null;
String currentDir = new File(".").getAbsolutePath();
log(currentDir);
String userDir = System.getProperty("user.dir"); //User working directory ; "user.home" User home directory
workingDirectory = new File(userDir);
log(workingDirectory.toString());
//
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(cmdLine,workingDirectory);
InputStream stdout = proc.getInputStream();
InputStream stderr = proc.getErrorStream();
InputStreamReader isr = new InputStreamReader(stdout);
InputStreamReader isr2 = new InputStreamReader(stderr);
BufferedReader br = new BufferedReader(isr);
BufferedReader br2 = new BufferedReader(isr2);
更新:
传递TMP和TEMP环境变量无济于事。
通过 null
而不是envp
也无济于事。
如果envp为null,则子进程继承当前进程的环境设置。
我来说两句