首页 > 解决方案 > 如何在同一个 cmd 窗口中多次运行 .jar?

问题描述

我的代码必须运行一个生成一系列字符串的 .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());
            }
        }

    }

标签: javaruntime.exec

解决方案


推荐阅读