为什么我的readLine返回null?

时间:2016-02-05 10:07:00

标签: java null readline

我的代码:

String correr = "msg /SERVER:" + NomePC.replace(" ", "") + " * /TIME:300 \"" + Mensagem + "\"";
    Process textoo = null;
    try {
        textoo = Runtime.getRuntime().exec(correr);
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    BufferedReader innTexto = new BufferedReader(new InputStreamReader(textoo.getInputStream()));

    String lineTexto=null;

    try {
        lineTexto = innTexto.readLine();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    if (lineTexto != null) {
        System.out.println("Não passou");
    }
    else if (lineTexto == null) {
        System.out.println("A sua mensagem foi enviada com sucesso para " + NomePC + " ás: " + dataFormatada);
    }

所以我的readLine总是空的,我不知道为什么。我打印出来确认一下。当我直接在cmd中执行我的命令时,我得到了答案。那为什么不读这条线呢?

Im trying with an array of strings. Still not working

1 个答案:

答案 0 :(得分:0)

问题是你不应该指定命令。您的命令由命令参数组成,您必须分开每个!

建立一个包含String,然后msg等的/SERVER:xxxx数组,并将其传递给exec

----编辑

String []cmd = new String[THE_RIGHT_NUMBER_OF_ARGS_COMMAND_INCLUDED];
cmd[0] = "msg";
cmd[1] = "/SERVER:xxxx";
cmd[2] = "*";
cmd[3] = "/TIME:300";
...
textoo = Runtime.getRuntime().exec(cmd)