Illegal state exception when adding shutdown hook