我想通过 java 中的 cmd 执行 git 命令,并想验证收到的整个输出。预期的:
克隆到“gitrepo”...
远程:计数对象:92,完成
远程:查找来源:100% (92/92)
远程:获取大小:100% (76/76)
远程:压缩对象:98% (2370/2400)
远程:总计 92(增量 14),重复使用 89(增量 14)
拆包对象:100% (92/92),完成。
检查连接...完成。
尝试 No.1 - ProcessBuilder
List<String> commands = new ArrayList<String>();
commands.add("cmd.exe");
commands.add("/k");
commands.add("cd D:/" + " && D: && git clone --depth 1 <url>");
ProcessBuilder pb = new ProcessBuilder(commands);
pb.redirectErrorStream(true);
Process prs = pb.start();
prs.waitFor();
BufferedReader input = new BufferedReader(new InputStreamReader(prs.getInputStream()));
String line = null;
while((line = input.readLine())!= null) {
output = output + line;
System.out.println(line);
}
System.out.println(output);
// final String fileAsText = input.lines().collect(Collectors.joining());
//System.out.println(fileAsText);
input.close();
结果:在 While 循环中挂起
尝试 2 - 新线程中的扫描仪
ProcessBuilder proc = new ProcessBuilder(commands);
proc. redirectErrorStream(true);
proc.redirectOutput();
final Process p = proc.start();//Runtime.getRuntime().exec(cur_string);
//Handle streams
//in
new Thread(new Runnable(){
public void run(){
Scanner stdin = new Scanner(p.getInputStream());
while(stdin.hasNextLine()){
System.out.println(stdin.nextLine());
if (stdin.nextLine().isEmpty())
break;
}
System.out.println("exit");
stdin.close();
}
}).start();
//wait
p.waitFor();
p.destroyForcibly();
结果 - 在 while 循环中挂起
尝试 No. 3 - Runnable 类
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
List<String> commands = new ArrayList<String>();
commands.add("cmd.exe");
commands.add("/k");
commands.add("cd D:/" + " && D: && git clone --depth 1 <url>");
ProcessBuilder pb = new ProcessBuilder(commands);
pb.redirectErrorStream(true);
try {
Process prs = pb.start();
Thread inThread = new Thread(new In(prs.getInputStream()));
inThread.start();
Thread.sleep(2000);
} catch (IOException e) {
e.printStackTrace();
}
}
class In implements Runnable {
private InputStream is;
public In(InputStream is) {
this.is = is;
}
@Override
public void run() {
byte[] b = new byte[1024];
int size = 0;
try {
while ((size = is.read(b)) != -1) {
System.out.println(new String(b));
}
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
结果——永不返回
尝试第 4 项 - org.apache.commons.fileupload.utils.stream
final Process _p = Runtime.getRuntime().exec("cmd.exe /k cd \"D:\\" && D: && git clone --depth 1 <url>");
// Handle stdout...
new Thread() {
public void run() {
try {
Streams.copy(_p.getInputStream(), System.out,true);
} catch (Exception anExc) {
anExc.printStackTrace();
}
}
}.start();
// Handle stderr...
new Thread() {
public void run() {
try {
Streams.copy(_p.getInputStream(), System.out,true);
} catch (Exception anExc) {
anExc.printStackTrace();
}
}
}.start();
_p.waitFor();
结果——永远挂起
尝试 5 号——Crydust/Git.java
https://gist.github.com/Crydust/fd1b94afc52cd0f7dd4c
结果 - 仅返回第一条语句“克隆到 git.repo...”
尝试 6 号 --apache.commomns.executor (Executor Watch dog)
结果 - 挂起
尝试 7 号 - ztexec 库
String output = new ProcessExecutor().command("git", "clone","--depth","1","url")
.readOutput(true).directory(new File("D:/git")).execute()
.outputUTF8();
System.out.println(output);
结果 - 仅返回第一条语句“克隆到 git.repo...”
注意::: 不使用 jgit 库,因为需要浅克隆,而 jgit 尚不支持。