469,645 Members | 1,216 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,645 developers. It's quick & easy.

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

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 '06 #1
11 2462
On 2006-07-21, jo*****@hotmail.com <jo*****@hotmail.comwrote:
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.
Jul 21 '06 #2
jo*****@hotmail.com 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
Jul 21 '06 #3
jo*****@hotmail.com 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
Jul 21 '06 #4
<jo*****@hotmail.comwrote
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
Jul 22 '06 #5
jacob navia <ja***@jacob.remcomp.frwrote:
jo*****@hotmail.com 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
Jul 24 '06 #6
Richard Bos wrote:
jacob navia <ja***@jacob.remcomp.frwrote:

>>jo*****@hotmail.com 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...

Jul 24 '06 #7
On Mon, 24 Jul 2006 20:46:56 +0200, in comp.lang.c , jacob navia
<ja***@jacob.remcomp.frwrote:
>Richard Bos wrote:
>jacob navia <ja***@jacob.remcomp.frwrote:

>>>jo*****@hotmail.com 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
Jul 24 '06 #8
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.
Jul 25 '06 #9
jacob navia <ja***@jacob.remcomp.frwrites:
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) ks***@mib.org <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.
Jul 25 '06 #10
jacob navia <ja***@jacob.remcomp.frwrote:
Richard Bos wrote:
jacob navia <ja***@jacob.remcomp.frwrote:
>jo*****@hotmail.com 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
Jul 25 '06 #11
On Tue, 25 Jul 2006 08:20:45 +0200, in comp.lang.c , jacob navia
<ja***@jacob.remcomp.frwrote:
>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
Jul 25 '06 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Nicolas Fleury | last post: by
15 posts views Thread by Sokar | last post: by
6 posts views Thread by jsw_nz | last post: by
1 post views Thread by darren kirby | last post: by
3 posts views Thread by pavi | last post: by
1 post views Thread by roocell | last post: by
8 posts views Thread by jkeith07 | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.