【问题标题】:Batch file not executed when trying to run in background尝试在后台运行时未执行批处理文件
【发布时间】:2026-01-27 15:00:01
【问题描述】:

我正在尝试从 java 类执行一些批处理文件。我希望批处理在不打开 cmd 窗口的情况下运行,我想等到它完成。

当使用下面的命令时(没有背景)——它工作得很好:

String executeCmd = "cmd /c start /wait " +config.getJarPath()+ " --context_param Path=" +folderName;
        final Process process = run.exec(executeCmd);
        process.waitFor();

但是当我添加 /b(在后台运行)时,批处理文件没有运行:

String executeCmd = "cmd /c start /B /wait " +config.getJarPath()+ " --context_param Path=" +folderName;
        final Process process = run.exec(executeCmd);
        process.waitFor();

你知道它是什么吗?

非常感谢

【问题讨论】:

  • 如果使用 /B 参数手动运行命令会发生什么。有用吗
  • 刚尝试这样做,我看到了一些奇怪的东西。我发送的 context_param 不知何故搞砸了。其中一个斜线被转换为♀(一些奇怪的字符)

标签: java windows batch-file cmd


【解决方案1】:

使用start /b 命令标签只需在当前 窗口中启动程序。

B           Start application without creating a new window. 

如果您想将批处理文件作为隐藏进程启动,可以使用简单的vb script 来执行此操作。

或者,您可以使用Batch-To-Exe converter,并在转换时指定您希望 exe 成为 Ghost 应用程序

【讨论】:

  • 感谢您的回答。没有为我工作。决定只使用 /min.. 这不是一个完美的解决方案,但我可以处理它。
【解决方案2】:

/B/wait 一起使用即可。

例如cmd /c start /B /wait yourbatfile.bat

【讨论】: