Redirect stdout from java to c

M

Mazze

Hi.

I have a strange problem ... I want to create a process and capture stdout :

...
gint std_out;
char* argv[3] = {"/opt/sun-jdk-1.4.2.08/bin/java","helloworld",NULL};
//char* argv[3] = {"/usr/bin/du","/",NULL};
g_spawn_async_with_pipes("/work/", argv, NULL,
G_SPAWN_CHILD_INHERITS_STDIN,
NULL, NULL, NULL,
NULL, &std_out,NULL,
NULL);

gio_stdout = g_io_channel_unix_new(std_out);
g_io_channel_set_flags(gio_stdout, G_IO_FLAG_NONBLOCK, NULL);
g_io_add_watch(gio_stdout, G_IO_IN, printme, (void*)user_data);
...

In the case of a "normal" program , e.g. "/usr/bin/du" or anything else,
this works fine, and I can read the programs stdout.
But using the java program i dont get anything at all... !?
helloworld.java just prints "Hello world" using System.out.println.

Can anybody tell me why i cant capture the output of a java program, and
how to fix this ?

Thanks
 
Z

Zara

Mazze said:
(...)
But using the java program i dont get anything at all... !?
helloworld.java just prints "Hello world" using System.out.println.

Can anybody tell me why i cant capture the output of a java program, and
how to fix this ?

Thanks

<OT>

Java prints to java console. This question should be answered by Java
people.
 
I

Irrwahn Grausewitz

Mazze said:
Hi.

I have a strange problem ... I want to create a process and capture stdout :

...
gint std_out;
char* argv[3] = {"/opt/sun-jdk-1.4.2.08/bin/java","helloworld",NULL};
//char* argv[3] = {"/usr/bin/du","/",NULL};
g_spawn_async_with_pipes("/work/", argv, NULL,
G_SPAWN_CHILD_INHERITS_STDIN,
NULL, NULL, NULL,
NULL, &std_out,NULL,
NULL);

gio_stdout = g_io_channel_unix_new(std_out);
g_io_channel_set_flags(gio_stdout, G_IO_FLAG_NONBLOCK, NULL);
g_io_add_watch(gio_stdout, G_IO_IN, printme, (void*)user_data);
...

In the case of a "normal" program , e.g. "/usr/bin/du" or anything else,
this works fine, and I can read the programs stdout.
But using the java program i dont get anything at all... !?
helloworld.java just prints "Hello world" using System.out.println.

Can anybody tell me why i cant capture the output of a java program, and
how to fix this ?

Since literally none of the functions your code calls is standard C,
you won't get a sensible answer in this group. You should try in a
group that deals with Unix programming or Java programming.

Best regards
 
N

Novitas

Actually, I do have a "serious answer."

One thing you might want to consider is the Java Native Interface (JNI)
binding. (I don't know for sure whether this applies to your
application or not.)

JNI provides an interface between Java and the C programming
environments permitting the mixing of C and Java in a single
application.
 
A

ajm

Mazze,

what you are observing here has to do with Java (not C) and how it
handles stdio (the same problem is also encountered if you invoke
processes from within Java and try to capture their output).

best not try to address this here so suggest you take your question to
a Java group (or offline) where you can find out about flushing the
stdio ;)

hth,
ajm.
 
V

Villy Kruse

Java prints to java console. This question should be answered by Java
people.

Java prints to stdout just like any other program. The way to capture this
is independent of wheter the program is done in java or C. It does, however,
depend on some function not defined in the C standard.


Villy
 

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,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top