简体   繁体   English

如何使用java打开wordpad

[英]How do I open wordpad using java

I am doing a small project for my girlfriends grandparents, that have a hard time using a computer so I thought I would be able to write something that might fix their problem. 我正在为我的女朋友祖父母做一个小项目,他们很难使用电脑,所以我想我可以写一些可能解决问题的东西。 Here is the code first off: 这是第一个代码:

import java.io.IOException;

public class OpenWordPad {

public static void main(String[] args) {
    try {
        System.out.println("Opening WordPad");
        Runtime runTime = Runtime.getRuntime();
        Process process = runTime.exec("wordpad");
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Closing WordPad");
        process.destroy();
    } catch (IOException e) {
        e.printStackTrace();
    }
  }

}

(had to indent some so sorry if it is a little wonky) (不得不缩进一些,如果它有点不稳定那么抱歉)

When I put notepad in the process line it works fine but when I put in wordpad it freaks out. 当我把记事本放在生产线上时,它可以正常工作,但是当我放入wordpad时,它就会吓坏了。 I want to be able to open wordpad so I can put it on their computer. 我想能够打开wordpad,所以我可以把它放在他们的电脑上。 Any suggestions? 有什么建议么?

For that you can use runTime.exec("write") : 为此,您可以使用runTime.exec("write")

import java.io.IOException;

public class OpenWordPad {

    public static void main(String[] args) {
        try {
            System.out.println("Opening WordPad");
            Runtime runTime = Runtime.getRuntime();
            Process process = runTime.exec("write"); // <--- here
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("Closing WordPad");
            process.destroy();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

opens WordPad. 打开写字板。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM