0

I need a way to run another java application from within my application. I want to recive its outputs into a JTextArea and send it inputs via a JTextBox.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
Taketheword
  • 1
  • 1
  • 4

1 Answers1

3

Depends.

You could use a custom URLClassLoader to load the second applications jar and call the main classes main method directly. Obviously, the issue there is getting the output from the program ;)

The other solution would be to use a ProcessBuilder to launch a java process and read the output via the InputStream

The problem here is trying to find the java executable. In general, if it's in the path you should be fine.

You can have a look at this as a base line example of how to read the inputstream

UPDATED with Example

This is my "output" program that produces the output...

public class Output {
    public static void main(String[] args) {
        System.out.println("This is a simple test");
        System.out.println("If you can read this");
        System.out.println("Then you are to close");
    }
}

This is my "reader" program that reads the input...

public class Input {

    public static void main(String[] args) {

        // SPECIAL NOTE
        // The last parameter is the Java program you want to execute
        // Because my program is wrapped up in a jar, I'm executing the Jar
        // the command line is different for executing plain class files
        ProcessBuilder pb = new ProcessBuilder("java", "-jar", "../Output/dist/Output.jar");
        pb.redirectErrorStream();

        InputStream is = null;
        try {

            Process process = pb.start();
            is = process.getInputStream();

            int value;
            while ((value = is.read()) != -1) {

                char inChar = (char)value;
                System.out.print(inChar);

            }

        } catch (IOException ex) {
            ex.printStackTrace();
        }        
    }
}

You can also checkout Basic I/O for more information

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • The problem is that I do have a good understanding of Java but I have no experience with ProcessBuilders or much with InputStreams.. I have used these before but on a side note.. where do you learn more Java? Ive already ready 2 books and watched/read online. – Taketheword Oct 13 '12 at 01:52
  • *"where do you learn more Java?"* Not relevant to this site & you didn't ask me, but I'll venture an answer anyway. I learn mostly from the Java Tutorials, answers I find on sites like this one, and asking (& answering) questions on this site. An answer to a question on this site can be quite instructive when your peers point at you and laugh, then describe in excruciating detail, the errors in your answer. ;) – Andrew Thompson Oct 13 '12 at 02:00
  • Yes the answers on this site are very helpful but I need help learning in general. This site is helpful in explaining what to use but not how to use it. – Taketheword Oct 13 '12 at 02:03
  • 1
    @Taketheword the single best "beginners" resource [The Java Tutorials](http://docs.oracle.com/javase/tutorial/index.html), I spent my first 4 weeks when I started my first job reading this site from cover to cover – MadProgrammer Oct 13 '12 at 02:06
  • +1 for `ProcessBuilder`. See also [`Console`](http://stackoverflow.com/a/4444677/230513). – trashgod Oct 13 '12 at 02:12