Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 842
K J Avatar asked Apr 12 '26 06:04

K J


2 Answers

Shutdown hooks don't get run by Eclipse when shutting down a process: https://bugs.eclipse.org/bugs/show_bug.cgi?id=38016

That bug report is resolved as Won't fix

I realise that its an old bug and so maybe things have changed since then.

like image 101
Channing Walton Avatar answered Apr 14 '26 00:04

Channing Walton


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

like image 21
Kim Stebel Avatar answered Apr 14 '26 00:04

Kim Stebel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!