Use ReturnValue from C Program in Java Program !!!

Hi:

I want to calculate n! in a C program. This program expects an int as argument and returns result as a long. I want to start this C program from java. I have to transfer argument to the program and expect return valu, which I want to show on the screen in my java programm. Here is my C-Program:

#include <stdlib.h>

#include <stdio.h>

long f(int n)

{

if (n<1)

return 1;

else

return n * f(n-1);

}

int main(int argcnt, char* argc[])

{

if (argcnt < 2)

return -1;

else

{

int n = atoi(argc[1]);

if (n<0)

return -1;

else

return f(n);

}

}

This program works fine in C. Now, I want to do followng:

// Call this program

Process p = Runtime.getRuntime().exec("C:\\Temp\\calcfak.exe, 5");

// C-Program calculates 5! = 120 and returns this. How to get this value ?

Thanks for any help.

[1776 byte] By [pitalica] at [2007-9-26 4:05:20]
# 1

Hi! Look at the docs for the java.lang.Runtime class. You'll notice that the exec() method returns a Process object. Look at the docs for the java.lang.Process object. You'll notice that you can access the input and output streams of this Process object and use those to communicate input values to the process as well as get the output from the process. That should do the trick for you.

Hope this helps!

Cheers!

amolk at 2007-6-29 13:04:39 > top of Java-index,Archived Forums,Java Programming...
# 2

Hi,

my suggestion:Process p = Runtime.getRuntime().exec("C:\\Temp\\calcfak.exe 5");

p.waitFor();

int ret = p.exitValue();

Java int is 32 bits. It will be the same range as C long (depends on your machine however).

Regards,

Martin

edosoft at 2007-6-29 13:04:39 > top of Java-index,Archived Forums,Java Programming...
# 3
Er, I don't think exitValue() will get the original poster what they want. If a process terminates normally, then exitValue() returns zero. That's probably not a desirable outcome.Cheers!
amolk at 2007-6-29 13:04:39 > top of Java-index,Archived Forums,Java Programming...
# 4
Thanks. Unhappily, it doesn't work. I become zero back. Any other idea?
pitalica at 2007-6-29 13:04:39 > top of Java-index,Archived Forums,Java Programming...
# 5

Reply to amolk.

In C the return x from main is equivalent to exit(x).

And the C program of the original poster use x to return the function value (or -1 on error). So it may be read with p.exitValue(). The zero value for success is only a convention and this program don't use it.

Regards,

Martin

edosoft at 2007-6-29 13:04:39 > top of Java-index,Archived Forums,Java Programming...
# 6

Hi Petar! The i/o streams associated with the java.lang.Process class deal w/ the standard i/o streams. So, if your C program prints the computed value to the standard output, you can acccess that using the mechanism I described earlier.

If you do run into difficulties, could you possibly post some code so folks here can take a look at it?

Cheers!

amolk at 2007-6-29 13:04:39 > top of Java-index,Archived Forums,Java Programming...
# 7

Hi Petar! Sorry, forgot one point. You said you wanted to invoke the C program as follows.

Process p = Runtime.getRuntime().exec("C:\\Temp\\calcfak.exe, 5");

If your C program is going to read the input as a command line argument, then you need to remove the comma in the argument to exec().

Hope this helps!

Cheers!

amolk at 2007-6-29 13:04:39 > top of Java-index,Archived Forums,Java Programming...
# 8
>Thanks. Unhappily, it doesn't work. I become zero back. Any other idea?Yes. Your C program should use a long type as follows:long main(int argcnt, char* argc[])BTW: why does you do such strange things? Why not using Java only?Regards,Martin
edosoft at 2007-6-29 13:04:39 > top of Java-index,Archived Forums,Java Programming...
# 9
Thanks for your replay.I tryed with long type, but it doesn't work too. >BTW: why does you do such strange things? Why not using Java only?Nun, I want to know, is it posibile and if yes, hot ist to do it. Any other idea?
pitalica at 2007-6-29 13:04:39 > top of Java-index,Archived Forums,Java Programming...