【问题标题】:Java clear console error: java.io.IOException: Cannot run program "clear"Java清除控制台错误:java.io.IOException:无法运行程序“清除”
【发布时间】:2013-07-17 10:05:51
【问题描述】:

所以我正在尝试制作一个用户输入 5 个单词的游戏,然后在他的对手必须猜测 5 个单词之前清除控制台。我没有做任何事情,比如删除空格或转换为一致的大小写,因为这只是我在尝试学习,而且我已经知道如何去做。尽管在系统尝试(并且失败)清除控制台时得到了这么长的错误列表,但程序仍然可以完美执行。这是输出(注意:我也使用了“cls”和“clear”,但都不起作用):

| Player One Enter Words | 
Word 1: why
Word 2: does
Word 3: this
Word 4: not
Word 5: work
java.io.IOException: Cannot run program "clear": CreateProcess error=2, The system   cannot find the file specified
at java.lang.ProcessBuilder.start(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at OrigClass.main(OrigClass.java:18)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(Unknown Source)
at java.lang.ProcessImpl.start(Unknown Source)
... 5 more
| Player Two Guess Words | 
Guess 1: not
Guess 2: work
Guess 3: why
Guess 4: does
Guess 5: this
Congrats. You Got A Perfect Score Of 5

这是我的实际代码:

import java.util.Arrays;
import java.util.Scanner;

class OrigClass{
    public static void main(String[] args){
    Scanner scanObj = new Scanner(System.in);
    String[] words;
    words = new String[5];
    String[] guess;
    guess = new String[5];
    System.out.println(" | Player One Enter Words | ");

    for(int count = 1; count <= 5; count++){
        System.out.print("Word " + count + ": ");
        words[count-1] = scanObj.nextLine();
    }
    try{
    Runtime.getRuntime().exec("clear");
    }
    catch (Exception e){
        e.printStackTrace();
    }
    System.out.println(" | Player Two Guess Words | ");

    for(int count2 = 1; count2 <= 5; count2++){
        System.out.print("Guess " + count2 + ": ");
        guess[count2-1] = scanObj.nextLine();
    }

    int score = 0;

    for(int count3 = 0; count3 < 5; count3++){
        if(Arrays.asList(words).contains(guess[count3])){
            score++;
        }
    }

    if(score >= 0 && score <= 2){
        System.out.println("Unlucky. You Scored " + score);
    }
    else if (score > 2 && score < 5){
        System.out.println("Good Effort. You Scored " + score);
    }
    else {
        System.out.println("Congrats. You Got A Perfect Score Of " + score);
    }
}

}

如果有人知道如何防止此错误,我将不胜感激。如果您有另一种方法来清除控制台,那很好,但我想知道为什么这不起作用。

谢谢

【问题讨论】:

  • Java: Clear the console的可能重复
  • Runtime.getRuntime().exec("clear"); 尝试运行一个名为 clear 的程序 - 它找不到它并给您一个异常。

标签: java exception console


【解决方案1】:

在控制台相关的 API 中几乎没有什么可以做一个清晰的屏幕。但是,我认为您可以通过println()s 达到相同的效果。很多 putty 客户端都是这样清除页面然后向上滚动的。

private static final int PAGE_SIZE = 25;

public static void main(String[] args) {
            // ...
    clearScreen();
}

private static void clearScreen() {
    for (int i = 0; i < PAGE_SIZE; i++) {
        System.out.println();
    }
}

【讨论】:

  • 是的,我试过 cls & clear 都不管用...?我在原帖中没有说得很清楚
  • 正常:这些是cmd的内置命令,不是可以调用的可执行文件。
  • @JackSmith 抱歉,我忽略了这一点。这种新方法对你有用吗?
【解决方案2】:
    final String ANSI_CLS = "\u001b[2J";
    final String ANSI_HOME = "\u001b[H";
    System.out.print(ANSI_CLS + ANSI_HOME);
    System.out.flush();     

试试这个。

这对我很有效。 (库本图)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-11
    相关资源
    最近更新 更多