1

我想在 java 代码中调用 cmd 命令。我说:

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一切正常时。有什么问题?

4

3 回答 3

3

这段代码有什么问题。这是完美的工作。打开并显示文件 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) {}

请检查路径是否正确,目录和文件是否有遗漏或拼写

于 2012-12-25T09:09:54.183 回答
0

我希望它不是cmd.exe请试试这个:

String[] command = new String[3];
command[0] = "cmd";
command[1] = "/c";
command[2] = "C:/uploaded_files/111.txt";

Process p = Runtime.getRuntime().exec (command);
于 2012-12-25T09:06:39.897 回答
0

如果你想在记事本中打开文件,试试这个。

String file = "C:/uploaded_files/111.txt";

Runtime.getRuntime().exec("cmd", "/c", "notepad.exe", file);

希望它是你想要的那个。

于 2012-12-25T09:07:10.840 回答