Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Output runtime error to a file

Status
Not open for further replies.

iSeriesCodePoet

Programmer
Jan 11, 2001
1,373
US
I am working on a program that works fine up to a point at which there is so many errors that in Win2k, the first part of the error message is not seen. I have tried to pipe it to a file, but it don't work, it only works for the result.

Any help would be appreciated. Mike Wills
RPG Programmer (but learning Java)

"I am bad at math because God forgot to include math.h into my program!"
 
Hi Koldark,

Do you mean runtime errors or compiler errors? Runtime errors are easy to write to the file: Just add try - catch to your source and write the error inside the catch-block
E.g.
try
{
do something...
}
catch(Exception e)
{
somewriter.println(e.toString());
}


But if you mean the compiler error messages, to be honest, I don't know how to redirect those ;)
But here is a some kind of hack ;) (hehe)
That program should work and it compiles the
source you have given & writes the compiler error messages to file. (But seriously, I have never thought how to redirect that compiler input in windows console... if somebody knows, please tell...)
_____________________________________________

import java.io.*;

public class RTJavac
{
public static void main(String[] argv)
{
if(argv.length == 0)
{
System.out.println(&quot;Usage: java RTJavac <java source name>&quot;);
return;
}

try
{
String compilerInput = null;

// This is the file where compiler output will be written
FileWriter fw = new FileWriter(&quot;complierlog.txt&quot;, false); // No append
PrintWriter pw = new PrintWriter(fw);

// execute javac to compile the source given as a parameter
Process p = Runtime.getRuntime().exec(&quot;javac &quot; + argv[0]);

// Get the process' Error stream
InputStream is = p.getErrorStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));

// Print compiler errors to the screen and write to file
System.out.println(&quot;Compiler errors:&quot;);
while((compilerInput = br.readLine()) != null)
{
System.out.println(compilerInput);
pw.println(compilerInput);
pw.flush();
}


}
catch(IOException ioe)
{
System.out.println(&quot;Following error has occured: &quot; + ioe.toString());
}

}
}
 
Thanks. I will try 'try'. ;-) Mike Wills
RPG Programmer (but learning Java)

&quot;I am bad at math because God forgot to include math.h into my program!&quot;
 
I am talking about a runtime error. I am using JDK 1.3.1. Mike Wills
RPG Programmer (but learning Java)

&quot;I am bad at math because God forgot to include math.h into my program!&quot;
 
Vepo, that didn't work. Here is my code:

import java.io.*;
import java.util.*;
import java.lang.*;
import java.text.*;

public class asgn4
{
static long count;

public static void main (String args[])
{
Integer a = new Integer(args[0]);
Integer b = new Integer(args[1]);

System.out.println(&quot;Value is &quot; + ack(a.longValue(), b.longValue()) + &quot; with &quot; + count + &quot; calls.&quot;);
}

public static long ack(long x, long y)
{
count++;

try
{
if(x > 0)
if(y > 0)
return ack(x-1, ack(x, y-1));
else
return ack(x-1, 1);
else
return y + 1;
}
catch(Exception ioe)
{
System.out.println(&quot;Following error has occured: &quot; + ioe.toString());
return 0;
}
}
}


When I run it passing the parms: 4 1 is when I get the error. When I run it using 3 1, it works perfectly. Any suggestions? Mike Wills
RPG Programmer (but learning Java)

&quot;I am bad at math because God forgot to include math.h into my program!&quot;
 
Hi,

That example cleared the situation, you are getting a java.lang.StackOverflowError because of that recursive call (fast ctrl+c solved situation), so that's why there is no help from catching the Exception. That Error causes JVM to halt, after it prints the stackTrace, which is quite long in this situation because of the recursion ;)
(See API documentation about java.lang.StackOverflowError)


Here is sollution for the Error catching:
import java.io.*;
import java.util.*;
import java.lang.*;
import java.text.*;

public class asgn4
{
static long count;

public static void main (String args[])
{
Integer a = new Integer(args[0]);
Integer b = new Integer(args[1]);

try
{
System.out.println(&quot;Value is &quot; + ack(a.longValue(), b.longValue()) + &quot; with &quot; + count + &quot;

calls.&quot;);
}
catch(Error e)
{
System.out.println(e.toString());
}

}

public static long ack(long x, long y) throws Error
{
count++;

if(x > 0)
if(y > 0)
return ack(x-1, ack(x, y-1));
else
return ack(x-1, 1);
else
return y + 1;

}
}

-Vepo
 
Thank you. That helped. Now the question is, how do you make the stack larger? Mike Wills
RPG Programmer (but learning Java)

&quot;I am bad at math because God forgot to include math.h into my program!&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top