Run Command through Java Program.

I am giving ping command in exec method of Runtime().

But ping syntax is different for linux and windows. So when I run command on windows it wasn't worked. I am giving command -->ping -c5 ipaddress . This works fine for linux but in windows, there is no -c option. So is there any solution to overcome this situation?

please reply ASAP.

Thanks,

Raj

[384 byte] By [Raj007a] at [2007-10-2 17:51:36]
# 1
> So is there any solution to overcome this situation?Don't use Runtime.exec(). Or determine the OS and call the appropriate command. Or scrap ping and simply try to open an URLConnection.
CeciNEstPasUnProgrammeura at 2007-7-13 19:09:56 > top of Java-index,Java Essentials,Java Programming...
# 2

I am writing code that runs ping command on all OS.

Ping syntax different for different OS (like windows,linux,solaris etc).

I don't want to check for each OS and run command specific to that OS.

I want to write a generic program so ping command execute regardless of which OS.

Can I use system calls in my program to execute the ping command, If yes, how?

please reply soon.

Thanks,

Raj

Raj007a at 2007-7-13 19:09:56 > top of Java-index,Java Essentials,Java Programming...
# 3

> I am writing code that runs ping command on all OS.

>

> Ping syntax different for different OS (like

> windows,linux,solaris etc).

> I don't want to check for each OS and run command

> specific to that OS.

> I want to write a generic program so ping command

> execute regardless of which OS.

This is self-contradictory. You can't use one command line string on different platforms if they don't support this one command string.

The ping command is different (concretely, this -c option you mention is indeed not supported on Windows - what does it do anyway?) on different OS.

So, you basically have two options:

- You use only a subset of the ping options, one that does run on all your platforms (I actually expect ping to be pretty portable anyway but...).

- Or you do use platform specific options like -c, in which case there's no way around checking the running OS and generating a specific command line.

> Can I use system calls in my program to execute the

> ping command, If yes, how?

Rephrase please.

Lokoa at 2007-7-13 19:09:56 > top of Java-index,Java Essentials,Java Programming...
# 4

> I am writing code that runs ping command on all OS.

Since the ping command isn't available to all OSes, that might not work.

> Ping syntax different for different OS (like

> windows,linux,solaris etc).

> I don't want to check for each OS and run command

> specific to that OS.

You'll have to.

> I want to write a generic program so ping command

> execute regardless of which OS.

Then you need to find a generic ping method. The ping commands are not generic. By the way, did it occur to you to enter "ping" into the forum search?

CeciNEstPasUnProgrammeura at 2007-7-13 19:09:56 > top of Java-index,Java Essentials,Java Programming...
# 5

Thanks for ur response.

But requirement is that I don't have to check OS.

>Can I use system calls in my program to execute the

> ping command, If yes, how?

-->Rephrase please.

Is java provide any system call that run ping command?

Thanks,

Raj

Raj007a at 2007-7-13 19:09:56 > top of Java-index,Java Essentials,Java Programming...
# 6

> Thanks for ur response.

>

> But requirement is that I don't have to check OS.

Why? Your requirement is to do something that is inherently platform specific, without checking the platform it is running on...

>

> >Can I use system calls in my program to execute the

> > ping command, If yes, how?

> -->Rephrase please.

>

> Is java provide any system call that run ping

> command?

Yes, thanks, that's meaningful.

What is a "system call" to you? Is it not calling a platform-specific C function in the OS' API (which you can do with JNI)? In which case you would have to know what platform you are running on to make the call.

Lokoa at 2007-7-13 19:09:56 > top of Java-index,Java Essentials,Java Programming...
# 7
> please reply ASAP. http://www.google.com/search?hl=en&q=java+ping
yawmarka at 2007-7-13 19:09:56 > top of Java-index,Java Essentials,Java Programming...
# 8

Thanks for ur response.

> What is a "system call" to you? Is it not calling a platform-specific C function in the OS' API (which you can do with JNI)? In which case you would have to know what platform you are running on to make the call.

Ok. I agree.. For use native methods i have to know OS.

So is there no any other way except check for OS. bcoz I have to implement code for each OS (linux,windows,solaris..etc) for that i have check OS and run appropriate command. Is that so ? Is there any better solution except this?

Thanks,

Raj

Raj007a at 2007-7-13 19:09:56 > top of Java-index,Java Essentials,Java Programming...
# 9

> The ping command is different (concretely,

> this -c option you mention is indeed not supported on

> Windows - what does it do anyway?) on different OS.

PING(8) System Manager's Manual: iputils

-c count

Stop after sending count ECHO_REQUEST packets.

With deadline option, ping waits for count ECHO_REPLY

packets, until the timeout expires.

BIJ001a at 2007-7-13 19:09:56 > top of Java-index,Java Essentials,Java Programming...
# 10
Ah, that's -n on Windows ping. I'm sure the differences are limited to this sort of small differences. It shouldn't be too hard to implement.
Lokoa at 2007-7-13 19:09:56 > top of Java-index,Java Essentials,Java Programming...
# 11

> So is there no any other way except check for OS.

> bcoz I have to implement code for each OS

> (linux,windows,solaris..etc) for that i have check OS

> and run appropriate command. Is that so ? Is there

> any better solution except this?

How about, as I think I already pointed out in the very first reply, implementing the ping functionality yourself? Shouldn't be rocket science.

CeciNEstPasUnProgrammeura at 2007-7-13 19:09:56 > top of Java-index,Java Essentials,Java Programming...
# 12
> implementing the ping functionality ... shouldn't be rocket science.Ping requires low-level protocol access which is not forcibly accessible from Java.
BIJ001a at 2007-7-13 19:09:56 > top of Java-index,Java Essentials,Java Programming...
# 13

Nope, I can't use URLConnection. Because I want to check connection for user entered IP Address.

How we implement ping functionality programatically.

I want same o/p as following commands giving:

Windows : ping -n 5 -l 64

linux : ping -c5 -s64

Thanks,

Maulik

Raj007a at 2007-7-13 19:09:56 > top of Java-index,Java Essentials,Java Programming...
# 14
> Nope, I can't use URLConnection. Because I want to> check connection for user entered IP Address.So what?
CeciNEstPasUnProgrammeura at 2007-7-13 19:09:56 > top of Java-index,Java Essentials,Java Programming...
# 15

//java.net.InetAddress

public boolean isReachable(int timeout)

throws IOException

/*

Test whether that address is reachable.

Best effort is made by the implementation to try to reach the host,

but firewalls and server configuration may block requests resulting

in a unreachable status while some specific ports may be accessible.

A typical implementation will use ICMP ECHO REQUESTs

if the privilege can be obtained, otherwise

it will try to establish a TCP connection on port 7 (Echo)

of the destination host.

Since:

1.5

*/

BIJ001a at 2007-7-20 23:24:26 > top of Java-index,Java Essentials,Java Programming...
# 16
isReachable is worthless, it does not use ICMP ECHO on Windows.
Herko_ter_Horsta at 2007-7-20 23:24:26 > top of Java-index,Java Essentials,Java Programming...