How do I make my jar application generate a crash log file?

crash-reports, jar, java

Solution

I used this solution in the end, which works exactly like I want it to. Everytime the application crashes it saves the exception in a text file in a subfolder named "crashlogs" with the date and time as filename.

Just added this code at the start of my main function

Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
    @Override
    public void uncaughtException(Thread t, Throwable e) {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");

        String filename = "crashlogs/"+sdf.format(cal.getTime())+".txt";

        PrintStream writer;
        try {
            writer = new PrintStream(filename, "UTF-8");
            writer.println(e.getClass() + ": " + e.getMessage());
            for (int i = 0; i < e.getStackTrace().length; i++) {
                writer.println(e.getStackTrace()[i].toString());
            }

        } catch (FileNotFoundException | UnsupportedEncodingException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

    }
});

Problem

I've made an application and exported it as jar file. If the jar-application crashes for some reason I want it to generate a crash-log text file with the error. If I run the application in Eclipse, then eclipse always tells me where it crashed, can I get the same kind of message in a text-file instead when running the application as a jar? I run my jar-file by typing this: java -jar MyApplication.jar Can I add something to this command in order to generate a crash-log if a crash occurs? Thanks!

Original source

Related problems