日期:2014-05-20 浏览次数:21129 次
public static void main(String[] args)
{
String command = "cmd /c C:\\test.bat";
try
{
Runtime.getRuntime().exec(command);
}
catch (Exception e)
{
e.printStackTrace();
}
}
Runtime.getRuntime().exec("cmd /c start C:\\test.bat");
------解决方案--------------------
Runtime.getRuntime().exec("C:\\test.bat");
直接这样可以不?
------解决方案--------------------
"cmd /c start \"C:\\test.bat\""
------解决方案--------------------
你按照我的步骤走一遍:
1.
G盘下建一个文件
文件名HelloWorld.bat
文件内容:
import javax.swing.*;
public class HelloWorld {//无论如何要记住这个类名,因为它是程序的入口
public static void main(String[] args) {
JFrame jFrame = new JFrame();
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.setBounds(200, 200, 30, 50);
jFrame.add(new JLabel("HelloWorld"));//显示
jFrame.setVisible(true);
}
}
------解决方案--------------------
对比一下 很显然,
1.你的应该是批处理文件有问题
应该用对路径,比如我的 -d g:/ -classpath g:/
就是批处理中要执行的另一程序路径定死
2.main方法中 process.waitFor();
------解决方案--------------------
说的不够清楚:
批处理中的内容中
你不应该用相对路径,因为批处理认识那个程序相对路径 可是java虚拟机却不认识了
------解决方案--------------------
你的代码没有等待bat执行结束。
这样改:
public static void main(String[] args)
{
String command = "cmd /c d:\\test.bat";
try
{
Process process = Runtime.getRuntime().exec(command);
process.waitFor();
}
catch (Exception e)
{
e.printStackTrace();
}
}
------解决方案--------------------
public static void main(String[] args) {
String command = "cmd /c start C:\\test.bat";
try
{
Runtime.getRuntime().exec(command);
}
catch (Exception e)
{
e.printStackTrace();
}
}