java中调用cmd命令

发布网友 发布时间:2022-04-24 19:35

我来回答

5个回答

热心网友 时间:2023-10-08 20:52

可以通过Runtime 的exec方法来进行执行。举例:
public class Test {
public static void main(String[] args) {
Runtime r=Runtime.getRuntime();
Process p=null;
try{
String s="DOT的路径\\dot.exe c:\\user\\Administrator\\test.txt -Tpng -o result.png";
p=r.exec(s);
}catch(Exception e){
System.out.println("错误:"+e.getMessage());
e.printStackTrace();
}
}
}

热心网友 时间:2023-10-08 20:53

Runtime.getRuntime().exec("cmd命令");

Runtime.getRuntime().exec("E:/tomcat/bin/service.bat install");追问The tomcat.exe was not found...
The CATALINA_HOME environment variable is not defined correctly.
This environment variable is needed to run this program

追答Runtime.getRuntime().exec("cd /d E:/tomcat/bin");

Runtime.getRuntime().exec("service.bat install");

试试或者 自己写个bat
里面
cd /d E:/tomcat/bin
service.bat install
然后在执行你写的这个bat

热心网友 时间:2023-10-08 20:53

Runtime.getRuntime().exec("cmd /c E:/tomcat/bin/service.bat install");

热心网友 时间:2023-10-08 20:54

Runtime.getRuntime().exec("cmd /c service.bat install", null, "e:/tomcat/bin");

热心网友 时间:2023-10-08 20:55

Runtime.getRuntime().exec(command);

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com