在Java中包含空格的工作目录中执行外部可执行文件?

发布于 2024-10-05 23:56:44 字数 1282 浏览 7 评论 0原文

在 Os x 上执行程序期间,我有一个特定需要解压缩在不同子目录中找到的文件。我通过调用免费软件命令行工具 unrar 来完成此操作,该工具运行良好。 但是,unrar 始终会在当前工作目录中解压文件,这意味着我必须为执行的进程指定工作目录,或者获取 .jar 主文件夹中的每个解压文件。 使用 processBuilder.directory(dir) 或 runTime.getRuntime().exec(args,null,dir) 很容易做到这一点,例如 dir 是一个文件。 这工作得很好,但当工作目录包含空格时就不行了。举一个简短的例子:

File dir=new File("/Users/chargedPeptide/rar 2");
String[] cmd = { "/Users/chargedPeptide/rar/unrar", "e", "-o+","/Users/chargedPeptide/rar", "2/weather.rar"};
Process pr = Runtime.getRuntime().exec(cmd,null,dir);
int exitValue=pr.waitFor();

将不起作用,同时使用: Process pr = Runtime.getRuntime().exec(cmd); 相反,将成功启动命令,但将 jar 中的所有文件留给我工作目录。 使用 processbuilder 代替并使用 processbuilder.directory(dir); 设置目录 退出并显示消息:

Exception: Cannot run program "/Users/chargedPeptide/rar/unrar" (in directory "/Users/chargedPeptide/rar 2"): error=2, No such file or directory

帮助?我该如何处理这些空格?我尝试在空格之前添加反斜杠,以使它们字面上没有帮助,因为文件对象将它们视为路径的实际部分。

编辑:为了让整个事情更清楚一点: 1.我有一个单独的方法,它向执行方法提供命令和目录给processbuilder,所有目录都是由前面的方法找到并存在的。除非目录包含空格,否则此操作有效。 2.我需要设置工作目录,否则命令将在错误的位置执行。 3.解析文件对象: dir=新文件(dir.tostring.replace(" ","\ "); 在所有空格前面放置反斜杠是行不通的,因为 java File 对象随后会查找包含实际反斜杠的路径。运气不好。 4.使用 rt.exec 而不是 processbuilder 也没有帮助。

任何想法都非常受欢迎。

I have a specific need to unrar files found in different subdirectories during execution of my program on Os x. I do this by calling the freeware command line tool unrar, which works very well.
However, unrar will always unrar files in the current working directory, which means that I have to specify a working directory for the executed process or get every unpacked file in my .jar home folder.
This is easy enough to do using either the processBuilder.directory(dir) or runTime.getRuntime().exec(args,null,dir) for example where dir is a File.
This works excellent but not when the working directory contains a space. As a short example:

File dir=new File("/Users/chargedPeptide/rar 2");
String[] cmd = { "/Users/chargedPeptide/rar/unrar", "e", "-o+","/Users/chargedPeptide/rar", "2/weather.rar"};
Process pr = Runtime.getRuntime().exec(cmd,null,dir);
int exitValue=pr.waitFor();

Will not work, while using: Process pr = Runtime.getRuntime().exec(cmd); instead will launch the command successfully but leave me with all of the files in the jars working directory.
Using processbuilder instead and using processbuilder.directory(dir); to set the directory
exits with the message:

Exception: Cannot run program "/Users/chargedPeptide/rar/unrar" (in directory "/Users/chargedPeptide/rar 2"): error=2, No such file or directory

Help? How do I handle the spaces? I've tried adding backslashes before the spaces to make them literal no help since the File object treats them like actual part of the path.

Edit: To make the whole thing a bit more clear:
1. I have a separate method that feeds the execute method a command and a directory to processbuilder, all directories are found by the previous method and exist. This works except when the dir contains a space.
2.I need to set the working dir or the command will execute in the wrong place.
3.Parsing the file object by:
dir=new File(dir.tostring.replace(" ","\ ");
to put a backslash in front of all spaces does not work since the java File object then looks for a path containing actual backslashes. No luck.
4.Using rt.exec instead of processbuilder dosen't help either.

Any ideas most welcome.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七色彩虹 2024-10-12 23:56:44

怎么样:

dir.mkdirs();

在启动流程之前。

这将创建丢失的目录。

编辑

这看起来很奇怪。

String[] cmd = { "/Users/chargedPeptide/rar/unrar", "e", "-o+","/Users/chargedPeptide/rar", "2/weather.rar"};

如果这是一个 shell,您需要编写:

unrar e -o+ "/Users/chargedPeptide/rar 2/weather.rar"

您实际上必须将 .rar 文件放在引号中,否则它将被解释为进程的 2 个参数。

您拆分 'cmd' 的方式正是这样做的,将 rar 参数分成两部分。尝试:

String[] cmd = { "/Users/chargedPeptide/rar/unrar", "e", "-o+","/Users/chargedPeptide/rar 2/weather.rar"};

How about:

dir.mkdirs();

before launching the process.

This creates the missing directory.

Edit:

This looks strange.

String[] cmd = { "/Users/chargedPeptide/rar/unrar", "e", "-o+","/Users/chargedPeptide/rar", "2/weather.rar"};

If this was a shell you'd write:

unrar e -o+ "/Users/chargedPeptide/rar 2/weather.rar"

You actually have to put the .rar file in quotes, since otherwise it will be interpreted as 2 arguments to the process.

The way you've split 'cmd' will do exactly that, break the rar argument in two. Try:

String[] cmd = { "/Users/chargedPeptide/rar/unrar", "e", "-o+","/Users/chargedPeptide/rar 2/weather.rar"};
做个少女永远怀春 2024-10-12 23:56:44

不确定它是否有效,但是您可以尝试在路径末尾添加 / 吗?
IE,

File dir=new File("/Users/chargedPeptide/rar 2/");

Not sure if it will work, but can you try putting a / at the end of the path.
i.e.,

File dir=new File("/Users/chargedPeptide/rar 2/");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文