1

I'm writing a library to cover WMctrl shell program. I have problem with resizing windows:

String command = "wmctrl -r \"Calculator\" -e 0,100,100,500,500";

System.out.println(command);
String output = this.bashCommandExecutor.execute(command);
System.out.println(output);

This doesn't work - output variable is empty. But when I copy-paste wmctrl -r "Calculator" -e 0,100,100,500,500 to the Terminal it works properly.

Other commands like "wmctrl -d" and "wmctrl -l" work in this.bashCommandExecutor.execute() method.

This method looks like this:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class BashCommandExecutor
{
    String execute(String bashCommand)
    {
        Process p = null;
        try {
            p = Runtime.getRuntime().exec(bashCommand);
            p.waitFor();
        }
        catch (InterruptedException | IOException e) {
            e.printStackTrace();
        }

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

        String line = "";
        StringBuilder sb = new StringBuilder();
        try {
            while ((line = reader.readLine())!= null) {
                sb.append(line + "\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return sb.toString();
    }
}

Why does resizing work in command line, but doesn't work in Java app?

Simon
  • 1,099
  • 1
  • 11
  • 29

1 Answers1

0

I have found that using the ProcessBuilder class rather than Runtime.exec() works to run the wmctrl command you described. So rather than:

String bashCommand = "wmctrl -r \"Calculator\" -e 0,100,100,500,500";
Process p = Runtime.getRuntime().exec(bashCommand);

You can try:

String bashCommand = "wmctrl -r \"Calculator\" -e 0,100,100,500,500";
ProcessBuilder pb = new ProcessBuilder("bash", "-c", bashCommand);
Process p = pb.start();

Here is a link to another post explaining the different between Runtime.exec() and using ProcessBuilder.