0

I am unable to run c executable file from a java program in ubuntu.

I created executable file using following command :

gcc ex.c -o process

In java program i tried everything i could find but no result.

Runtime.getRuntime().exec("/home/cori/Desktop/process.exe);

another method

Runtime rt = Runtime.getRuntime();
Process proc = rt.exec("process");

Please suggest.

Manav Singhal
  • 183
  • 5
  • 22
  • 1
    do you get any exceptions? did you read out the output? http://stackoverflow.com/questions/4741878/redirect-runtime-getruntime-exec-output-with-system-setout – deterministicFail Dec 05 '13 at 13:21
  • here in this example process is an executable which reads from a text file and creates a jpg file. When i run it by double clicking on it , it works fine but not with java code – Manav Singhal Dec 05 '13 at 13:27
  • no exceptions are getting thrown. – Manav Singhal Dec 05 '13 at 13:32
  • thats realy nice, but this isnt helping. With this informations i can just suggest something like you dont have the rights to access the desktop of the user. You have to provide something more like an exception or the output of your runtime – deterministicFail Dec 05 '13 at 13:32
  • There is no output for this , the only output that i am supposed to get is an image file on my desktop. I have rights. – Manav Singhal Dec 05 '13 at 13:38
  • Are you calling `process.exe` or `process`? UNIX-like systems like GNU/Linux do not add any file extensions like '.exe' to executable files. –  Dec 05 '13 at 13:43
  • You called your program `process` not `process.exe` – Peter Lawrey Dec 05 '13 at 13:44
  • I called it as process , but still no output. – Manav Singhal Dec 05 '13 at 14:06

2 Answers2

0

*nix like systems doesn't uses the *.exe postfix for executable files.

Runtime.getRuntime().exec("/home/cori/Desktop/process")

should work fine. But I still don't get what result you want to get. The only things you can get from a Process class is few streams, and its return status. Which for you c program should be 0. (if you returned 0 end the end of your source code).

And: The 2 methods you mentioned are the same. Just the first uses something call Method chaining :)

Ganesh Gaxy
  • 657
  • 5
  • 11
MB.One
  • 110
  • 1
  • 2
  • I tried without using .exe . no output . My executable will read data from a file and convert it into a jpg format file. I just want to use java code , so that on press of a button this executable can be called and a jpg image will be created on desktop . – Manav Singhal Dec 05 '13 at 13:46
0

I am assuming you have access to the C code because I see you trying to compile it.

Have you considered using Java Native Inteface? Java allows you to call non-Java code, usually referred to as native code.

You have to follow a sequence of steps to get it work. See an example here http://www3.ntu.edu.sg/home/ehchua/programming/java/JavaNativeInterface.html

  1. Create a Java class with native method
  2. Load the library which implements the method
  3. Invoke the native method from Java
  4. Use javah utility to generate the header file
  5. Create c code to implement the java stub method
  6. compile c program tp create .so or .dll (depending on OS)
  7. Set LD_LIBRARY_PATH
  8. Run the Java code

It is a long process and It takes some trial and error to get it right. But once you figure it out it is a really powerful tool. This is a very old feature of Java, so there are lots of resources online.

kensen john
  • 5,439
  • 5
  • 28
  • 36