Java invokes DOS and obtains output of bat in execution

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.