Solved

unix in java example

Posted on 2016-10-21
9
78 Views
Last Modified: 2016-10-28
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
0
Comment
Question by:gudii9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
9 Comments
 
LVL 7

Assisted Solution

by:Thomas Wheeler
Thomas Wheeler earned 250 total points
ID: 41854397
it's saying that the ping utility that is being called uses -n not -c for the count
0
 
LVL 38

Accepted Solution

by:
Gerwin Jansen, EE MVE earned 250 total points
ID: 41854427
You are running this program under Windows, not Unix/Linux. The output you see is from the Windows ping command.

Change like this:


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

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

            String output = obj.executeCommand(command);
0
 
LVL 7

Author Comment

by:gudii9
ID: 41854530
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?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Author Comment

by:gudii9
ID: 41854531
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?
0
 
LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 41854562
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...
0
 
LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 41854565
So your program in Eclipse is working, your internet connection issue is not what we can solve here for you.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41857439
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
0
 
LVL 7

Author Comment

by:gudii9
ID: 41857447
one other thing i did different is run as administrator
0
 
LVL 7

Author Comment

by:gudii9
ID: 41857448
any link on step by step examples of unix on cygwin examples starting from hello world to more complex loops etc
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
runtime exception 2 50
Is there a simpler dropbox system? 10 44
Is Applet the way to go for my drag and drop system? 8 36
Java 8 to Java 6 8 33
Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This video teaches viewers about errors in exception handling.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question