从Java程序C程序编译 [英] C program compilation from a java program

查看:126
本文介绍了从Java程序C程序编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从Linux平台上的Java程序编译C程序。我的片段是。

I am trying to compile a c program from a java program on Linux platform. My snippet is.

          ProcessBuilder processBuilder = new ProcessBuilder("/usr/bin/gcc",
          "-c","/hipad/UserProject/example.c");

          Process proc = processBuilder.start();

有是java程序的编译过程中没有错误,但我不能够得到的.o文件。我试图找出解决方案,但没有一个人在工作。
任何建议.....

There is no error during compilation of java program but I am not able to get .o file. I tried to find out solutions but no one is working. Any suggestion.....

推荐答案

默认的工作子进程的目录是什么都目录中的Java进程有一个工作目录,这通常是它从启动。并且默认的的gcc 的写入文件输出到当前工作目录。这就是你应该找到 example.o

The default working directory of a child process is what ever directory the Java process has as a working directory, which usually is where it was launched from. And by default gcc writes output files to current working directory. That's where you should find example.o.

有两种简单的方法来解决这个问题。你可以给的的gcc -o 选项和完整路径和所需的输出文件的名称,也可以设置工作子进程的目录,如下所示:

There are two simple ways to solve this. You can give gcc -o option and full path and name of desired output file, or you can set working directory of child process, like this:

ProcessBuilder processBuilder =
    new ProcessBuilder("/usr/bin/gcc", "-c","example.c"); // source in working dir
processBuilder.directory(new File ("/hipad/UserProject")); // or whatever
Process proc = processBuilder.start();

请参阅的ProcessBuilder的javadoc 了解更多信息

这篇关于从Java程序C程序编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆