Search code examples
eclipsescalascala-ide

Scala println doesn't work while termination in IDE


I'm writing a server that runs in a loop and terminates in case of SIGINT(ctrl-c) or terminate button pressed in the console window of Eclipse IDE. I want it to shutdown gracefully printing out termination logs. But the problem is that println doesn't seem to work while shutdown sequence triggered by pressing the terminate button in Eclipse IDE. Look at the simple code below:

object Test extends App {
  println("start")
  Runtime.getRuntime().addShutdownHook(new Thread {
    override def run = println("shutdown")
  })
  synchronized { wait }
}

It works well with the command line scala tool. Both messages "start" and "shutdown" are printed when I hit ctrl-c. But the "shutdown" message isn't printed when I run it in Eclipse IDE and hit the terminate button of the console window. It just terminates silently. I've verified that everything else in the shutdown hook runs correctly while termination. It's only println that doesn't work.

Any idea about this? I need to print out termination messages for logging.
Thanks for your help in advance!


Solution

  • Try logging the shutdown messages to a file instead. Eclipse probably closes its end of the stdout pipe before the program is really terminated.