我使用以下命令创建了一个批处理文件:
@echo 关闭
echo.>“桌面:\testing\draft.txt”
@echo 将文本写入draft.txt> 桌面:\testing\draft.txt
这意味着当我执行批处理文件时,我希望在我在桌面上创建的测试文件夹中创建一个包含一些文本的 Draft.txt 文件。我希望在运行 Java 类时执行批处理文件。但是,我收到以下错误:
There is no program associated to perform the requested action. Please install a program or, if one is already installed, create an association in the Default Programs control panel.
这是我的 Java 类:
public class DraftBatchFile {
public DraftBatchFile() {
super();
}
/**Main Method
* @param args
*/
public static void main(String[] args) {
//Get Runtime object
Runtime runtime = Runtime.getRuntime();
try {
//Pass string in this format to open Batch file
runtime.exec("cmd /c start Desktop:\\DraftBatchFile.bat");
} catch (IOException e) {
System.out.println(e);
}
}
}
运行 Java 类时如何获取批处理文件来执行命令?我什至无法运行 Java 类。为什么会这样?我需要添加更多代码吗?有人请帮助我,因为我是新手。非常感谢。
最佳答案
桌面:
没有任何意义。
"%userprofile%\Desktop\Testing\Draft.txt"
会做你的意思。 (注意引号)
Driveletter:\Folder\File.ext
所以
c:\windows\win.ini
参见Command to run a .bat file了解更多信息。
关于java - 无法使用java类运行批处理命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47449078/