使用Runtime.getRuntime().exec(cmdLine, envp,workingDirectory)启动的进程;
无法创建临时文件。
用在 Maven plugin for Eclipse 里面
引自 mvn
启动:
Caused by: java.io.IOException: �ܾ���ʡ�
at java.io.WinNTFileSystem.createFileExclusively(Native Method)
at java.io.File.createTempFile(File.java:1879)
这与 Maven 无关,如 Gradle has the same issue
Demo piece of code遇到同样的错误。
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<String, String> envm = new HashMap<String, String>();
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, envp, 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
也没有帮助。
If envp is null, the subprocess inherits the environment settings of the current process.
最佳答案
通过传递一组环境变量来解决。
关于Java - 使用 Runtime.getRuntime().exec 启动的进程(无法创建临时文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21397170/