Execute or Run application with java Runtime() or Process Builder

二次信任 提交于 2020-01-05 01:31:21

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!