We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › open java console when executing
Pages: 1 2 
open java console when executing (Read 2641 times)
open java console when executing
Oct 23rd, 2009, 2:36am
 
Hi all,

does anybody know how i can open the java console (by code) when executing the application? my problem is, that i'd like to see all the print() things in the executed file... which right now only appears when strating the file in processing.

cheers
Re: open java console when executing
Reply #1 - Oct 23rd, 2009, 3:10am
 
The code below should work, copied it from :
http://www.exampledepot.com/egs/javax.swing.text/ta_Console.html

..and modified it a bit to make it run in processing.

To create the console:
Code:
  try
 {
   console = new Console();
 }
 catch(Exception e)
 {
 }


Code:

public class Console extends javax.swing.JFrame {
       PipedInputStream piOut;
       PipedInputStream piErr;
       PipedOutputStream poOut;
       PipedOutputStream poErr;
       javax.swing.JTextArea textArea = new javax.swing.JTextArea();
   
       public Console() throws IOException {
           // Set up System.out
           piOut = new PipedInputStream();
           poOut = new PipedOutputStream(piOut);
           System.setOut(new PrintStream(poOut, true));
   
           // Set up System.err
           piErr = new PipedInputStream();
           poErr = new PipedOutputStream(piErr);
           System.setErr(new PrintStream(poErr, true));
   
           // Add a scrolling text area
           textArea.setEditable(false);
           textArea.setRows(20);
           textArea.setColumns(50);
           getContentPane().add(new javax.swing.JScrollPane(textArea), BorderLayout.CENTER);
           pack();
           setVisible(true);
   
           // Create reader threads
           new ReaderThread(piOut).start();
           new ReaderThread(piErr).start();
       }
   
       class ReaderThread extends Thread {
           PipedInputStream pi;
   
           ReaderThread(PipedInputStream pi) {
               this.pi = pi;
           }
   
           public void run() {
               final byte[] buf = new byte[1024];
               try {
                   while (true) {
                       final int len = pi.read(buf);
                       if (len == -1) {
                           break;
                       }
                       javax.swing.SwingUtilities.invokeLater(new Runnable() {
                           public void run() {
                               textArea.append(new String(buf, 0, len));
   
                               // Make sure the last line is always visible
                               textArea.setCaretPosition(textArea.getDocument().getLength());
   
                               // Keep the text area down to a certain character size
                               int idealSize = 1000;
                               int maxExcess = 500;
                               int excess = textArea.getDocument().getLength() - idealSize;
                               if (excess >= maxExcess) {
                                   textArea.replaceRange("", 0, excess);
                               }
                           }
                       });
                   }
               } catch (IOException e) {
               }
           }
       }
   }
Re: open java console when executing
Reply #2 - Oct 23rd, 2009, 3:55am
 
thx. did this code work for you? somehow i get the exception: "The constructor Console() is not visible"
Re: open java console when executing
Reply #3 - Oct 23rd, 2009, 4:01am
 
It worked for me... if you post the whole code, I can probably spot what is wrong.
Re: open java console when executing
Reply #4 - Oct 23rd, 2009, 4:03am
 
i just took what you posted. plus i included the import things of the initial code... but still getting an error
Re: open java console when executing
Reply #5 - Oct 23rd, 2009, 4:05am
 
ah and i used "Console console" because otherwise i get the exception: cannot find anything namend console

Code:
  try
{
Console console = new Console();
}
catch(Exception e)
{
}


Re: open java console when executing
Reply #6 - Oct 23rd, 2009, 4:11am
 
You did copy the whole Console class in your pde right ?
Re: open java console when executing
Reply #7 - Oct 23rd, 2009, 4:16am
 
yep, just as it is
Re: open java console when executing
Reply #8 - Oct 23rd, 2009, 4:19am
 
so this would be the whole sketch:
Code:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;

Console console;

try
{
console = new Console();
}

catch(Exception e){
}


public class Console extends javax.swing.JFrame {
PipedInputStream piOut;
PipedInputStream piErr;
PipedOutputStream poOut;
PipedOutputStream poErr;
javax.swing.JTextArea textArea = new javax.swing.JTextArea();

public Console() throws IOException {
// Set up System.out
piOut = new PipedInputStream();
poOut = new PipedOutputStream(piOut);
System.setOut(new PrintStream(poOut, true));

// Set up System.err
piErr = new PipedInputStream();
poErr = new PipedOutputStream(piErr);
System.setErr(new PrintStream(poErr, true));

// Add a scrolling text area
textArea.setEditable(false);
textArea.setRows(20);
textArea.setColumns(50);
getContentPane().add(new javax.swing.JScrollPane(textArea), BorderLayout.CENTER);
pack();
setVisible(true);

// Create reader threads
new ReaderThread(piOut).start();
new ReaderThread(piErr).start();
}

class ReaderThread extends Thread {
PipedInputStream pi;

ReaderThread(PipedInputStream pi) {
this.pi = pi;
}

void run() {
final byte[] buf = new byte[1024];
try {
while (true) {
final int len = pi.read(buf);
if (len == -1) {
break;
}
javax.swing.SwingUtilities.invokeLater(new Runnable() {
void run() {
textArea.append(new String(buf, 0, len));

// Make sure the last line is always visible
textArea.setCaretPosition(textArea.getDocument().getLength());

// Keep the text area down to a certain character size
int idealSize = 1000;
int maxExcess = 500;
int excess = textArea.getDocument().getLength() - idealSize;
if (excess >= maxExcess) {
textArea.replaceRange("", 0, excess);
}
}
});
}
} catch (IOException e) {
}
}
}
}
Re: open java console when executing
Reply #9 - Oct 23rd, 2009, 4:19am
 
Copy/paste this entire sketch in a new sketch, this works for me.

Code:
Console console;

void setup()
{
 try
 {
   console = new Console();
 }
 catch(Exception e)
 {
 }
 println("Test");
}

void draw()
{
}

public class Console extends javax.swing.JFrame {
 PipedInputStream piOut;
 PipedInputStream piErr;
 PipedOutputStream poOut;
 PipedOutputStream poErr;
 javax.swing.JTextArea textArea = new javax.swing.JTextArea();

 public Console() throws IOException {
   // Set up System.out
   piOut = new PipedInputStream();
   poOut = new PipedOutputStream(piOut);
   System.setOut(new PrintStream(poOut, true));

   // Set up System.err
   piErr = new PipedInputStream();
   poErr = new PipedOutputStream(piErr);
   System.setErr(new PrintStream(poErr, true));

   // Add a scrolling text area
   textArea.setEditable(false);
   textArea.setRows(20);
   textArea.setColumns(50);
   getContentPane().add(new javax.swing.JScrollPane(textArea), BorderLayout.CENTER);
   pack();
   setVisible(true);

   // Create reader threads
   new ReaderThread(piOut).start();
   new ReaderThread(piErr).start();
 }

 class ReaderThread extends Thread {
   PipedInputStream pi;

   ReaderThread(PipedInputStream pi) {
     this.pi = pi;
   }

   public void run() {
     final byte[] buf = new byte[1024];
     try {
       while (true) {
         final int len = pi.read(buf);
         if (len == -1) {
           break;
         }
         javax.swing.SwingUtilities.invokeLater(new Runnable() {
           public void run() {
             textArea.append(new String(buf, 0, len));

             // Make sure the last line is always visible
             textArea.setCaretPosition(textArea.getDocument().getLength());

             // Keep the text area down to a certain character size
             int idealSize = 1000;
             int maxExcess = 500;
             int excess = textArea.getDocument().getLength() - idealSize;
             if (excess >= maxExcess) {
               textArea.replaceRange("", 0, excess);
             }
           }
         }
         );
       }
     }
     catch (IOException e) {
     }
   }
 }
}
Re: open java console when executing
Reply #10 - Oct 23rd, 2009, 4:21am
 
thats working thx!
Re: open java console when executing
Reply #11 - Oct 23rd, 2009, 4:23am
 
timm wrote on Oct 23rd, 2009, 4:19am:
so this would be the whole sketch:
Code:

try
{
  console = new Console();
}

catch(Exception e){
}


That code should be in your setup() function.
Re: open java console when executing
Reply #12 - Oct 23rd, 2009, 4:26am
 
but when i do something like:

void draw()
{
 println("test and so on");
 delay(100);
}

it's not printing out the first two letters. so the print result would be:

st and so on
Re: open java console when executing
Reply #13 - Oct 23rd, 2009, 4:37am
 
Yes indeed, that's strange. Anyway, intead of trying to seek out the error (probably something to do with the way the buffer is read), I sought out another console implementation, and adapted it.

Source: http://www.comweb.nl/java/Console/Console.html

So here it is:


Code:
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

Console console;

void setup()
{
 size(800, 600);

 try
 {
   console = new Console();
 }
 catch(Exception e)
 {
 }

 println("This is some text printed to the console");
}

void draw()
{
 println("test and so on");
 delay(100);
}

public class Console extends WindowAdapter implements WindowListener, ActionListener, Runnable
{
 private JFrame frame;
 private JTextArea textArea;
 private Thread reader;
 private Thread reader2;
 private boolean quit;

 private final PipedInputStream pin=new PipedInputStream();
 private final PipedInputStream pin2=new PipedInputStream();

 Thread errorThrower; // just for testing (Throws an Exception at this Console

 public Console()
 {
   // create all components and add them
   frame=new JFrame("Java Console");
   Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
   Dimension frameSize=new Dimension((int)(screenSize.width/2),(int)(screenSize.height/2));
   int x=(int)(frameSize.width/2);
   int y=(int)(frameSize.height/2);
   frame.setBounds(x,y,frameSize.width,frameSize.height);

   textArea=new JTextArea();
   textArea.setEditable(false);
   JButton button=new JButton("clear");

   frame.getContentPane().setLayout(new BorderLayout());
   frame.getContentPane().add(new JScrollPane(textArea),BorderLayout.CENTER);
   frame.getContentPane().add(button,BorderLayout.SOUTH);
   frame.setVisible(true);

   frame.addWindowListener(this);
   button.addActionListener(this);

   try
   {
     PipedOutputStream pout=new PipedOutputStream(this.pin);
     System.setOut(new PrintStream(pout,true));
   }
   catch (java.io.IOException io)
   {
     textArea.append("Couldn't redirect STDOUT to this console\n"+io.getMessage());
   }
   catch (SecurityException se)
   {
     textArea.append("Couldn't redirect STDOUT to this console\n"+se.getMessage());
   }

   try
   {
     PipedOutputStream pout2=new PipedOutputStream(this.pin2);
     System.setErr(new PrintStream(pout2,true));
   }
   catch (java.io.IOException io)
   {
     textArea.append("Couldn't redirect STDERR to this console\n"+io.getMessage());
   }
   catch (SecurityException se)
   {
     textArea.append("Couldn't redirect STDERR to this console\n"+se.getMessage());
   }

   quit=false; // signals the Threads that they should exit

   // Starting two seperate threads to read from the PipedInputStreams
   //
   reader=new Thread(this);
   reader.setDaemon(true);
   reader.start();
   //
   reader2=new Thread(this);
   reader2.setDaemon(true);
   reader2.start();

   // testing part
   // you may omit this part for your application
   //
   System.out.println("Hello World 2");
   System.out.println("All fonts available to Graphic2D:\n");
   GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
   String[] fontNames=ge.getAvailableFontFamilyNames();
   for(int n=0;n<fontNames.length;n++)  System.out.println(fontNames[n]);
   // Testing part: simple an error thrown anywhere in this JVM will be printed on the Console
   // We do it with a seperate Thread becasue we don't wan't to break a Thread used by the Console.
   System.out.println("\nLets throw an error on this console");
   errorThrower=new Thread(this);
   errorThrower.setDaemon(true);
   errorThrower.start();
 }

 public synchronized void windowClosed(WindowEvent evt)
 {
   quit=true;
   this.notifyAll(); // stop all threads
   try {
     reader.join(1000);
     pin.close();  
   }
   catch (Exception e){
   }
   try {
     reader2.join(1000);
     pin2.close();
   }
   catch (Exception e){
   }
   System.exit(0);
 }

 public synchronized void windowClosing(WindowEvent evt)
 {
   frame.setVisible(false); // default behaviour of JFrame
   frame.dispose();
 }

 public synchronized void actionPerformed(ActionEvent evt)
 {
   textArea.setText("");
 }

 public synchronized void run()
 {
   try
   {
     while (Thread.currentThread()==reader)
     {
       try {
         this.wait(100);
       }
       catch(InterruptedException ie) {
       }
       if (pin.available()!=0)
       {
         String input=this.readLine(pin);
         textArea.append(input);
         textArea.setCaretPosition(textArea.getDocument().getLength());

       }
       if (quit) return;
     }

     while (Thread.currentThread()==reader2)
     {
       try {
         this.wait(100);
       }
       catch(InterruptedException ie) {
       }
       if (pin2.available()!=0)
       {
         String input=this.readLine(pin2);
         textArea.append(input);
         textArea.setCaretPosition(textArea.getDocument().getLength());

       }
       if (quit) return;
     }
   }
   catch (Exception e)
   {
     textArea.append("\nConsole reports an Internal error.");
     textArea.append("The error is: "+e);
   }
 }

 public synchronized String readLine(PipedInputStream in) throws IOException
 {
   String input="";
   do
   {
     int available=in.available();
     if (available==0) break;
     byte b[]=new byte[available];
     in.read(b);
     input=input+new String(b,0,b.length);
   }
   while( !input.endsWith("\n") &&  !input.endsWith("\r\n") && !quit);
   return input;
 }
}
Re: open java console when executing
Reply #14 - Oct 23rd, 2009, 4:45am
 
cool, thx again!
Pages: 1 2