Runtime.getruntime().exec not working properly with gedit on Ubuntu

769 views Asked by At

I need to open a txt file from a location on Ubuntu (e.x. /usr/local/share/SomeFolder/Readme.txt) through my java code.

I've used Runtime.getRuntime.exec(String) and Runtime.getRuntime().exec(args) with gedit but it opens a new tab alongside the Readme.txt file.

The code is as below

String x="gedit /usr/local/share/SomeFolder/Readme.txt";
Runtime.getRuntime.exec(x);

<OR>

String[] args={"gedit","/usr/local/share/SomeFolder/Readme.txt"};
Runtime.getRuntime().exec(args)

I've also tried using ProcessBuilder but it behaves the same way. Also i've explored different options of gedit but still no luck.

When I run "gedit /usr/local/share/SomeFolder/Readme.txt" from terminal it opens only one instance and that of the Readme.txt file.

Maybe gedit is programmed to behave this way or I am doing something wrong.

Any help will be appreciated.

I am using Ubuntu 12.04 LTS,64 Bit with Eclipse Juno SR2 (JDK 1.7u51)

Thanks !

1

There are 1 answers

6
Honey Goyal On

Try this, i just tested using ProcessBuilder

    ProcessBuilder pb = new ProcessBuilder("gedit", "/home/honey/filename.json");
    pb.redirectOutput(Redirect.INHERIT);

    Process p = pb.start();
    p.waitFor();

    //This will print console logs of your process
    InputStream is = null;
    try {
        is = p.getInputStream();
        int in = -1;

        while ((in = is.read()) != -1) {
            System.out.print((char) in);
        }

    } finally {
        is.close();
    }