How can I pass C prog output to parent java program

N

Nishi Bhonsle

Hi:

I am calling a C program inside a java prog. The C program gets the available disk space on the system.
How can I pass the available disk space( in this case a variable of type long) returned by the C program to the parent java program?

ie I am using
Runtime.getRuntime().exec(command) to calling the C program where command is the "C program file object file"

Thanks.
 
S

Steve W. Jackson

Nishi Bhonsle said:
:Hi:
:
:I am calling a C program inside a java prog. The C program gets the available
:disk space on the system.
:How can I pass the available disk space( in this case a variable of type
:long) returned by the C program to the parent java program?
:
:ie I am using
:Runtime.getRuntime().exec(command) to calling the C program where command is
:the "C program file object file"
:
:Thanks.
:

Did you look at the API docs yet? See the java.lang.Process class,
which is what's returned by the exec() call. You'll note there that
it's possible to "get" its output stream (stdout). You should probably
expect a timing issue, since you have to exec() the program and then
make this call against the returned Process instance. But the C program
can simply printf() its output. Of course, there are other ways...you
could have your program listen on a Socket, for instance, and the C
program can connect, send its info, and then disconnect and quit. There
are numerous alternatives.

= Steve =
 
T

Tony Morris

As mentioned, there are several alternatives, all of which supercede your
suggested one in robustness.
I'd recommend you take the "socket approach" or investigate JNI.

--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)
 
N

nos

Nishi Bhonsle said:
Hi:

I am calling a C program inside a java prog. The C program gets the
available disk space on the system.
How can I pass the available disk space( in this case a variable of type
long) returned by the C program to the parent java program?
ie I am using
Runtime.getRuntime().exec(command) to calling the C program where command
is the "C program file object file"
I did this with NJI and a 'c' program that
returns a float -- imagine that -- but it works
great on MS WIN.
---
/* 3:00 PM 06/20/2003 */
#include <jni.h>
#include "GetDiskDemo.h"
#include <stdio.h>
#include <windows.h>
#include <direct.h>

JNIEXPORT jfloat JNICALL
Java_GetDiskDemo_getDiskFreeSpace(JNIEnv *env, jobject jc, jint disk_drive)
{
struct _diskfree_t df = {0};
unsigned uErr = -1;

// check validity
// drives start at 1=A:, 2=B:, 3=C: up to 26=Z:
if((disk_drive < 1)||(disk_drive > 26)) {
printf("invalid disk drive = %d\n",disk_drive);
return -1.0;
}
uErr = _getdiskfree(disk_drive, &df);
if (uErr != 0) {
printf("error return from _getdiskfree() for disk drive = %d\n",
disk_drive);
return -1.0;
}
return (float)df.avail_clusters * (float)df.sectors_per_cluster *
(float)df.bytes_per_sector;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top