传递给执行官的路径 [英] Passing a path to exec

查看:118
本文介绍了传递给执行官的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用exec函数。可执行文件的路径包含空格,这给了我悲伤
我的代码看起来像这样

I am trying to use the exec function. The path to the executable contains spaces and this is giving me grief My code looks like this

Runtime.getRuntime().exec("\"C:\\Program Files (x86)\\ASL\\_ASL Software Suite_installation.exe\"", null, new File("\"C:\\Program Files (x86)\\ASL\\_ASL Software Suite_installation\""));

当执行此操作时,我得到一个例外 -

When this is executed I get an exception -

Cannot run program ""c:\Program" 

如果有人能帮我解决这个问题,我将不胜感激

I would be grateful if someone can give me some help in solving this

提前致谢

推荐答案

来自 Runtime.exec(String command,String [] envp,File dir)


在具有指定环境和工作目录的单独进程中执行指定的字符串命令。

Executes the specified string command in a separate process with the specified environment and working directory.

这是一种方便的方法。调用形式的exec(command,envp,dir)的行为方式与调用 exec(cmdarray,envp,dir),其中 cmdarray 命令中所有标记的数组

This is a convenience method. An invocation of the form exec(command, envp, dir) behaves in exactly the same way as the invocation exec(cmdarray, envp, dir), where cmdarray is an array of all the tokens in command.

更确切地说,命令字符串使用 StringTokenizer分解为标记由调用 new StringTokenizer(命令)创建,没有进一步修改字符类别。然后,由tokenizer生成的标记以相同的顺序放置在新的字符串数组 cmdarray 中。

More precisely, the command string is broken into tokens using a StringTokenizer created by the call new StringTokenizer(command) with no further modification of the character categories. The tokens produced by the tokenizer are then placed in the new string array cmdarray, in the same order.

这意味着无论外部引号如何,第一个字符串都会被分成标记。使用 Runtime.exec(String [] cmdarray,String [] envp,File dir) 版本以避免可执行路径的标记化。

This means the first string is broken into tokens, regardless of the outer quotes. Use the Runtime.exec(String[] cmdarray, String[] envp, File dir) version to avoid the tokenization of the executable path.

或者,使用 ProcessBuilder

Or, use ProcessBuilder:

File d = new File("C:/Program Files (x86)/ASL/_ASL Software Suite_installation");
ProcessBuilder pb = new ProcessBuilder(d.getAbsolutePath() + "/main.exe");
Process p = pb.directory(d)
              .start();

参见:

  • Why should avoid using Runtime.exec() in java?
  • ProcessBuilder vs Runtime.exec()

这篇关于传递给执行官的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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