- Execute An External Program and Capture The Output

zhaozj2021-02-08  407

Execute An External Program and Capture THE OUTPUT

Import java.io. *;

Public class cmdexec {

Public cmdexec (String cmdline) {

Try {

String line;

Process P = runtime.getRuntime (). EXEC (CMDLINE);

// jdk1.0.2

DataInputstream INPUT =

New DataInputStream (p.GetinputStream ());

// jdk1.1.1

// BufferedReader commandResult =

// new bufferedreader

// (New INPUTSTREAMREADER (P.GetinputStream ()));

While ((line = input.readline ())! = NULL) {

System.out.println (line);

}

INPUT.CLOSE ();

}

Catch (Exception Err) {

System.out.println ("Exec Failed:" Err.toString ());

Err.PrintStackTrace ();

}

}

Public static void main (String Argv []) {

/ *

** javac cmdexec tree.com

** javac cmdexec "tree.com / a"

* /

New cmdexec (argv [0]);

}

}

You can include a path for the program to be executed. On the winpablemiff, you will have it. To fix them in it. To fix they, put the path in quotes.

Public class test {

Public static void main (string [] args) throws exception {

Process p = runtime.getRuntime (). EXEC (

"/" c: / program files / windows / notepad.exe / "");

p.WaitFor ();

}

}

转载请注明原文地址:https://www.9cbs.com/read-848.html

New Post(0)