Question

Can you tell me a way to execute process from Java irrespective of underlying operating system? E.G.

Process p = Runtime.getRuntime().exec("cmd /c mvn clean install");

Above line executes Maven in Windows environment but how can I run above command in any operating system?

If I don’t add "cmd/c" in starting then I am not able to run in windows environment and the error says cannot find mvn.bat although maven home is set properly.

Was it helpful?

Solution 2

You really can't literally "execute process from Java irrespective of underlying operating system", because Runtime.exec forwards a launch command top the OS. You have then to describe the process you want to launch, and the method to do so. But, you can "virtualize" the Process Launching by implementing a,say, ProcessLauncher interface for processes you know and subclass launch for each OS With a factory which detects on which OS you are, and create the correct ProcessLauncher, you should be fine

OTHER TIPS

You can check what's the type of OS you're having and then execute the command-line statement.

In order to check the OS type, you need to retrieve the value of the os.name system property:

String OS = System.getProperty("os.name").toLowerCase();

Then with a simple if/else statement you can execute the command-line statement depending on the type of OS.

if (isUnix()) {
    String statement = new String[] { "/bin/bash", "-c", "mvn clean install" };
    Process p = Runtime.getRuntime().exec(statement);
else if (isWindows()) {
    Process p = Runtime.getRuntime().exec("cmd /c mvn clean install");
}

...

Helper methods to check the OS type:

public boolean isUnix() {
    return (OS.indexOf("nix") >= 0 || OS.indexOf("nux") >= 0 || OS.indexOf("aix") > 0 );
}

public boolean isWindows() {
    return (OS.indexOf("win") >= 0);
}

More info you can find here.

You'll probably have to check which operating system it is, then replace cmd /c with the relevant command for that operating system.

See this question for how to determine which system you're running on: How do I programmatically determine operating system in Java?

Executing external programs is something which is per definition not platform independent. There may be different places the programs are installed, different settings of PATH or different call syntax. (e.g. /all on windows and --all on unix). (Let alone there may be different programms from different people for the same task.)

You best bet is to use either a configuration file for your program where you specify what program and how you want to call it or do the same thing with setting up your programs execution environment in a platform-specific way (e.g. using start.bat / start.sh) and use System.getEnv().

My personal favorite is the first approach because it hasn't to deal side-effects. The second on the other hand allows the "system-hackers" to put some code there to do ... aehm... their stuff (e.g. see Tomcat's startup.sh/bat catalina.sh/bat) :)

For this particular case there are libraries that can help, for example you could use the maven-ant-tasks library programatically, as Ant hides all the platform-specific mess behind its own platform-independent API:

Project p = new Project();
p.init();
Mvn mvnTask = new Mvn();
mvnTask.setProject(p);
mvnTask.setPom(new File("path/to/pom.xml"));
mvnTask.createArg().setValue("clean");
mvnTask.createArg().setValue("install");

mvnTask.perform();

This should work the same on all platforms, and does not require a standalone installation of mvn - you just need the Ant and maven-ant-tasks libraries on your classpath, and the Mvn task will download whatever else it needs the first time it is run.

Better use apache commons exec for running external programs since there are many OS platform specific issues like output stream buffer size, error handling etc.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top