Avatar of gudii9
gudii9
Flag for United States of America asked on

unix in java example

Hi,

I am running below exam in eclipse

https://www.mkyong.com/java/how-to-execute-shell-command-from-java/

getting below errrors.

Bad option -c.


Usage: ping [-t] [-a] [-n count] [-l size] [-f] [-i TTL] [-v TOS]
            [-r count] [-s count] [[-j host-list] | [-k host-list]]
            [-w timeout] [-R] [-S srcaddr] [-4] [-6] target_name

Options:
    -t             Ping the specified host until stopped.
                   To see statistics and continue - type Control-Break;
                   To stop - type Control-C.
    -a             Resolve addresses to hostnames.
    -n count       Number of echo requests to send.
    -l size        Send buffer size.
    -f             Set Don't Fragment flag in packet (IPv4-only).
    -i TTL         Time To Live.
    -v TOS         Type Of Service (IPv4-only. This setting has been deprecated
                   and has no effect on the type of service field in the IP Header).
    -r count       Record route for count hops (IPv4-only).
    -s count       Timestamp for count hops (IPv4-only).
    -j host-list   Loose source route along host-list (IPv4-only).
    -k host-list   Strict source route along host-list (IPv4-only).
    -w timeout     Timeout in milliseconds to wait for each reply.
    -R             Use routing header to test reverse route also (IPv6-only).
    -S srcaddr     Source address to use.
    -4             Force using IPv4.
    -6             Force using IPv6.


package sample;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class ExecuteShellComand {

	public static void main(String[] args) {

		ExecuteShellComand obj = new ExecuteShellComand();

		String domainName = "google.com";

		//in mac oxs
		String command = "ping -c 3 " + domainName;

		//in windows
		//String command = "ping -n 3 " + domainName;

		String output = obj.executeCommand(command);

		System.out.println(output);

	}

	private String executeCommand(String command) {

		StringBuffer output = new StringBuffer();

		Process p;
		try {
			p = Runtime.getRuntime().exec(command);
			p.waitFor();
			BufferedReader reader =
                            new BufferedReader(new InputStreamReader(p.getInputStream()));

                        String line = "";
			while ((line = reader.readLine())!= null) {
				output.append(line + "\n");
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

		return output.toString();

	}

}

Open in new window

please advise
JavaUnix OS

Avatar of undefined
Last Comment
gudii9

8/22/2022 - Mon
SOLUTION
Thomas Wheeler

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
Gerwin Jansen

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
gudii9

ASKER
package sample;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class ExecuteShellComand {

	public static void main(String[] args) {

		ExecuteShellComand obj = new ExecuteShellComand();

		String domainName = "google.com";

		//in mac oxs
		//String command = "ping -c 3 " + domainName;

		//in windows
		//String command = "ping -n 3 " + domainName;

		//String output = obj.executeCommand(command);
		 String command = "ping -n 3 " + domainName;

         String output = obj.executeCommand(command);
		System.out.println(output);

	}

	private String executeCommand(String command) {

		StringBuffer output = new StringBuffer();

		Process p;
		try {
			p = Runtime.getRuntime().exec(command);
			p.waitFor();
			BufferedReader reader =
                            new BufferedReader(new InputStreamReader(p.getInputStream()));

                        String line = "";
			while ((line = reader.readLine())!= null) {
				output.append(line + "\n");
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

		return output.toString();

	}

}

Open in new window

above gave below output
Pinging google.com [64.233.177.102] with 32 bytes of data:
Request timed out.
Request timed out.
Request timed out.

Ping statistics for 64.233.177.102:
    Packets: Sent = 3, Received = 0, Lost = 3 (100% loss),

not sure why timing out. is it due to firewall?
gudii9

ASKER
package sample;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class ExecuteShellComand {

	public static void main(String[] args) {

		ExecuteShellComand obj = new ExecuteShellComand();

		String domainName = "google.com";

		//in mac oxs
		//String command = "ping -c 3 " + domainName;

		//in windows
		//String command = "ping -n 3 " + domainName;

		//String output = obj.executeCommand(command);
		 String command = "ping -n 3 " + domainName;

         String output = obj.executeCommand(command);
		System.out.println(output);

	}

	private String executeCommand(String command) {

		StringBuffer output = new StringBuffer();

		Process p;
		try {
			p = Runtime.getRuntime().exec(command);
			p.waitFor();
			BufferedReader reader =
                            new BufferedReader(new InputStreamReader(p.getInputStream()));

                        String line = "";
			while ((line = reader.readLine())!= null) {
				output.append(line + "\n");
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

		return output.toString();

	}

}

Open in new window

above gave below output
Pinging google.com [64.233.177.102] with 32 bytes of data:
Request timed out.
Request timed out.
Request timed out.

Ping statistics for 64.233.177.102:
    Packets: Sent = 3, Received = 0, Lost = 3 (100% loss),

not sure why timing out. is it due to firewall?
Gerwin Jansen

Try another site to ping.

But from your other question: you cannot download from a Cygwin mirror, you have similar issues here -> fix your internet setup first...
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Gerwin Jansen

So your program in Eclipse is working, your internet connection issue is not what we can solve here for you.
gudii9

ASKER
when i used second radio button optiuon which says like 'internet proxy' that worked. Then it gave lot of urls and selected one and next next..... worked.Direct radio button option did not work
gudii9

ASKER
one other thing i did different is run as administrator
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
gudii9

ASKER
any link on step by step examples of unix on cygwin examples starting from hello world to more complex loops etc