java代码调用bat文件

package com.gyb;
import java.io.IOException;
import java.io.InputStream;

public class InvokeBat4 {
    public void runbat(String batName) {
        String cmd = "cmd /k start D:\\new\\news.bat";// pass
        try {
            Process ps = Runtime.getRuntime().exec(cmd);
            InputStream in = ps.getInputStream();
            int c;
            while ((c = in.read()) != -1) {
                System.out.print(c);// 如果你不需要看输出,这行可以注销掉
            }
            in.close();
            ps.waitFor();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
        catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("child thread donn");
    }

    public static void main(String[] args) {
        InvokeBat4 test1 = new InvokeBat4();
        test1.runbat("news");
        System.out.println("main thread");
    }
}

------bat文件中这样写

d:& cd new
java -jar -Xmn600m -Xms1200m -Xmx1200m lmsp-news-snatch.jar
exit

转载于:https://www.cnblogs.com/gyb109/p/6601757.html

你可能感兴趣的:(java,runtime)