如何在Java类中执行cmd命令?

问题描述:

我想用Java代码调用cmd命令。我说:

I want to call cmd command in java code. I say:

String str ="C:/uploaded_files/111.txt";
Process process = Runtime.getRuntime().exec(new String[]{"cmd.exe", "/c",str});
System.out.println(str);

也不要得到 111.txt 。奇怪的是,当这段代码在 jsp 中时,一切正常。可能是什么问题?

And dont get 111.txt. Its strange because when this code was in jsp all works fine. What can be wrong?

此代码有什么问题。一切正常。打开并显示文件111.txt

what's the problem with this code. It's perfectly working. opens and shows content of the file 111.txt

try {
    String str ="C:/uploaded_files/111.txt";
    Process process = Runtime.getRuntime().exec(new String[]{"cmd.exe", "/c",str});
    System.out.println(str);
    } catch (Exception ex) {}

请检查路径是否正确以及是否正确目录和文件不会丢失或拼写

please check whether the path is correct and whether the directories and files are not missed or spelled