Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to catch the event when the program was terminated unexpected?  RSS feed

 
pengpeng lin
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Deal all,

I have met a trouble with java.The requirement looks likes below:

1.The program must run in DOS console.

2.The program must catch the event and write log when someone close the DOS
window unexpectedly.

So there are 2 important point puzzle me:

1.How can we konw the "state" of the program:running or terminated?
2.How can we catch the event when the program was terminated unexpectedly?

Pls guide me to solve the problem,it's a urgency task.

Thanks all!
 
Amit Ghorpade
Bartender
Posts: 2856
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
As far as i know, you can easily have a Java program/ application that executes in a console window.
But the remaining part is something that i think cannot be done programatically in Java, because as you said if someone unexpectedly closes the console or kills the process, which causes the JVM shutdown, in such situations, nothing can be done nor Java can do.


Hope this helps
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to be notified when a program quits (and when that is not possible) were discussed recently in this thread.
 
pengpeng lin
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks! Ulf!

Do you think it's useful for the problem below

1.the application is staring with the console UI,when it accepts the parameter and then start a thread.

2.the thread execute the main job,with logic looks like below:

try{
// main job
}
catch(Exception){
// 1.get the thread state
// 2.if the thread is dead then call the log class
}

3.the log class has a method to deal with the unexpected exception in the folowing steps:

3.1 call Runtime.getRuntime().addShutdownHook(Thread)
3.2 the thread object is used to do some thing such as writes log to
local file

This is the only wany that I can think out.
Thank you very much!!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!