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

如何在Java类中执行cmd命令?,java,process,cmd,runtime.exec,Java,Process,Cmd,Runtime.exec,我想在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中时,一切都很好。有什么问题吗?我希望它不是cmd.exe请尝试以下操作: String[] comman

我想在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
中时,一切都很好。有什么问题吗?

我希望它不是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);

如果要在记事本中打开文件,请尝试此操作

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

Runtime.getRuntime().exec("cmd", "/c", "notepad.exe", 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) {}

请检查路径是否正确,目录和文件是否丢失或拼写正确

运行该程序会产生什么结果,你到底得到了什么?这可能与文件写入权限有关。请遵循我在评论中给出的相同建议。@Gustav Grusell-Hmm我想在
C:/uploaded\u files
文件夹中获得111.txt。但文件夹还是空的,他很可能只是打印了字符串。他从不要求进程输出。你是什么意思?我希望它不是cmd.exe?