在同一控制台中从java调用并运行jar文件

时间:2016-03-01 22:21:02

标签: java process runtime exec

我正在尝试编写一个能够从给定目录打开任何.jar文件的程序。使用Process ps = Runtime.getRuntime().exec("java -jar " + contents[selectInt - 1]);contents[selectInt - 1]的典型值类似于C:\Users\Kieran\Desktop\CharCount.jar(我已经过测试,确实给jar文件提供了完整的文件路径),没有命令提示符窗口弹出,当前窗口中不显示任何内容。在控制台中输入java -jar C:\Users\Kieran\Desktop\CharCount.jar就可以了。完整代码如下:

package listfiles;

import java.util.Scanner;
import java.io.*;

public class ListFiles {

    private static Scanner sc = new Scanner(System.in);
    private static File folder;

    public static void main(String[] args) {
        //Set folder to search
        String filePath;
        final int LENGTH = args.length;
        if (LENGTH > 0) {
            filePath = args[0];
        } else {
            System.out.print("File to search (or \"QUIT\" to exit): ");
            filePath = sc.next();
            if (filePath.equals("QUIT")) {
                exit();
            }
        }
        folder = new File(filePath);

        //Create an array of folder contents with the extension .jar and print it
        File[] contents = folder.listFiles(new JarFilter());
        System.out.print("Search in \"" + filePath + "\" found " + contents.length + " result");
        if (contents.length != 1) {
            System.out.print("s");
        }
        System.out.print(":\n\n");
        int i = 0;
        for (File file : contents) {
            i++;
            System.out.println("[" + i + "] " + file);
        }

        //Allow the user to run a jar file
        int selectInt = 0;
        while (true) {
            System.out.print("Please select a file number to open or \"QUIT\" to exit: ");
            String select = sc.next();
            if (select.equals("QUIT")) {
                exit();
            }
            try {
                selectInt = Integer.parseInt(select);
            } catch(NumberFormatException e) {
                continue;
            }
            if ((selectInt > 0) && (selectInt <= contents.length)) {
                break;
            }
        }
        try {
            System.out.println("java -jar " + contents[selectInt - 1]);
            Process ps = Runtime.getRuntime().exec("java -jar " + contents[selectInt - 1]);
        } catch (IOException e) {
            System.out.println("Opening .jar file failed.");
            exit();
        }
    }

    private static void exit() {
        System.out.print("\n\nExiting...\n");
        System.exit(0);
    }
}

JarFilter只接受.jar文件(并且可以工作)。

我非常感激任何可能会流下的光。

编辑:CharCount.jar的代码如下:

package charcount;

public class CharCount
    {
    public static void main(String[] args)
    {
        int i = 0;
        for (String str : args)
        {
            System.out.println("\"" + str + "\" has " + str.length() + " characters.");
            i += str.length();
        }
        System.out.println("Total characters: " + i);
        System.exit(0);
    }
}

1 个答案:

答案 0 :(得分:0)

如果您尝试从cmd运行它,请尝试:

Runtime.getRuntime().exec("cmd /c start java -jar FILEPATH");

或者在你的情况下:

Runtime.getRuntime().exec("cmd /c start java -jar" + contents[selectInt - 1]);

---- ---- UPDATE

要保持提示打开使用:

Runtime.getRuntime().exec("cmd /c start cmd /k java -jar " + contents[selectInt - 1]);