Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eclipse crashes on the user input

I have a class 'one' that compiles class 'two' using commands

I use this code to run two

Process p2 = Runtime.getRuntime().exec("java two");
           BufferedReader in = new BufferedReader(
                   new InputStreamReader(p2.getInputStream()) );
           while ((line = in.readLine()) != null) {
             System.out.println(line);
           }
           in.close();

Now when 'two' has printings in its main method, it works fine and they are printed in the console but when it has a user input Eclipse crashes. when I even remove the while loop it doesn't allow me to write in the console

I am creating a new console using

MessageConsole console = new MessageConsole("", null);
    console.activate();
    ConsolePlugin.getDefault().getConsoleManager()
            .addConsoles(new IConsole[] { console });
    MessageConsoleStream stream = console.newMessageStream();
    System.setOut(new PrintStream(stream, true));
like image 733
student1234 Avatar asked Nov 12 '22 03:11

student1234


1 Answers

I had a similar problem. I extended the MessageConsole (just to be able to have a specific consolePageParticipant) and in the constructor I have redirected the System.out to a new MessageConsoleStream. With the first version of my code the RCP application crashed, with the second version it hanged.

I already don't remember how did the code which crashed/hanged look like, but I found out that I cannot redirect the output sooner, than the MessageConsole is displayed. So I used a new thread to wait for some time (5 seconds - maybe too much?) before the redirect.

messageConsoleStream = myConsole.newMessageStream();

new Thread(new Runnable() {
    @Override
    public void run() {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
        }
        OutputStream out = new OutputStream() {
            @Override
            public void write(int b) throws IOException {
                messageConsoleStream.write(b);
                oldOut.write(b);
            }
        };
        System.setOut(new PrintStream(out));
        LOGGER.debug("'System.out' is redirected to the console."); //$NON-NLS-1$
    }
}, "Redirect system out to console...").start(); //$NON-NLS-1$

Still it would be good to change the Thread.sleep(5000); to some wait until the console is displayed...

like image 80
Stybi Avatar answered Nov 14 '22 21:11

Stybi