Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

How do I printStackTrace to PrintStream

Posted on 2008-11-17
9
Medium Priority
?
1,795 Views
Last Modified: 2012-08-14
I'm writing a JSP file that query the DB and process the results, i have put my code within try-catch and i want to catch(Throwable t). My question is how can I print the stackTrace to a PrintStream then to a text file.

Thank you for your assistance.

Ayad
0
Comment
Question by:Alqershi
  • 5
  • 2
7 Comments
 
LVL 27

Accepted Solution

by:
mrcoffee365 earned 100 total points
ID: 22979054
This is one example:
http://www.rgagnon.com/javadetails/java-0029.html

Basically, you create a PrintWriter and pass it to e.printStackTrace .
0
 
LVL 28

Expert Comment

by:rrz
ID: 22979212
Please try this JSP. Look in your web app's root folder for the file errorStream.txt  .
<%@ page import="java.io.*"%>
<%
  File f = new File(application.getRealPath("/errorStream.txt"));
  try{
      String s = null;
      s.trim();
  }catch(Exception e){
                      PrintStream ps = new PrintStream(f);
                      System.setErr(ps);
                      e.printStackTrace();
                      ps.close();
   }
%>
hello

Open in new window

0
 
LVL 28

Assisted Solution

by:rrz
rrz earned 100 total points
ID: 22979775
On second thought, mrcoffee's idea is better.  Try the following.
<%@ page import="java.io.*"%>
<%
  try{
 
      String s = null;
      s.trim();
 
     }catch(Exception e){
                        StringWriter sw = new StringWriter();
                        PrintWriter pw = new PrintWriter(sw);
                        e.printStackTrace(pw);
                        File f = new File(application.getRealPath("/errorStream.txt"));
                        FileWriter fw = new FileWriter(f,true);
                        fw.write(sw.toString());
                        fw.close();
      }
%>
hello2

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 28

Expert Comment

by:rrz
ID: 22979822
maybe add
pw.close();
0
 
LVL 28

Expert Comment

by:rrz
ID: 22981574
You should have split the points. It was mrcoffee's idea.
0
 
LVL 28

Expert Comment

by:rrz
ID: 22981585
I made a request for attention.
0
 
LVL 27

Expert Comment

by:mrcoffee365
ID: 22985951
Thanks rrz@871311.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you a startup company? Being a startup, you may be using shared hosting, or maybe even dedicated hosting. But have you ever given a thought to using cloud computing now? Yes, don’t be surprised, it is possible for startups to opt for cloud compu…
Why WooCommerce is one of the majorly favored choices when it comes to having an eCommerce store. This article will acquaint you with some reasons that I believe make it one of the best eCommerce platforms available.
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question