0

I did a code which can run cmd of windows from java,

and also I stored the result of that cmd in a string variable,

my question is is it possible to get a substring from that variable from where I had stored the output of the cmd.

I want to put a statement that create an action if a substring is found in the cmd output string variable.

here are my codes:

    public static void main(String[] args) throws IOException, InterruptedException
{
runWndCommand("ping 192.168.11.3");
}
public static void runWndCommand(String cmd) throws IOException, InterruptedException
{
    Runtime runtime = Runtime.getRuntime();
    Process p = runtime.exec(new String[] { "cmd.exe", "/C", cmd });

    Scanner reader = new Scanner(p.getInputStream());

     while (reader.hasNext())
    {

        String r=reader.nextLine();
System.out.println(r);
    }
 p.waitFor();
 }
2
Contributors
2
Replies
3
Views
4 Years
Discussion Span
Last Post by sofien.fkih
1

There are a few ways to do this, but the easiest is probably the contains method:

String s = "Hello from DaniWeb";
if (s.contains("from")) ...
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.