gpt4 book ai didi

java - 从java执行make命令来编译其他程序

转载 作者:行者123 更新时间:2023-12-01 15:00:01 24 4
gpt4 key购买 nike

Possible Duplicate:
How to use javas Process.waitFor()?

我有一个程序要上传到我的电脑中的 Arduino。我创建了一个 makefile 用于编译并将其上传到 arduino fio。

效果很好。现在我正在尝试创建一个 java 程序来执行该 makefile,但我对这个命令有问题:

Runtime.getRuntime().exec("make");

当我执行它时,程序被阻止。

我应该做什么来执行该订单?对于其他订单,它工作正常。使干净,使依赖......

我正在尝试使用我在互联网上找到的代码来完成该任务:

public static void prueba() throws InterruptedException, IOException {

Process proc = Runtime.getRuntime().exec("make");
int size;
String s;
int exCode = proc.waitFor();
StringBuffer ret = new StringBuffer();



while((size = proc.getInputStream().available()) != 0) {
byte[] b = new byte[size];
proc.getInputStream().read(b);
s = new String(b);
ret.append(s);
}
System.out.println(ret.toString());
}

最佳答案

在调用process.waitFor()之前,您需要在单独的线程中读取输入流(stdout)和错误流(stderr),以防止阻塞。

这篇文章必读:"When Runtime.exec() won't"并向您展示如何正确使用 Runtime.exec

关于java - 从java执行make命令来编译其他程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13797435/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com