简体   繁体   English

如何从Java程序执行命令

[英]How to execute command from Java program

I need to execute this command to stop selenium server. 我需要执行此命令来停止硒服务器。

wmic path win32_process Where "CommandLine Like '%selenium-server%'"  Call Terminate

from my java program. 从我的java程序。 I tried 我试过了

Runtime.getRuntime().exec("command as a single string escaped")

and

Runtime.getRuntime().exec(String[])

but it is not getting executed. 但是它没有被执行。 When I execute the same command from cmd it is working fine. 当我从cmd执行相同的命令时,它工作正常。 Can someone please help. 有人可以帮忙吗?

使用ProcessBuilder更好地转义/处理空格。

You can try to use this one : 您可以尝试使用此方法

public Process exec(String[] cmdarray, String[] envp, File dir) 
  throws IOException   

Executes the specified command and arguments in a separate process with the specified environment and working directory. 在具有指定环境和工作目录的单独进程中执行指定的命令和参数。

then wait for the process to end using the process.waitFor() methode. 然后使用process.waitFor()方法等待进程结束。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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