calling "java from C dll" (system command??)

Discussion in 'C Programming' started by jobs239@hotmail.com, Jul 21, 2006.

  1. Guest

    Can I use this line inside C program "system(java -jar <jarfilename>)"
    to run a java program from C?

    Or do I have to use some JNI interface.?
     
    , Jul 21, 2006
    #1
    1. Advertising

  2. Flash Gordon Guest

    wrote:
    > Can I use this line inside C program "system(java -jar <jarfilename>)"
    > to run a java program from C?


    You can, but whether it works depends on things outside the C language.

    > Or do I have to use some JNI interface.?


    You may be able to do that as well.

    The best solution will depend on what you are trying to achieve, and you
    would be better off discussing it in a group dedicated to your
    implementation or, depending on what they consider topical, a Java group
    than on comp.lang.c (I've no idea about comp.lang.java.programmer).

    Follow-ups set, since solving the OPs problem will involve things beyond
    the scope of standard C and so is off topic for comp.lang.c
    --
    Flash Gordon, living in interesting times.
    Web site - http://home.flash-gordon.me.uk/
    comp.lang.c posting guidelines and intro:
    http://clc-wiki.net/wiki/Intro_to_clc
     
    Flash Gordon, Jul 21, 2006
    #2
    1. Advertising

  3. On 2006-07-21, <> wrote:
    > Can I use this line inside C program "system(java -jar <jarfilename>)"
    > to run a java program from C?
    >


    No. That's a either a string literal or a syntax error, depending on
    whether you preserve your quotes.

    > Or do I have to use some JNI interface.?
    >


    I don't know. Unfortunately, I can't think of who /would/ know, so I
    can't redirect you.
     
    Andrew Poelstra, Jul 21, 2006
    #3
  4. jacob navia Guest

    wrote:
    > Can I use this line inside C program "system(java -jar <jarfilename>)"
    > to run a java program from C?
    >
    > Or do I have to use some JNI interface.?
    >


    There is a very simple way to verify this:

    1) open a command shell
    2) type in that command shell
    java -jar filename

    If that works, then the C function call
    system("java -jar filename");

    will work too. Note the correct quotes, and not
    as you typed...

    jacob
     
    jacob navia, Jul 21, 2006
    #4
  5. Malcolm Guest

    <> wrote
    > Can I use this line inside C program "system(java -jar <jarfilename>)"
    > to run a java program from C?
    >
    > Or do I have to use some JNI interface.?
    >

    System is the portable, ANSI C way of doing it.
    Usually portable, ANSI methods are best, but "system" is often an exception.
    There might be a better way that doesn't rely on your user having all his
    paths set up correctly.
    --
    Buy my book 12 Common Atheist Arguments (refuted)
    $1.25 download or $7.20 paper, available www.lulu.com/bgy1mm
     
    Malcolm, Jul 22, 2006
    #5
  6. Richard Bos Guest

    jacob navia <> wrote:

    > wrote:
    > > Can I use this line inside C program "system(java -jar <jarfilename>)"
    > > to run a java program from C?

    >
    > There is a very simple way to verify this:
    >
    > 1) open a command shell
    > 2) type in that command shell
    > java -jar filename
    >
    > If that works, then the C function call
    > system("java -jar filename");
    >
    > will work too.


    This is far from guaranteed. It's a start; but it's not enough for
    certainty.

    > Note the correct quotes, and not as you typed...


    True.

    Richard
     
    Richard Bos, Jul 24, 2006
    #6
  7. jacob navia Guest

    Richard Bos wrote:
    > jacob navia <> wrote:
    >
    >
    >> wrote:
    >>
    >>>Can I use this line inside C program "system(java -jar <jarfilename>)"
    >>>to run a java program from C?

    >>
    >>There is a very simple way to verify this:
    >>
    >>1) open a command shell
    >>2) type in that command shell
    >> java -jar filename
    >>
    >>If that works, then the C function call
    >> system("java -jar filename");
    >>
    >>will work too.

    >
    >
    > This is far from guaranteed. It's a start; but it's not enough for
    > certainty.
    >
    >
    >>Note the correct quotes, and not as you typed...

    >
    >
    > True.
    >
    > Richard


    The standard says about the system function:
    < quote>
    If string is a null pointer, the system function determines whether the
    host environment has a command processor. If string is not a null
    pointer, the system function passes the string pointed to by string to
    that command processor to be executed in a manner which the
    implementation shall document
    < end quote >

    I do not see how that method above could fail, given that java is
    installed in the machine and is in the path...
     
    jacob navia, Jul 24, 2006
    #7
  8. On Mon, 24 Jul 2006 20:46:56 +0200, in comp.lang.c , jacob navia
    <> wrote:

    >Richard Bos wrote:
    >> jacob navia <> wrote:
    >>
    >>
    >>> wrote:
    >>>
    >>>>Can I use this line inside C program "system(java -jar <jarfilename>)"
    >>>>to run a java program from C?
    >>>
    >>>There is a very simple way to verify this:
    >>>
    >>>1) open a command shell
    >>>2) type in that command shell
    >>> java -jar filename
    >>>
    >>>If that works, then the C function call
    >>> system("java -jar filename");
    >>>
    >>>will work too.

    >>
    >>
    >> This is far from guaranteed. It's a start; but it's not enough for
    >> certainty.
    >>
    >>
    >>>Note the correct quotes, and not as you typed...

    >>
    >>
    >> True.
    >>
    >> Richard

    >
    >The standard says about the system function:
    >< quote>
    >If string is a null pointer, the system function determines whether the
    >host environment has a command processor. If string is not a null
    >pointer, the system function passes the string pointed to by string to
    >that command processor to be executed in a manner which the
    >implementation shall document
    >< end quote >
    >
    >I do not see how that method above could fail, given that java is
    >installed in the machine and is in the path...


    For all sorts of reasons which are offtopic here, such as environment
    inheritance / cloning, permissions, security configuration, etc etc.

    --
    Mark McIntyre

    "Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are,
    by definition, not smart enough to debug it."
    --Brian Kernighan
     
    Mark McIntyre, Jul 24, 2006
    #8
  9. jacob navia Guest

    Mark McIntyre wrote:
    >>
    >>The standard says about the system function:
    >>< quote>
    >>If string is a null pointer, the system function determines whether the
    >>host environment has a command processor. If string is not a null
    >>pointer, the system function passes the string pointed to by string to
    >>that command processor to be executed in a manner which the
    >>implementation shall document
    >>< end quote >
    >>
    >>I do not see how that method above could fail, given that java is
    >>installed in the machine and is in the path...

    >
    >
    > For all sorts of reasons which are offtopic here, such as environment
    > inheritance / cloning, permissions, security configuration, etc etc.
    >


    Those reasons would make it fail within the command shell also!
    If you have the wrong permissions, for instance, it should
    not work in the command shell!!!

    What I want to say is that IF the command works within a
    command shell, it MUST work within the system() function,
    unless the user or the program is impersonating somebody else.
     
    jacob navia, Jul 25, 2006
    #9
  10. jacob navia <> writes:
    > Mark McIntyre wrote:

    [...]
    [attribution lost]
    >>> I do not see how that method above could fail, given that java is
    >>> installed in the machine and is in the path...

    >> For all sorts of reasons which are offtopic here, such as
    >> environment
    >> inheritance / cloning, permissions, security configuration, etc etc.

    >
    > Those reasons would make it fail within the command shell also!
    > If you have the wrong permissions, for instance, it should
    > not work in the command shell!!!
    >
    > What I want to say is that IF the command works within a
    > command shell, it MUST work within the system() function,
    > unless the user or the program is impersonating somebody else.


    Not correct. I can think of a number of other reasons why a system()
    command might fail even if the same command string works in a command
    shell.

    <OT>
    Just a few examples (many of these are Unix-specific):

    The user's default shell could be different from the one used by
    system(). The program's environment might be different from the
    user's environment, perhaps because the program changed it, perhaps
    for some other reason. The program might be executed from a cron job,
    which has a limited default environment, including a relatively short
    $PATH. The command might be an alias or shell function, which would
    be invisible to system(). The command might be setuid, i.e.,
    impersonating someone else, a possibility you acknowledged above, but
    not in your previous followup.
    </OT>

    I presume there are similar possibilities for other systems.

    The point is that the standard makes very few guarantees about how
    system() behaves, and assuming, perhaps based on your own experience,
    that there are additional guarantees in real life is unwise.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Jul 25, 2006
    #10
  11. Richard Bos Guest

    jacob navia <> wrote:

    > Richard Bos wrote:
    > > jacob navia <> wrote:
    > >
    > >> wrote:
    > >>
    > >>>Can I use this line inside C program "system(java -jar <jarfilename>)"
    > >>>to run a java program from C?
    > >>
    > >>There is a very simple way to verify this:
    > >>
    > >>1) open a command shell
    > >>2) type in that command shell
    > >> java -jar filename
    > >>
    > >>If that works, then the C function call
    > >> system("java -jar filename");
    > >>
    > >>will work too.

    > >
    > > This is far from guaranteed. It's a start; but it's not enough for
    > > certainty.


    > The standard says about the system function:
    > < quote>
    > If string is a null pointer, the system function determines whether the
    > host environment has a command processor. If string is not a null
    > pointer, the system function passes the string pointed to by string to
    > that command processor to be executed in a manner which the
    > implementation shall document
    > < end quote >
    >
    > I do not see how that method above could fail, given that java is
    > installed in the machine and is in the path...


    I can see how a naive user could think so, but a programmer really
    should have more experience of how expectations can go wrong. Your
    reasoning might work with a particularly unwarrantedly literal
    interpretation of that quotation, but in the real world, which you
    yourself are so fond of invoking, things are far from that simple.

    FWIW, everything Mark and Keith mentioned, and more, can go wrong; and
    what's more, not one of them goes against the ISO C Standard. And FYI, I
    have actually encountered, /mutatis mutandis/, one of the catches in
    Keith's list; so we're not talking about theoretical situations here.

    Richard
     
    Richard Bos, Jul 25, 2006
    #11
  12. On Tue, 25 Jul 2006 08:20:45 +0200, in comp.lang.c , jacob navia
    <> wrote:

    >Mark McIntyre wrote:


    (of the possibility that a command might work in a shell but fail when
    exec'ed via system).
    >>
    >> For all sorts of reasons which are offtopic here, such as environment
    >> inheritance / cloning, permissions, security configuration, etc etc.

    >
    >Those reasons would make it fail within the command shell also!
    >If you have the wrong permissions, for instance, it should
    >not work in the command shell!!!


    You're wrong, and apparently have no experience of .net to boot. Its
    even possible to demonstrate this problem with Excel, of all things.

    >What I want to say is that IF the command works within a
    >command shell, it MUST work within the system() function,
    >unless the user or the program is impersonating somebody else.


    Then you are still mistaken.

    This is however wildly offtopic here. If you want some ontopic ones,
    consider that the system command is not required to actually do
    anything.
    --
    Mark McIntyre

    "Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are,
    by definition, not smart enough to debug it."
    --Brian Kernighan
     
    Mark McIntyre, Jul 25, 2006
    #12
    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. Elhanan
    Replies:
    6
    Views:
    655
    Owen Jacobson
    Mar 5, 2006
  2. Replies:
    5
    Views:
    706
    Richard Bos
    Jul 24, 2006
  3. Irmen de Jong
    Replies:
    2
    Views:
    449
    =?ISO-8859-1?Q?Gerhard_H=E4ring?=
    Aug 12, 2003
  4. boriq
    Replies:
    4
    Views:
    627
    Tim Golden
    Jun 12, 2008
  5. Gary Perkins

    calling system command as another user

    Gary Perkins, Jul 15, 2003, in forum: Perl Misc
    Replies:
    4
    Views:
    136
Loading...

Share This Page