【问题标题】:Cannot access and run batch file commands using java class无法使用 java 类访问和运行批处理文件命令
【发布时间】:2018-05-07 03:02:57
【问题描述】:

我创建了一个 java 类来执行我桌面上的批处理文件,以便批处理文件中的命令也将被执行。问题是,我不断收到错误:

文件名、目录名或卷标语法不正确。 文件名、目录名或卷标语法不正确。

我已经检查了 .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 off echo.>"Desktop:\testing\draft.txt" @echo Writing text to draft.txt> Desktop:\testing\draft.txt

当我通过运行 java 类执行 DraftBatchFile.bat 时,我希望在我创建的测试文件夹中创建一个 draft.txt 文件(在桌面中)。

【问题讨论】:

  • 检查 PATH 环境变量。查看链接更改路径java.com/en/download/help/path.xml
  • 您好,感谢您的帮助。应该在 Path 环境中添加什么?
  • Java PATH 目录,C:\Program Files\Java\jdk1.7.0_67\bin
  • 我已经设置好了
  • @YevhenDanchenko 按照我下面的回答最好使用%userprofile%\Desktop,因为如果它在其他系统上运行,用户attsuap 将不存在

标签: java batch-file cmd


【解决方案1】:

没有desktop:\这样的东西

不如试试这样的。

@echo off
echo . %userprofile%\Desktop\testing\dblank.txt
@echo Writing text to draft.txt > %userprofile%\Desktop\testing\dblank.txt

【讨论】:

  • 我仍然收到错误The filename, directory name, or volume label syntax is incorrect. 我不知道为什么 :( 我编辑了批处理文件。我在我的 java 类中编辑了runtime.exec("cmd /c start %userprofile%/Desktop/DraftBatchFile.bat"); 那行代码但是当我运行我的 java 类时我仍然没有得到我想要的它仍然给出错误
  • 我希望在我在桌面上创建的测试文件夹中创建 draft.txt 文件。实际上,我只需要证明,当我运行我的 java 类时,我的批处理文件正在执行。有没有其他更简单的命令可以放在我的批处理文件中来证明这一点?
  • 我将创建一个新的批处理文件并尝试一下
  • @SushaNaidu 现在请尝试我的批处理代码,我编辑了我的答案我忘了替换第一个回显中的桌面:\ 部分。
  • 成功了!太感谢了 ! :)
【解决方案2】:

您只需要更改目录并在不存在的情况下创建新的子目录。我的提议是像这样更改 .bat 文件:

@echo off
if not exist "%userprofile%\Desktop\testing" mkdir "%userprofile%\Desktop\testing"
echo.>"%userprofile%\Desktop\draft.txt"
@echo Writing text to draft.txt>"%userprofile%\Desktop\draft.txt"

您还可以创建 .txt 文件并通过 Java 代码在其中写入文本:

    File directory = new File(System.getProperty("user.home")+"//Desktop//testing");
    if (!directory.exists())
         directory.mkdirs();
    String content = "Writing text to draft.txt";
    File file = new File(System.getProperty("user.home")+"//Desktop//testing//draft.txt");
    BufferedWriter writer = null;
    try {
        writer = new BufferedWriter(new FileWriter(file));
        writer.write(content);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (writer != null)
                writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-08
    • 2013-06-21
    • 1970-01-01
    • 2013-02-27
    • 1970-01-01
    • 2018-07-31
    相关资源
    最近更新 更多