使用ProcessBuilder运行MVN [英] running mvn using ProcessBuilder

查看:307
本文介绍了使用ProcessBuilder运行MVN的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个类,该类使用ProcessBuilder创建流程,然后启动流程

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

ProcessBuilder pb = new ProcessBuilder("mvn","exec:java","-Dexec.mainClass="+"FunnyClass");

现在,当我在linux机器上运行该类时,它运行良好,但是在Windows上却给了我错误,指出未找到"mvn"之类的东西,我需要更改为

Now when I m running this class on linux box, it run fine, but on windows it give me error, stating something like 'mvn' not found, I need to change to

ProcessBuilder pb = new ProcessBuilder
                       ("mvn.bat","exec:java","-Dexec.mainClass="+"FunnyClass");

但是,如果我在命令提示符"mvn exec:java -Dexec.mainClass = FunnyClass"上运行命令,它将运行正常.那么,为什么我需要在processbuilder中提供mvn.bat.

But if I am running command on command prompt "mvn exec:java -Dexec.mainClass=FunnyClass", it run fine. So why I need to give mvn.bat in processbuilder.

有什么解决办法吗?

我的Java应用程序将同时在Windows& Windows上运行linux box,那我该怎么办?

my java application is going to run on both windows & linux boxes, So what should I do?

推荐答案

之所以会发生这种情况,是因为Windows Shell(cmd)具有以下功能:它尝试将扩展exe,'bat','cmd'添加到命令行中正在跑步.找到第一个匹配项(即文件系统中确实存在的文件)后,它将运行它.

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.

对于Maven,您有不能在Windows和Windows批处理文件'.bat'上执行的Unix Shell脚本mvn.命令提示符将'.bat'添加到您在命令提示符中键入的'mvn',看到该文件存在并运行它.

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.

当您从Java运行进程时,您没有外壳程序,因此没有人进行此工作. 我建议您检查操作系统并按操作系统保留命令.如果要清除解决方案,请创建资源文件cmd.properties:

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

现在使用系统属性os.name检查OS,并使用cmd.properties中的数据创建命令.

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

另一种解决方案是在Windows上使用cmd /c在Unix上使用'/bin/sh -c'来运行命令,但是它并没有简化任何事情,因此我避免这样做.

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.

这篇关于使用ProcessBuilder运行MVN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆