3

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();

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.....

8
  • 1
    Does it work fine when you compile the c program manually with the same options ? Commented Dec 9, 2013 at 5:05
  • the .o file probably appears in the current working directory, whatever that is. Commented Dec 9, 2013 at 5:06
  • I can see that you are using a *nix machine right? I've noticed that providing the entire file path for files in that will help with most situations..... Commented Dec 9, 2013 at 5:16
  • 1
    Why do you want to do that? >_> Commented Dec 9, 2013 at 5:17
  • 2
    @Staven What's it matter? I can think of many reasons ... perhaps the OP is developing a C IDE in Java. Commented Dec 9, 2013 at 5:29

1 Answer 1

3

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.

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();

See ProcessBuilder javadoc for more info.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.