我创建了一个java类来执行桌面上的批处理文件,以便批处理文件中的命令也将被执行。问题是,我不断收到错误:
The filename, directory name, or volume label syntax is incorrect. The filename, directory name, or volume label syntax is incorrect.
我已经检查了 .bat 名称和目录。它是正确的。当我输入 cmd/c start C:/Users/attsuap1/Desktop 时,Windows 资源管理器将打开桌面选项卡。但是,当我输入 cmd/c start C:/Users/attsuap1/Desktop/DraftBatchFile.bat 时,会出现错误。我的 DraftBatchFile.bat 位于我的桌面上。
这是我的java代码:
public class OpenDraftBatchFile{
public OpenDraftBatchFile() {
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 C:/Users/attsuap1/Desktop/DraftBatchFile.bat");
} catch (IOException e) {
System.out.println(e);
}
}
为什么目录正确却无法执行批处理文件?请有人帮助我。非常感谢。
这些是DraftBatchFile.bat中的代码
@echo 关闭
echo.>“桌面:\testing\draft.txt”
@echo 将文本写入draft.txt> 桌面:\testing\draft.txt
当我通过运行java类执行DraftBatchFile.bat时,我希望在我创建的测试文件夹(在桌面中)中创建一个draft.txt文件。
最佳答案
不存在桌面:\
尝试这样的事情。
@echo off
echo . %userprofile%\Desktop\testing\dblank.txt
@echo Writing text to draft.txt > %userprofile%\Desktop\testing\dblank.txt
关于java - 无法使用 java 类访问和运行批处理文件命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47450543/