简体   繁体   English

在Java中执行批处理文件时出现问题

[英]Problem executing a batch file in Java

I am trying to execute a batch file in my Java application. 我正在尝试在Java应用程序中执行批处理文件。 The code is the following: 代码如下:

Runtime.getRuntime().exec("cmd /C start C:/Documents and Settings/Zatko/My Documents/Project-Workspace/IUG/external/apps/archive/run-server.bat");

When it executes, a error dialog appear telling "Windows cannot find 'C:/Documents'. make sure you typed the name corretly...." 当它执行时,将出现一个错误对话框,提示“ Windows找不到'C:/ Documents'。请确保您正确键入了该名称...。”

When I execute with the same code another batch file, named file.bat and located in the C:/Temp folder, it works perfectly.... 当我用相同的代码执行另一个位于C:/ Temp文件夹中的名为file.bat的批处理文件时,它可以完美运行。

Does anyone know where the problem may be? 有人知道问题可能在哪里吗? Is it about spacing characters? 关于空格字符吗?

Thanks in advance 提前致谢

Edit: 编辑:

It seems the start command needs an extra parameter whenever the path to the executable to start is enclosed in ". As one must surround parameters which contains spaces by " this is a little bit confusing as the start comand works as excepted when one has a path without spaces and thus does not enclose it with ". That's what happened when I tested the code below for a folder c:/temp and it worked without an additional parameter. 似乎只要将要启动的可执行文件的路径括在“中,启动命令就需要一个额外的参数。因为必须将包围空格的参数用”包围起来,所以这有点令人困惑,因为启动命令的工作方式与当有路径时的例外相同没有空格,因此不会用“”将其括起来。这就是我在以下代码中测试文件夹c:/ temp的代码时发生的情况,并且该代码无需附加参数即可工作。

The parameter in charge is a title for the window that is opened. 负责的参数是打开的窗口的标题。 It must come es second paramter and if it contains spaces must be surrounded by ". 它必须是第二个参数,如果包含空格,则必须用“。”括起来。

I suggest to always use " for both title and path. 我建议对标题和路径始终使用“”。

So here is the updated command: 所以这是更新的命令:

You need to enclose 你需要附上

c:/Document and Settings/... c:/文档和设置/ ...

with " as the filename contains spaces. And you need to include a title when using the start command with a paramter with ". 使用“”,因为文件名包含空格。使用带有“。”的参数的启动命令时,需要包含一个标题。

For Java that would be: 对于Java而言:

Runtime.getRuntime().exec("cmd /C start \\"Server\\" \\"C:/Documents and Settings/Zatko/My Documents/Project-Workspace/IUG/external/apps/archive/run-server.bat\\""); Runtime.getRuntime()。exec(“ cmd / C start \\” Server \\“ \\” C:/ Documents and Settings / Zatko / My Documents / Project-Workspace / IUG / external / apps / archive / run-server.bat \\ “”);

Greetz, GHad 加的特Greetz

It's much better to use an array: 最好使用数组:

String[] array = { ... };
Runtime.getRuntime().exec(array);

as in

String[] array = { "cmd", "/C", "start", 
    "C:/Documents and Settings/Zatko/My Documents/.../run-server.bat" };
Runtime.getRuntime().exec(array);

Using an array is specially important if you have spaces in one of the parameters, like you do. 如果像其中一样,在其中一个参数中有空格,则使用数组特别重要。

Runtime.getRuntime().exec("cmd /C start \"\" \"C:/Documents and Settings/Zatko/My Documents/Project-Workspace/IUG/external/apps/archive/run-server.bat\"");

should work. 应该管用。

You need to quote arguments with spaces or shell metacharacters in them. 您需要在引号中加上空格或外壳元字符。 And start expects the first quoted argument to be a window title, so give it an empty one so it's happy. 并且start希望第一个引用的参数是一个窗口标题,所以给它一个空的就很高兴。

这有效:

List<String> templst = new ArrayList<String>();

templst.add("cmd");

templst.add("/C");

templst.add("start");

templst.add("backup.bat");

Process p = rt.exec(templst.toArray(new String[]{}), null, new File(path));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM