0

I'm executing a linux command in java using processBuilder but it adds [?1034h as the last line.

It is printing all lines but after my expected last line, it adds another line with those characters.

My code:

 ProcessBuilder processBuilder = new ProcessBuilder();

        // -- Linux --

        // Run a shell command
        processBuilder.command("bash", "-c", "sudo /usr/sbin/ilorest load -f "+biossource);

    

        try {

            Process process = processBuilder.start();

             output = new StringBuilder();

            BufferedReader  reader = new BufferedReader(
                    new InputStreamReader(process.getInputStream()));

            String line;
            while ((line = reader.readLine()) != null) {
                output.append(line);
            }
             BufferedReader stdError = new BufferedReader(new 
                     InputStreamReader(process.getErrorStream()));
                     
                     System.out.println("Here is the standard error of the command (if any):\n");
                while ((line = stdError.readLine()) != null) {
                    output.append(line);
                    System.out.println(line);
                }
            int exitVal = process.waitFor();
            if (exitVal == 0) {
                System.out.println("Success!");
                System.out.println(output);
                System.exit(0);
            } else {
                //abnormal...
            }

I also wrote the output to file and even there it is showing the same.

colourCoder
  • 1,394
  • 2
  • 11
  • 18
jasvik
  • 75
  • 7
  • Looks like an ANSI escape for the terminal. – Kayaman Sep 23 '20 at 11:11
  • so how to overcome that – jasvik Sep 23 '20 at 11:11
  • I had to think of Ctrl-D + \n as Ctrl-D = end-of-file, and the bytes 4, 10 (depending on the endianess) could very well be 4*256+10 = 1034, a short 'h'. – Joop Eggen Sep 23 '20 at 11:47
  • One possible error cause would be encoding. You are using InputStreamReader without charset encoding. It will default to the platform encoding, which however seems correct. Maybe you should execute `sudo` without `bash`. – Joop Eggen Sep 23 '20 at 11:52
  • This problem has been reported by other folks using Python's `readline` module. I don't know enough about Python to know how to fix it. If this were my problem to solve, I'd filter the bogus text out of the response, perhaps using `string.replaceAll()`. – Kevin Boone Sep 23 '20 at 13:42

0 Answers0