如何在同一cmd窗口中多次运行.jar?

时间:2018-11-12 12:40:51

标签: java runtime.exec

我的代码必须运行一个.jar来生成一系列字符串。我输入要运行.jar的次数以及每次应用程序运行时生成多少个字符串。我可以做到,但是每次打开一个新的CMD窗口。如何使其显示在单个窗口中?

我之所以这样,是因为我正在循环中创建cmd.exe进程,但是在没有创建cmd.exe进程且无法正常工作的地方尝试了其他代码。

谢谢。

public static void main(String args[]) {

        Process newpro;
        int reps = 0, stri = 0;            
        String command = "cmd /C start cmd.exe /K java -jar D:\\Documents\\NetBeansProjects\\Array\\dist\\Array.jar";

        System.out.println("How many times do you want to launch the app?");
        reps = readInt(0, 10);
        System.out.println("How many strings do you want to create in each repetition?");
        stri = readInt();

        for (int i = 0; i < reps; i++) {
            try {
                newpro = Runtime.getRuntime().exec(command + " " + stri);
            } catch (SecurityException ex) {
                System.out.println("Error.");
            } catch (Exception ex) {
                System.out.println("Error: " + ex.toString());
            }
        }

    }

0 个答案:

没有答案
相关问题