Redirect stdout from java to c

Discussion in 'C Programming' started by Mazze, Sep 14, 2005.

  1. Mazze

    Mazze Guest

    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
    Mazze, Sep 14, 2005
    #1
    1. Advertising

  2. Mazze

    Zara Guest

    Mazze wrote:
    > Hi.
    >

    (...)
    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.
    Zara, Sep 14, 2005
    #2
    1. Advertising

  3. Mazze <> wrote:
    >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
    --
    Irrwahn Grausewitz ()
    welcome to clc : http://www.ungerhu.com/jxh/clc.welcome.txt
    clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
    clc frequent answers: http://benpfaff.org/writings/clc
    Irrwahn Grausewitz, Sep 14, 2005
    #3
  4. Mazze

    Mazze Guest

    Thank you, for answering.
    Mazze, Sep 14, 2005
    #4
  5. Mazze

    Novitas Guest

    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.
    Novitas, Sep 14, 2005
    #5
  6. Mazze

    ajm Guest

    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.
    ajm, Sep 14, 2005
    #6
  7. Mazze

    Villy Kruse Guest

    On Wed, 14 Sep 2005 11:00:16 GMT,
    Zara <> wrote:


    >
    > 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
    Villy Kruse, Sep 14, 2005
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VisionSet
    Replies:
    6
    Views:
    9,626
    Sudsy
    Sep 20, 2003
  2. Elad
    Replies:
    0
    Views:
    409
  3. Sal
    Replies:
    1
    Views:
    402
  4. Andreas S
    Replies:
    3
    Views:
    261
    Eric Hodel
    Dec 9, 2006
  5. Replies:
    2
    Views:
    329
    A. Sinan Unur
    Dec 7, 2005
Loading...

Share This Page