问题
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.
来源:https://stackoverflow.com/questions/31201250/execute-or-run-application-with-java-runtime-or-process-builder