1

My code:

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);
    }

So my readLine is always null and I don't know why. I've printed it out to confirm. When I execute my command in cmd directly I had an answer. So why isn't it reading the line?

Im trying with an array of strings. Still not working

Bence Kaulics
  • 7,066
  • 7
  • 33
  • 63
R.Quintal
  • 43
  • 1
  • 3
  • 13

1 Answers1

0

The problem is that it is not the way you should specify the command. Your command is made of command arguments, and you must separate each!

So build an array of Strings that contains : msg, then /SERVER:xxxx, etc. and pass it to exec.

---- EDIT

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)
Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69
  • Are you talking about doing this : String Mensagem = textAreaMsg.getText(); String NomePC = TxtNomePc.getText(); ?? If you don't I'm not understanding you. – R.Quintal Feb 05 '16 at 10:25
  • See my EDIT, you must **separate** each argument of the command line into `String`s, and build an array of these strings. If not the entire string is considered as a command but there is no command that is called `msg /SERVER:xxx...`, there is a command named `msg` that takes arguments. – Jean-Baptiste Yunès Feb 05 '16 at 10:32
  • I've tried it now. Still not working. I'm going to update an image, check it. – R.Quintal Feb 05 '16 at 10:40
  • You have several other problems: "*" must be expanded to the files you want, and you shoudn't use `"` inside you last argument, every arg you pass will be received as-is by your command. Test with a simpler command, like `dir` for example... – Jean-Baptiste Yunès Feb 05 '16 at 11:10
  • I understand what you are saying but my cmd command that I'm trying to use takes " and * so there is nothing I can do. I have to do It like that.... I'm going crazy about this. – R.Quintal Feb 05 '16 at 11:40
  • I think you misunderstand what the command line interpreter do... `*` is expanded (by the command line interpreter) before the command is launched, your command don't receive `*` as a parameter. What is the command line you use in your hand-test? – Jean-Baptiste Yunès Feb 05 '16 at 15:15
  • msg /SERVER:xxxx * /Time:300 "Message". And I need the * because If I don't put it the command doesn't work. – R.Quintal Feb 05 '16 at 17:58
  • `*` is a wildcard to represent files of any name, replace this with the files in the directory you are in, and the `"` are just to protect the string, but are not useful here. – Jean-Baptiste Yunès Feb 05 '16 at 18:11