running mvn using ProcessBuilder

后端 未结 1 1021
一生所求
一生所求 2021-01-22 18:37

I had created a class which create process using ProcessBuilder and then launch process

ProcessBuilder pb = new ProcessBuilder(\"mvn\",\"exec:java\",\"-Dexec.mai         


        
相关标签:
1条回答
  • 2021-01-22 19:30

    This happens because the windows shell (cmd) has a feature: it tries to add extensions exe, 'bat', 'cmd' to command line you are running. Once it finds the first match (i.e. file that really exists in file system) it runs it.

    In case of maven you have unix shell script mvn that cannot be executed on windows and windows batch file '.bat'. Command prompt adds '.bat' to 'mvn' that you type in command prompt, sees that the file exists and runs it.

    When you are running process from java you do not have shell, so no-one does this job. I'd suggest you to check the operating system and hold command per OS. If you want clear solution create resource file cmd.properties:

    mvn.windows = mvn.bat
    mvn.unix = mvn
    

    Now check OS using system property os.name and create command using data from cmd.properties.

    Alternative solutionis to run command using cmd /c on windows and '/bin/sh -c' on unix but it does not simplify anything, so I'd avoid this.

    0 讨论(0)
提交回复
热议问题