【问题标题】:Unable to run batch commands using java class无法使用 java 类运行批处理命令
【发布时间】:2017-11-23 06:39:07
【问题描述】:

我用以下命令创建了一个批处理文件:

@echo off echo.>"Desktop:\testing\draft.txt" @echo Writing text to draft.txt> Desktop:\testing\draft.txt

这意味着当我执行批处理文件时,我希望在我在桌面上创建的测试文件夹中创建一个包含一些文本的 draft.txt 文件。我希望在运行 Java 类时执行批处理文件。但是,我收到以下错误:

没有关联的程序来执行请求的操作。请安装一个程序,如果已经安装,请在“默认程序”控制面板中创建一个关联。

这是我的 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 类。为什么呢?我必须添加更多代码吗?有人,请帮助我,因为我是新手。非常感谢。

【问题讨论】:

  • 为什么使用Desktop:而不是文件的绝对路径? I am even unable to run the Java class 是什么意思?有什么问题?
  • 文件位于桌面。当我运行 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. 我不知道为什么会这样
  • 问题是你的 Windows 不知道如何处理 Desktop: 协议,因为没有应用程序被指定为协议处理程序。但是当你运行cmd /c start %USERPROFILE%\Desktop\DraftBatchFile.bat 时会发生什么?
  • 我必须运行 java 类来执行 DraftBatchFile.bat,当我运行 java 类时,draft.txt 必须在测试文件夹中创建。我该怎么做,请帮帮我
  • @saw303 当我运行cmd /c start %USERPROFILE%\Desktop\DraftBatchFile.bat 时,会打开一个命令提示符并显示The filename, directory name, or volume label syntax is incorrect. The filename, directory name, or volume label syntax is incorrect.

标签: java batch-file


【解决方案1】:

Desktop: 没有任何意义。

    "%userprofile%\Desktop\Testing\Draft.txt"

会按照你的意思去做。 (注意引号)

     Driveletter:\Folder\File.ext

所以

     c:\windows\win.ini

请参阅Command to run a .bat file 了解更多信息。

【讨论】:

    猜你喜欢
    • 2018-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-23
    • 2018-09-08
    • 1970-01-01
    • 1970-01-01
    • 2013-02-27
    相关资源
    最近更新 更多