0

My code is like this:

    @WebListener
public class MyListener implements ServletContextListener {

@Override
public void contextDestroyed(ServletContextEvent arg0) {
}

@Override
public void contextInitialized(ServletContextEvent arg0) {
    new Thread(new Runnable() {

        @Override
        public void run() {
            throw new RuntimeException("sdfsfds");                  
        }
    }).start();
}   

}

This is my minimum deployment to tomcat 8.0.5, nothing gets printed in the console. Can anybody give some clue?

Thanks

Architucas
  • 129
  • 10

3 Answers3

1

There is no console in server so you are not getting anything

But you will get the statements in catalina.out file located in the logs folder.

Note: if your server is linux machine then you will the sysout statements will be printed in the catalina.out file

If you server is in windows machine then you will not get sysout statements in catalina.out file.

You have to manually redirect the console statements to catalina.out file. Follow the below steps

  1. create a file and name it as catalinaRedirectLog.bat
  2. Open it in notepad and type catalina run > ..\logs\catalina.out 2<&1
  3. save this file
  4. run shutdown.bat
  5. run catalinaRedirectLog.bat to start the tomcat,dont run startup.bat from now onwards

I would strongly recommend you to use log4 or any other logging frameworks and avoid using sysout statements

SpringLearner
  • 13,738
  • 20
  • 78
  • 116
  • well, I'm running tomcat from eclipse using eclipse plugin, and normally, I always get the console output in my console tab in eclipse. I also tried writing to log, still nothing, instead, I was redirected to debug perspective as if the thread hung. Thanks – Architucas Dec 07 '15 at 06:00
  • @Architucas are you running as server or debugging on server? – SpringLearner Dec 07 '15 at 06:02
  • when running main, I'm using as Application. It seems that this only happens when I deploy to tomcat. My tomcat is 8.0.5 btw – Architucas Dec 07 '15 at 07:53
  • updated my question. it seems to be tomcat related issue, have you seen this before? – Architucas Dec 07 '15 at 08:13
  • @Architucas there are no sysout statements in your updated post – SpringLearner Dec 07 '15 at 08:28
  • @Architucas use debugger. my answer will be void now as you have completely changed your post – SpringLearner Dec 07 '15 at 08:44
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/97176/discussion-between-architucas-and-springlearner). – Architucas Dec 07 '15 at 08:48
1

System.out and System.err are both redirected to CATALINA_BASE/logs/catalina.out when using Tomcat's startup scripts (bin/startup.sh/.bat or bin/catalina.sh/.bat). Any code that writes to System.out or System.err will end up writing to that file.

By default there are no consoles for the Apache Tomcat server. You can however use Apache log4j which is a Java-based logging utility.

Logging in Apache Tomcat is implemented with the help of Apache Commons Logging library. That library is a thin wrapper above different logging frameworks. It provides Tomcat with the ability to log hierarchically across various log levels without the need to rely on a particular logging implementation.

You can configure Log4j for Tomcat using the instructions provided in logging log4j

EDIT: (From one of your comments) For configuring logs in your Eclipse console you can refer to this thread

Community
  • 1
  • 1
Kurenai Kunai
  • 1,842
  • 2
  • 12
  • 22
  • I have tested printing system.out and it worked perfectly in tomcat. But for this particular case, it seems nothing gets printed. so it's as if it fails only on tomcat? my tomcat is 8.0.5 btw. – Architucas Dec 07 '15 at 07:54
  • updated my question. it seems to be tomcat related issue, have you seen this before? – Architucas Dec 07 '15 at 08:13
0

How are you submitting tasks to ExecutorService es? Through execute() or through submit()?

If you submit the task through submit(), exceptions will be swallowed and they can't reach you.

Have a look at this Code

 if (getState() == RUNNING) { // recheck after setting thread
            V result;
            try {
                result = callable.call();
            } catch (Throwable ex) {
                setException(ex);
                return;
            }
            set(result);
        } else {
            releaseShared(0); // cancel
        }

Have a look how to handle Future task exceptions. You have to override

protected void  afterExecute(Runnable r, Throwable t)
//Method invoked upon completion of execution of the given Runnable.

which is part of ThreadPoolExecutor

Community
  • 1
  • 1
Ravindra babu
  • 37,698
  • 11
  • 250
  • 211