Java 中的进程构建器 [英] ProcessBuilder in Java
问题描述
我正在本页研究这个主题,但找不到解决方案,如何使用 ProcessBuilder 在 cmd 中执行程序?我想使用命令行压缩、打开、删除带有 7zip 的文件.我将此代码作为示例,但不起作用,cmd 启动但不创建压缩文件:
I´m studing about this subject in this page, but I can´t find the solution, how can I execute programs in cmd using ProcessBuilder? I want to compress, open, delete a file with 7zip using the command line. I have this code as example, but doesn´t work, cmd starts but doesn´t create the compress file:
String[] commands=
{"cmd.exe", "7za", "a", "-t7z", "test.7z", "Test.txt", "/C", "start"};
ProcessBuilder pb = new ProcessBuilder(commands);
推荐答案
问题是Java有点问题(start()缺失),7za有点问题(命令行乱码)
The problem is a little bit in Java (start() missing), a little bit in 7za (command line garbled)
new ProcessBuilder("7za", "a", "-t7z", "test.7z", "Test.txt").start();
此命令仅当 7za 在您的 PATH 中时才有效...
This command works only if 7za is in your PATH...
如果不是你必须指定它的路径,在Windows下:
If not you have to specify its path, under Windows:
new ProcessBuilder(
"C:/Program Files/7-Zip/7za.exe", "a", "-t7z", "test.7z", "Test.txt").start();
七个 zip 帮助:
7-Zip (A) 9.20 Copyright (c) 1999-2010 Igor Pavlov 2010-11-18
Usage: 7za <command> [<switches>...] <archive_name> [<file_names>...]
[<@listfiles...>]
<Commands>
a: Add files to archive
...
<Switches>
...
-t{Type}: Set type of archive
这篇关于Java 中的进程构建器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!