如何从我的 Java 应用程序运行批处理文件? [英] How do I run a batch file from my Java Application?

查看:24
本文介绍了如何从我的 Java 应用程序运行批处理文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 Java 应用程序中,我想运行一个批处理文件,该文件调用scons -Q 隐式-deps-changed buildfile_load_type exportfile_load_type"

In my Java application, I want to run a batch file that calls "scons -Q implicit-deps-changed buildfile_load_type exportfile_load_type"

似乎我什至无法让我的批处理文件执行.我没主意了.

It seems that I can't even get my batch file to execute. I'm out of ideas.

这是我在 Java 中所拥有的:

This is what I have in Java:

Runtime.
   getRuntime().
   exec("build.bat", null, new File("."));

以前,我有一个想要运行的 Python Sconscript 文件,但由于它不起作用,我决定通过批处理文件调用该脚本,但该方法尚未成功.

Previously, I had a Python Sconscript file that I wanted to run but since that didn't work I decided I would call the script via a batch file but that method has not been successful as of yet.

推荐答案

批处理文件不是可执行文件.他们需要一个应用程序来运行它们(即 cmd).

Batch files are not an executable. They need an application to run them (i.e. cmd).

在 UNIX 上,脚本文件在文件开头有 shebang (#!) 来指定执行它的程序.在 Windows 中双击由 Windows 资源管理器执行.CreateProcess 对此一无所知.

On UNIX, the script file has shebang (#!) at the start of a file to specify the program that executes it. Double-clicking in Windows is performed by Windows Explorer. CreateProcess does not know anything about that.

Runtime.
   getRuntime().
   exec("cmd /c start "" build.bat");

注意:使用 start "" 命令,将打开一个单独的命令窗口,标题为空白,批处理文件的任何输出都将显示在那里.它也应该只使用cmd/c build.bat",在这种情况下,如果需要,可以从 Java 的子进程中读取输出.

Note: With the start "" command, a separate command window will be opened with a blank title and any output from the batch file will be displayed there. It should also work with just `cmd /c build.bat", in which case the output can be read from the sub-process in Java if desired.

这篇关于如何从我的 Java 应用程序运行批处理文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆