Chani
Chani

Reputation: 5165

firing a command through terminal in Ubuntu using Java Runtime.exec

This is fairly simple in Windows, but a little tricky in Linux. I am using

Runtime.getRuntime().exec(new String[] { "/bin/bash", "-c", "java -classpath /home/4/byz/Orc" });

where Orc is the class file with a main function. But nothing happens. Are there any settings ? Am I doing something wrong ?

I wish the java program to run in the terminal.

EDIT

Here is the solution:

        String[] cmdArray = {"gnome-terminal","java -classpath /home/r/byz/ Orchestrator"};

        try {
            Runtime.getRuntime().exec(cmdArray);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

So basically, we have to use gnome-terminal ..

Upvotes: 1

Views: 8069

Answers (1)

Jared Burrows
Jared Burrows

Reputation: 55517

I believe this is already resolved, however I'll post an answer:

How to Run:

executeCommand(new String[]{"/bin/bash", "-c", "java -classpath /home/4/byz/Orc"});

Method:

public String executeCommand(String[] cmd) {
    StringBuffer theRun = null;
    try {
        Process process = Runtime.getRuntime().exec(cmd);

        BufferedReader reader = new BufferedReader(
                new InputStreamReader(process.getInputStream()));
        int read;
        char[] buffer = new char[4096];
        StringBuffer output = new StringBuffer();
        while ((read = reader.read(buffer)) > 0) {
            theRun = output.append(buffer, 0, read);
        }
        reader.close();
        process.waitFor();

    } catch (IOException e) {
        throw new RuntimeException(e);
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
        return theRun.toString().trim();
}

Let me know if this helps!

Upvotes: 3

Related Questions