Results 1 to 10 of 10
  1. #1
    jmHoekst is offline Member
    Join Date
    Jun 2008
    Posts
    14
    Rep Power
    0

    Default [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

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,461
    Rep Power
    25

    Default

    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.

  3. #3
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    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.

  4. #4
    jmHoekst is offline Member
    Join Date
    Jun 2008
    Posts
    14
    Rep Power
    0

    Default

    thanks for the tips,

    Do either of you have any syntactical help on using one thread to determine if another thread is finished?

  5. #5
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    isAlive()

    Thread (Java Platform SE 6)

    The API docs are your friend
    JDK 6 Documentation

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,461
    Rep Power
    25

    Default

    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.

  7. #7
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    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.

  8. #8
    jmHoekst is offline Member
    Join Date
    Jun 2008
    Posts
    14
    Rep Power
    0

    Default

    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?

  9. #9
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    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).

  10. #10
    jmHoekst is offline Member
    Join Date
    Jun 2008
    Posts
    14
    Rep Power
    0

    Default

    awesome.... thanks for the tips guys, i have it working perfectly!

    thanks again

Similar Threads

  1. session handling
    By priyanka_t in forum JavaServer Pages (JSP) and JSTL
    Replies: 10
    Last Post: 08-11-2009, 12:32 PM
  2. Replies: 0
    Last Post: 04-01-2008, 10:17 AM
  3. SQL error handling example
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-12-2008, 09:38 AM
  4. Replies: 0
    Last Post: 09-28-2007, 12:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •