Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-06-2007, 10:20 PM
Member
 
Join Date: Jul 2007
Posts: 7
yelllow4u is on a distinguished road
Trying to catch thread errors
I using StreamGobbler to execute a command line system call. The class seems to work just fine. My problem is that when I try to catch my errors it always comes back "null". Basically I have 3 text boxes that need to be filled in and then the user hits the execute button to run the command. If I mistype something in the first two text boxes (txt_server and txt_server2) teh error that gets returned is "null". If I mistype something in the 3rd text box (txt_VOBStore) I get a usefull error that pops up...which is what I would expect. Below is the code for my main class and my StreamGobbler class. Please take a look over the code and let me know where I might be going wrong...

Code:
String Error = ""; Process p = null; try { p = Runtime.getRuntime().exec(command); // any output? StreamGobbler outputGobbler = new StreamGobbler(p.getInputStream(), "OUTPUT"); StreamGobbler errorGobbler = new StreamGobbler(p.getErrorStream(), "ERROR"); // kick them off outputGobbler.run(); errorGobbler.run(); Error = errorGobbler.Error; int returnVal = p.waitFor(); if (returnVal != 0) { throw ( new Throwable()); } } catch (Exception e){ JOptionPane.showMessageDialog(null, "Could not change file protections: " + e.getMessage(), "Error", JOptionPane.WARNING_MESSAGE); return; } catch (Throwable T){ if (Error != null){ JOptionPane.showMessageDialog(null, "Could not change file protections: " + Error, "Error", JOptionPane.WARNING_MESSAGE); return; }else JOptionPane.showMessageDialog(null, "Could not change file protections: " + T.getMessage(), "Error", JOptionPane.WARNING_MESSAGE); return; }
Here is my SG Class...

Code:
import java.io.InputStream; import java.util.*; import java.io.*; import javax.swing.JOptionPane; /* * streamGobbler.java * * Created on August 2, 2007, 1:32 PM * * To change this template, choose Tools | Template Manager * and open the template in the editor. */ /** * * @author br */ public class StreamGobbler extends Thread { InputStream is; String type; String Error = ""; /** Creates a new instance of streamGobbler */ public StreamGobbler(InputStream is, String type) { this.is = is; this.type = type; } public void run(){ try { InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); String line=null; while ( (line = br.readLine()) != null) Error = br.readLine(); // System.out.println(Error); } catch (IOException ex) { JOptionPane.showMessageDialog(null, "Could not change VOB protections: " + ex.getMessage(), "Error", JOptionPane.WARNING_MESSAGE); return; } } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-07-2007, 03:53 PM
Member
 
Join Date: Aug 2007
Posts: 47
henry_78 is on a distinguished road
Hi,
When u create a new process if u want to know the errors u have to get a reference to the errorStream of that process(which is not your standard input and standard output). Try this code:

InpueStream err = p.getErrorStream();
BufferedReader br = new BufferedReader(new InputStreamReader(err));
try {
String line = br.readline();
while(line != null) {
System.out.println(line);
line = br.readline();
}
}
catch(IOException e) {
}
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 08-07-2007, 04:52 PM
Member
 
Join Date: Jul 2007
Posts: 7
yelllow4u is on a distinguished road
Ha
That makes sense. It also works now. Sometimes I write so much junk I lose track of what is actually happening. I appreciate the help.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
If JNI thread call the java object in another thread, it will crash. skaterxu Advanced Java 0 01-28-2008 09:02 AM
Creating a Thread (extending Java Thread Class) JavaForums Java Blogs 0 12-19-2007 11:31 AM
Try Catch Renegade85 New To Java 4 12-03-2007 06:10 PM
when to use try...catch javaplus New To Java 2 11-18-2007 10:52 PM
Use try and catch zoe New To Java 2 07-25-2007 09:50 PM


All times are GMT +3. The time now is 09:47 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org