使用java程序执行cmd命令

时间:2014-12-18 06:28:48

标签: java cmd command

我想使用java程序执行以下命令。 " java -jar Demo.jar readExcelDemo.Hvd"

public class ExcelDriver {
    public static void main(String[] args) throws IOException {
        Runtime runTime = Runtime.getRuntime();
        Process process = runTime.exec("cmd","/c", "cmd.exe","java -jar Demo readExcelDemo.Hvd");

            }
}

3 个答案:

答案 0 :(得分:0)

创建包含内容的.bat或.cmd文件 java -jar演示readExcelDemo.Hvd

将您的代码更改为

public class ExcelDriver {
public static void main(String[] args) throws IOException {
    Runtime runTime = Runtime.getRuntime();
    Process process = runTime.exec("cmd","/c", "myfile.cmd");

        }

}

答案 1 :(得分:0)

试试这个:

Runtime runTime = Runtime.getRuntime();
Process process = runTime.exec("java -jar Demo.jar readExcelDemo.Hvd");

如果以上代码不合适,请尝试以下操作:

Runtime runTime = Runtime.getRuntime();
Process process = runTime.exec("cmd.exe /c start java -jar Demo.jar readExcelDemo.Hvd");

如果您想观看cmd,可以使用以下代码。 由于/ c标志,它会立即关闭。

 Process p = runTime.exec("cmd.exe /c start cmd /k java -jar Demo.jar readExcelDemo.Hvd");

答案 2 :(得分:-1)

您可以致电:

Runtime.getRuntime().exec(command);