loading a system call failed with JNI

L

Lancelot

I had wrote a c program like below

int ret = system("ping -c 1 1.2.3.4");

the return value (0) was correct when ran just as a c program,
but when I worte it as a JNI program to called by Java functions,
the return value was not correct , it always return the value (-1),
what's wrong with this program ? Can java do this ?

thx.....
 
G

Gordon Beaton

I had wrote a c program like below

int ret = system("ping -c 1 1.2.3.4");

the return value (0) was correct when ran just as a c program,
but when I worte it as a JNI program to called by Java functions,
the return value was not correct , it always return the value (-1),
what's wrong with this program ? Can java do this ?

Yes, you can do something like this from Java, with or without JNI.
Have a look at Runtime.exec() for example.

If you want help with your code, post the one that failed, not the one
that worked.

/gordon
 
L

Lancelot

Gordon Beaton said:
Yes, you can do something like this from Java, with or without JNI.
Have a look at Runtime.exec() for example.

If you want help with your code, post the one that failed, not the one
that worked.

/gordon

I knew that can be do by exec() routine, but when I using exec() the
return value always be 0 even if the ip don't assign to any machine on
the lan.
I need to get the status of ping program. The return value of system()
will tell me about the ping program's status. 0 was found the ip on
lan, other was failed. That's why I used system() instead of
exec().It's worked in C.
I want to know that can the system() do normal in Java by JNI model.
Because I always get the wrong return value.
 
P

Phil...

I have only used JNI a couple of times. My understanding
is that you end up calling a "C" program in the native environment.
The "C" program might have to be different for each operating
system you wish to run on.
The value returned by this "C" program is passed back to
the java method that invoked the "C" program via JNI.
For me I pass back a double and it works fine. For you,
you would have to have the "C" program invoke the "system"
call, get the return from the system call and then return that
value to the java method; and it should work fine.

Phil...
 
G

Gordon Beaton

I knew that can be do by exec() routine, but when I using exec() the
return value always be 0 even if the ip don't assign to any machine
on the lan.

Well perhaps the reason it isn't working is that you've done something
wrong. As I said in my earlier reply, post your code if you want help
with it.

At any rate, I have no problems getting the return value from ping
when I run it from Runtime.exec():

Process p = Runtime.getRuntime().exec("/bin/ping -c 1 -w 3 " + hostname);
p.waitFor();
int n = p.exitValue();

This works as expected, and I can see the difference between hosts
that are up, down and nonexistent.
I need to get the status of ping program. The return value of
system() will tell me about the ping program's status. 0 was found
the ip on lan, other was failed. That's why I used system() instead
of exec().It's worked in C. I want to know that can the system() do
normal in Java by JNI model. Because I always get the wrong return
value.

Yes you can also use JNI but IMO that's just plain stupid, when you
can do it with 3 lines of Java.

Without seeing your code, it is impossible to say why you don't get
the expected results.

/gordon
 
R

Roedy Green

Yes you can also use JNI but IMO that's just plain stupid, when you
can do it with 3 lines of Java.

It depends on how often he has to do it. If it becomes a performance
bottleneck then JNI should be considerably faster. Agreed, it would
be nuts to put yourself through the JNI wringer unless there was a
proven bottleneck.
 

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

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top