如何使用eclipse和java编程打开Windows命令提示符?

时间:2014-05-28 14:12:02

标签: java eclipse window external command-prompt

我正在创建一个基于文本的程序/游戏,我认识的人创建了一个将控制台打开到Windows命令提示符的程序。这使它可以不断刷新,使文本清晰,外观整洁,而不是笨重的控制台视图,只是将文本添加到其上,为基于文本的游戏制作了不良媒体。

1 个答案:

答案 0 :(得分:0)

以下可能会给你一个想法:

public static void main(String[] args) throws Exception {
    // Since you are using eclipse, bin contains the compiled classes
    // This depends on your working directory. 
    File directory = new File("bin");

    // In my case, the main class is Game (in the default package)
    ProcessBuilder pb = new ProcessBuilder("cmd", "/c", "java", "Game");

    // directory defined above
    pb.directory(directory);

    // The fun begins!
    pb.start();
}

Game类仅包含以下内容:

import java.util.Scanner;

public class Game {
    public static void main(String[] args) {
        try (Scanner sc = new Scanner(System.in)) {
            System.out.println(sc.nextLine());
        }
    }
}