String s; Process process; try {process = Runtime.getruntime (). EXEC ("cmd/c d://test.bat"); InputStream In=process.getinputstream (); BufferedReader BufferedReader = new BufferedReader (new InputStreamReader (in)); while ((S=bufferedreader.readline ())!= null) {System.out.println ("SSS" +s);} in.close (); Bufferedreader.close (); catch (IOException e) {e.printstacktrace ();}
BAT Content
@echo off:: set/p var= Please enter username::: echo the username you entered is%var% Java-classpath e:/6/test::d ir d:/success/echo success Exit 0
Attention:
When Java invokes a bat through DOS, if you use the CMD/C start D://test.bat (that is, the start command), the contents of the bat output are not obtained. This way, although we can't get the bat from the Java program if it's done, we can tell if the bat is over by the last printed information through the bat.
Also, so that Java can get the information printed in the Java main function in the bat called through System.out.print and in the bat, through the Echo