Search code examples
javaprintlnsystem.outsystem.err

Override System.out/err.println


Since the deprecation of the JDBC Bridge in Java, I've been making use of UCanAccess for JDBC connections. However UCanAccess does not throw exceptions in the form of normal try-catches, but immediately prints error lines to the console. As my users will not be accessing the software through the console, I would like to redirect all my console outputs to a JOptionPane message dialog, in order for my users to see error prints instead of it simply going to waste in the non-visible console. Is this possible? I have tried System.setErr, but I can only get to override the write() method, outputting only a int?


Solution

  • UCanAccess throws exceptions in "the normal form", so your question is a bit misleading and your assumptions are wrong.

    Also it uses SQLWarning while loading the db, see the jdbc interface about it (e.g. if it can't load a query). The UCanAccess console just prints them.

    At Last, in the case of database corruption, data integrity problems or other very high severity problems jackcess or ucanaccess may print on the System.err. Obviously it may happen just if they are not blocking despite the severity. It's just to be sure that error message is logged without blocking a succesful db loading with an Exception(so I can't use a SQLWarning for them ).

    For those specific issues you can redirect the System.err messages to a JOptionPane, but they are often very technical errors about mdb/accdb format, so I think it isn't a good idea show them in a user interface.

    To re-direct the system.err in a specific error file instead(for subsequent problem solving) is much better.