Execute or Run application with java Runtime() or

2019-07-08 11:08发布

问题:

I'm trying to execute applications from java servlet. When I run it on eclipse integrated tomcat its working fine. When I'm trying to do the same on os integrated tomcat server by deploying .war file into webapps, its not working. It doesn't encountering any error as well. Even I checked the logs, there is no error nothing but usual tomcat access log. Is there any other ways that I can execute applications like firefox, chrome, gedit etc.

Note: Basic bash commands like ls, chmod, mkdir were working. But while calling gedit, firefox like apps its not working.

Statements Used: * Runtime.getRuntime().exec(command) even firefox is called with /bin/bash -c as well. No result. * ProcessBuilder

Any alternatives ??

回答1:

ProcessBuilder accepts multiple arguments, and you are supposed to split your command arguments. So if you're for example executing ls -sl, you have to call

 new ProcessBuilder("ls", "-sl");

Another example:

 new ProcessBuilder("pg_dump", "database", '-u', 'username', '--clean');

Otherwise ProcessBuilder might not recognise the command. Also don't forget to use

processBuilder.redirectErrorStream(true);   // equivalent of 2>&1

Which will redirect stderr to stdout.