简体   繁体   中英

Execute command using Java on Windows

I want to execute a command mspview -r "C:\\\\Users\\\\SS\\\\Desktop\\\\phantomjs-1.9.2-windows\\\\image.tif" . How can I do it via Java code? I am trying to do this with a batch file. The same command when I run with the help of RUN . I am getting correct output. I have executed a .exe program with the help of a batch file with the following code C:\\Users\\SS\\Desktop\\phantomjs-1.9.2-windows\\phantomjs.exe .

您基本上是在问如何在Java中运行Shell命令,对吗?

Runtime.getRuntime().exec("whatever system call you want");

You need to use ProcessBuilder

Process process = new ProcessBuilder(
"C:\\PathToExe\\exe.exe","param1","param2").start();
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;

System.out.printf("Output of running %s is:", Arrays.toString(args));

while ((line = br.readLine()) != null) {
  System.out.println(line);
}

code that is already found on stackoverflow Execute external program in java

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM