1

process launched with Runtime.getRuntime().exec(cmdLine, envp, workingDirectory); cannot create temp file.

It is used inside Maven plugin for Eclipse

Quote from mvn launched:

 Caused by: java.io.IOException: �ܾ���ʡ�
    at java.io.WinNTFileSystem.createFileExclusively(Native Method)
    at java.io.File.createTempFile(File.java:1879)

Full log

This is not Maven related as Gradle has the same issue

Demo piece of code runs into the same error.

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

Update:

Passing TMP and TEMP environment variables does not help.
Passing null instead of envp also does not help.

If envp is null, the subprocess inherits the environment settings of the current process.

Community
  • 1
  • 1
Paul Verest
  • 60,022
  • 51
  • 208
  • 332
  • Beware, that each element in `cmdLine` is expected to be a individual argument passed to the first element in `cmdLine` (ie `cmdline[0]` is the command to executes, all other elements are individual arguments). Right now, you `bat` file is receiving a single argument, which I doubt is what you want ;) – MadProgrammer Jan 28 '14 at 05:12
  • I am quite aware of that. This is done on purpose, otherwise there is some undocumented handling of "a=b" string (just becomes "b"). See http://stackoverflow.com/questions/21358548/java-runtime-getruntime-exec-looses-changes-some-parameters – Paul Verest Jan 28 '14 at 12:02

1 Answers1

0

Solved by passing a set of environment variables.

Paul Verest
  • 60,022
  • 51
  • 208
  • 332