Java Runtime.getRuntime(). exec() Does not execute commands with space

Asked

Viewed 468 times

1

I have a client socket and a server, the client sends a command to the server to run, but when the command has spaces, the server generates an exception: java.io.Ioexception: invalid null Character in command

Server Code:

import java.io.*;
import java.net.*;
import java.io.IOException;

public class UDPServidor{
public static void main(String[] args) throws Exception{

        DatagramSocket servidorSocket = new DatagramSocket(9876);
        byte[] dadosRecebidos = new byte[1024];
        byte[] dadosEnviados = new byte[1024];
        while(true){
            dadosRecebidos = new byte[1024];
            dadosEnviados = new byte[1024];
            DatagramPacket pacoteRecebido = new DatagramPacket(dadosRecebidos,dadosRecebidos.length);

            servidorSocket.receive(pacoteRecebido);
            String sentence = null;
            sentence = new String(pacoteRecebido.getData());

            InetAddress enderecoIP = pacoteRecebido.getAddress();
            int porta = pacoteRecebido.getPort();
            System.out.println("Comando Recebido = " + sentence);
            Runtime run = Runtime.getRuntime();
            try {
                Process p = run.exec(sentence);
                String line = null;     

                BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
                while ((line = input.readLine()) != null) {
                    System.out.println(line);
                }
                input.close();

                System.out.println("\n##Processo Finalizado.");
            } catch (IOException e) {
                e.printStackTrace();
            }

            String sentenceCapturada = sentence.toUpperCase();

            dadosEnviados = sentenceCapturada.getBytes();

            DatagramPacket pacoteEnviado = new DatagramPacket(dadosEnviados,dadosEnviados.length,enderecoIP,porta);
            servidorSocket.send(pacoteEnviado);
        }

}
}

Client Code:

    import java.io.*;
    import java.net.*;

    public class UDPCliente{
         public static void main(String[] args) throws Exception {
         BufferedReader cadeidaUsuario = new BufferedReader(new InputStreamReader(System.in));
         DatagramSocket clienteSocket = new DatagramSocket();

         InetAddress enderecoIP = InetAddress.getByName("localhost");
         byte[] enviaDados = new byte[1024];
         byte[] recebeDados = new byte[1024];

         String sentence = cadeidaUsuario.readLine();
         enviaDados = sentence.getBytes();

         DatagramPacket enviaPacote = new DatagramPacket(enviaDados,enviaDados.length,enderecoIP,9876);
         clienteSocket.send(enviaPacote);

        DatagramPacket recebePacote = new DatagramPacket(recebeDados,recebeDados.length);
        clienteSocket.receive(recebePacote);
        String sentenceModificada = new  String(recebePacote.getData());
        System.out.println("Código recebido com sucesso: "+ sentenceModificada);
        clienteSocket.close();

        }
 }
  • Have you tried printing the instruction on the server before running? If so, is the output correct? With spaces or "strange" characters in place of spaces?

  • Yes, I print the command on the server and the output is correct, just like the input

  • Is this "command" to run on Windows? You can show one of the commands that gives error?

  • No, I’m running Ubuntu Linux. It gives error when the command is "mkdir test" or any other command that has space, but when I am an "ls" or "Eject" it works normally.

1 answer

0


Try to 'Clean' the String before executing the command:

  String sentence = null;
  sentence = new String(pacoteRecebido.getData()).trim();

It happens that when sending your String, he will complete the 1024 bytes remaining with empty characters!

  • It worked! That’s right, thank you so much for your help!!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.