Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-07-2008, 06:02 PM
Member
 
Join Date: Jun 2008
Posts: 14
jmHoekst is on a distinguished road
[SOLVED] error handling
greetings,

i have a java program calling a .bat file on an XP system.

Runtime.getRuntime().exec("c:\\myBatch.bat");

the batch file does a bunch of stuff, including things that may often incur unwanted popup windows or prompts that I would like automatically bypassed.

i am wondering if i can somehow time how long it has taken for the batch file to finish and return to my java program. if it has taken more than, say, five minutes, then my java program should simply move on. my program runs overnight, and i don't want to come to my pc in the morning and have to click on an "OK" prompt, then wait for it to finish.

any tips, suggestions or questions are welcome, thanks for looking
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-07-2008, 06:23 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SW MO, USA
Posts: 1,571
Norm is on a distinguished road
Sounds like a job for Threads. Put the batch file caller on one thread and then have another thread that waits some time say 5 minutes and then checks if the batch file thread has finished.
Fancier code could use wait() and notify() and synchronized methods to coordinate the two threads.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-07-2008, 06:53 PM
Senior Member
 
Join Date: Jun 2008
Posts: 317
masijade is on a distinguished road
Not to mention that you're going to need some (additional) threads to read the output and error streams of the process or it is likely to simply hang anyway.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 07-07-2008, 06:58 PM
Member
 
Join Date: Jun 2008
Posts: 14
jmHoekst is on a distinguished road
thanks for the tips,

Do either of you have any syntactical help on using one thread to determine if another thread is finished?
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 07-07-2008, 07:23 PM
Senior Member
 
Join Date: Jun 2008
Posts: 317
masijade is on a distinguished road
isAlive()

Thread (Java Platform SE 6)

The API docs are your friend
JDK 6 Documentation
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 07-07-2008, 09:03 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SW MO, USA
Posts: 1,571
Norm is on a distinguished road
Use a boolean flag. Set true just before the thread exists.
Test if set in the other thread.
To use isAlive() you'd need a reference to the other thread via a variable which cost as much as referencing the boolean flag.
Of course, there are other ways to get references by using names and ThreadGroups.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 07-07-2008, 09:34 PM
Senior Member
 
Join Date: Jun 2008
Posts: 317
masijade is on a distinguished road
For the one thread to check whether the other thread is alive, whether with the isAlive method or with a boolean, a reference to the other thread is still needed, so I don't know what you were getting at with that sentence.

So, why "roll your own" when something finished already exists.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 07-07-2008, 09:40 PM
Member
 
Join Date: Jun 2008
Posts: 14
jmHoekst is on a distinguished road
I have been using a new thread to make the batch call, and thus have to give it a name. I then use the main thread to check if it is done within 5 minutes. I pretty much have it all figured out now, but I do not know how to terminate the thread if it has failed to return. Java tells me that Thread.stop() is unsafe.

Also, how do i reference the Main thread? Can I reference it from itself?
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 07-07-2008, 11:44 PM
Senior Member
 
Join Date: Jun 2008
Posts: 317
masijade is on a distinguished road
Lesson: Concurrency (The Java™ Tutorials > Essential Classes)

You use call interuppt against a thread, and the thread that gets interuppted needs to catch that and clean itself up, then stop (or even continue again, depends on what you're doing and why you interuppted).
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 07-07-2008, 11:46 PM
Member
 
Join Date: Jun 2008
Posts: 14
jmHoekst is on a distinguished road
awesome.... thanks for the tips guys, i have it working perfectly!

thanks again
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use JDBC Template classes to control basic JDBC processing and error handling Java Tip Java Tips 0 04-01-2008 11:17 AM
Handling Cookies in JSP (III) JavaForums Java Blogs 0 03-31-2008 05:50 PM
SQL error handling example Java Tip Java Tips 0 02-12-2008 10:38 AM
session handling priyanka_t JavaServer Pages (JSP) and JSTL 9 12-17-2007 10:11 AM
How to use JDBC Template classes to control basic JDBC processing and error handling JavaBean Java Tips 0 09-28-2007 01:56 PM


All times are GMT +3. The time now is 05:02 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org