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

J

jobs239

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.?
 
F

Flash Gordon

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
 
A

Andrew Poelstra

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.
 
J

jacob navia

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
 
M

Malcolm

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.
 
R

Richard Bos

jacob navia said:
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
 
J

jacob navia

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




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...
 
M

Mark McIntyre

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
 
J

jacob navia

Mark said:
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.
 
K

Keith Thompson

jacob navia said:
Mark McIntyre wrote:
[...]
[attribution lost]
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.
 
R

Richard Bos

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
 
M

Mark McIntyre

Mark McIntyre wrote:

(of the possibility that a command might work in a shell but fail when
exec'ed via system).
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
 

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,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top