【问题标题】:Why does running MAKE from a Java application result in an error message?为什么从 Java 应用程序运行 MAKE 会导致错误消息?
【发布时间】:2018-08-15 01:31:39
【问题描述】:

我尝试从 Java 运行 Linux shell 命令。这些命令直接在 shell 上运行良好,但总是崩溃。
我有一个 makefile,我尝试通过 Java 运行 make。

我的 Java 代码:

String command = "make generate FILE=" + name ;
String [] envp = { } ;
File dir = new File ( System.getProperty("user.dir")) ;
Process proc = Runtime.getRuntime().exec(command,envp,dir);
proc.waitFor();

BufferedReader buf = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String line = "";
while ((line=buf.readLine())!=null) {
    System.out.println(line);
}

我的生成文件:

generate:
    llvm-as -f VSOP_Executable/$(FILE).ll -o $(FILE).bc
        llc $(FILE).bc
        gcc -c $(FILE).s -o $(FILE).o
        gcc $(FILE).o -o $(FILE) -no-pie

它总是在我尝试生成可执行文件的最后一行崩溃。
错误:

makefile:47: 目标“生成”的配方失败

【问题讨论】:

  • 我有一个 makefile,我尝试通过 java 运行 make。 为什么
  • 你检查过你的环境变量了吗? PATH、LDLIBRARY 等?
  • 其实就是这个问题,因为你设置环境为空,所以找不到llvm-as、llc、gcc,gcc也找不到链接。
  • Process::waitFor 如有必要,使当前线程等待,直到此 Process 对象表示的进程终止。 为什么您希望能够读取输入流终止的进程?我错过了什么吗?
  • @ElliottFrisch 制作文件已经制作完成。即使我尝试直接在代码中运行命令,而不使用 make 文件。同样的问题。最后一个命令不起作用。

标签: java linux shell


【解决方案1】:

解决了Paul Hicks在他的评论中写的。原因是清除所有环境变量。

我更改了代码并删除了空的envp。现在好像可以了。

String cmd = "make generateVSOP FILE=" + name;
Runtime run = Runtime.getRuntime();
Process pr = run.exec(cmd);
pr.waitFor();

BufferedReader buf = new BufferedReader(new InputStreamReader(pr.getInputStream()));
String line = "";
while ((line=buf.readLine())!=null) {
    System.out.println(line);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-27
    • 1970-01-01
    • 1970-01-01
    • 2020-09-10
    • 2014-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多