java - 将路径传递给 exec

标签 java

我正在尝试使用 exec 函数。可执行文件的路径包含空格,这让我很伤心 我的代码如下所示

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\""));

执行此操作时,我收到异常 -

Cannot run program ""c:\Program" 

如果有人能给我一些帮助来解决这个问题,我将不胜感激

提前致谢

最佳答案

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

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

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.

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)版本以避免可执行路径的标记化。

或者,使用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();

参见:

关于java - 将路径传递给 exec,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44647639/

相关文章:

java - 线程 "main"java.lang.NoClassDefFoundError : edu/stanford/nlp/pipeline/StanfordCoreNLP 中出现异常

java - 如何将 ImageView 移至顶部?

MySql 连接中的 Java ClassNotFoundException (java.sql.SQLType)

java - 需要统计自然语言处理的资源

java - 我可以使用什么库来解析 Java 中的 Schedule String

java - 从 JTextArea 获取输入

java - ORA-29977 : Unsupported column type for query registration in guaranteed mode

java - 为什么 System.out.println(map.put(1 ,"test")) 打印 null 值?

java - 如何将 ResulSet 的多个值放入 Java 对象中

java - 当我拥有所有 Joda DateTime 对象时,如何使用 SWT 日历?