【问题标题】:what is runtime.getruntime().exec("domain specific equivalent of cls") for windows 7什么是 windows 7 的 runtime.getruntime().exec("域特定等效的 cls")
【发布时间】:2013-02-06 18:05:35
【问题描述】:

我想在我的java应用程序中清除屏幕,在阅读了很多问题和谷歌搜索之后,我找到了下面的代码

runtime.getruntime().exec("cls")

Runtime.getRuntime().exec("cmd /c cls");

但上面的代码在 Windows 7 中不起作用。我知道“cls”脚本是特定于域的,有谁知道我应该在 Windows 7 中使用什么文本。这真的很有帮助,谢谢进步。

【问题讨论】:

  • 您是否遇到任何错误?
  • 将'Home'和'Clear to EOS'的ANSI转义序列写入System.out会简单得多。
  • @askappy 我想我明白了,因为该命令不起作用。

标签: java process console-application runtime.exec


【解决方案1】:

我知道您正在寻找一种简单的方法来清除屏幕。您将不得不使用换行黑客或 使用启用 ANSI 的控制台。这是您或其他阅读本文的人可以考虑的使用 JNA 的更困难的仅限 Windows 的方法。这是一个教学示例。根据需要添加错误检查/处理/导入/包含。您必须已经知道如何使用 JNA。如果您是 JNA 的新手,这是您第一个尝试的好程序。

//------------------------------------------
// Java2Win.class
//------------------------------------------
public interface Java2Win extends Library {
    Java2Win java2Win = (Java2Win)Native.loadLibrary("Java2Win64",Java2Win.class);
    void cls();
}
//------------------------------------------

//------------------------------------------
// Java2Win.c (Java2Win.dll & Java2Win64.dll)
//------------------------------------------
JNIEXPORT void cls() {
   system("cls");
}
//------------------------------------------

//------------------------------------------
// Test
//------------------------------------------
public static void main(final String args[]) throws Exception {
    final File file = new File("rootToDLL", "Java2Win64.dll");
    LibraryLoader.loadLibrary(file);
    System.out.println("-----some output");
    System.out.println("-----some output");
    System.out.println("-----some output");
    Thread.sleep(2000);
    Java2Win.java2Win.cls();
    System.out.println("-----cleared");
}
//------------------------------------------

【讨论】:

    【解决方案2】:

    由于cls 是一个内部命令(cmd.exe 自己执行而不是调用可执行程序),您可以这样做:

    cmd /c cls
    

    这在 Windows 7 下运行良好,假设您实际运行的是控制台类型的应用程序。

    【讨论】:

    • 我会试试这个,但它看起来像是我之前尝试过的众多组合之一。
    • Runtime.getRuntime().exec("cmd /c cls"); dint 工作像往常一样得到 IO 异常。
    • @Radan 您需要将参数作为字符串数组传递,请参见:stackoverflow.com/questions/3608944/…
    • 它在这里仍然不起作用:String[] command = new String[] {"cmd.exe", "/c", "cls"}; Runtime.getRuntime().exec(command);
    • 我可以确认这对我在 Win7 上也不起作用。我也试过:String[] command = new String[] {"C:\\Windows\\System32\\cmd.exe", "/c", "cls"}; Runtime.getRuntime().exec(command); 没有给出错误,该行看起来像是被跳过了。
    猜你喜欢
    • 2011-02-11
    • 2011-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-05
    • 2012-12-27
    • 1970-01-01
    • 2012-04-18
    相关资源
    最近更新 更多