Running an executable command in Java with parameters?

时间秒杀一切 提交于 2019-12-13 07:46:09

问题


So i understand how to use the Runtime command in java pretty well to get an executable to run. My question is how would i code that to incorporate a parameter such as you would see in the target in a shortcut property, i.e. target: "C:......\notepad.exe" -w. In what way could I incorporate a parameter such as -w into the Java runtime command.


回答1:


Use a ProcessBuilder and supply the necessary arguments to its constructor:

ProcessBuilder builder = new ProcessBuilder("C:\\path\\to\\notepad.exe", "-w");

The first argument is always the application, any other arguments (if present) will be the arguments to add to the application.

You can then call the start() method to start it and grab the process object back if you so wish.




回答2:


Take a look at ProcessBuilder - http://download.oracle.com/javase/1.5.0/docs/api/java/lang/ProcessBuilder.html

This should provide you a relatively fail-safe way to execute with parameters and arguments




回答3:


You can supply String[] to the exec method, see here. Where the first argument is the command and the next are the parameters.




回答4:


In addition to the abovementioned, you can do something like:

                    Runtime.getRuntime().exec(exeFile.toString() + "params");

where exeFile is a File of your executable.



来源:https://stackoverflow.com/questions/6963232/running-an-executable-command-in-java-with-parameters

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