1

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 ..

8
  • No surprises the command java -classpath /home/4/byz/Orc is incomplete. If you want to run a java Class, you will have to mention that at the end of java command. Did you try something like java -classpath $CLASSPATH:/home/4/byz/ Orc - note the space before Orc Commented Mar 31, 2012 at 3:23
  • i just tried your sugesstion, it didnt work :( Commented Mar 31, 2012 at 3:38
  • how do you run Orc from commandline? Commented Mar 31, 2012 at 4:04
  • i go to the folder containing the class file using cd byz and then I am doing r@r-desktop:~/byz$ java Orc Commented Mar 31, 2012 at 4:10
  • or I use java -classpath /home/r/byz Orc Commented Mar 31, 2012 at 4:13

1 Answer 1

3

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!

Sign up to request clarification or add additional context in comments.

2 Comments

so i guess you are not only running the command, but also reading the output the command generates ? Please let me know if this is indeed the case. Thanks.
Yeah, I've used this in one of my apps before. It works great for returning output :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.