CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15
  1. #1
    Join Date
    Apr 2010
    Posts
    8

    [RESOLVED] help with delay, execute second system command

    The following code works fine in my VM station where I am developing a windows-based router. I need, however, to insert a 5 second delay after the GET router_image.exe command, then actually issue a second command to execute the downloaded image. I've tried various methods and none work. I would appreciate some help if someone could offer. Thanks.

    import java.applet.*;
    import java.awt.*;
    import java.io.*;
    public class ROUTERcmd extends Applet {
    public void init() {
    Process f;
    String cmd = "tftp -i 192.168.1.101 GET router_image.exe";
    try {
    f = Runtime.getRuntime().exec(cmd);
    }
    catch(IOException e) {
    e.printStackTrace();
    }
    Process s;
    }
    }

  2. #2
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: help with delay, execute second system command

    The standard way of pausing for 5 seconds is:
    Code:
    Thread.currentThread().sleep(5000);
    If it is a Swing application and don't want it to freeze for 5 seconds, you should put the relevant code into a worker thread (see Concurrency In Swing).

    One must learn by doing the thing; for though you think you know it, you have no certainty, until you try...
    Sophocles
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  3. #3
    Join Date
    Apr 2010
    Posts
    8

    Re: help with delay, execute second system command

    import java.applet.*;
    import java.awt.*;
    import java.io.*;
    public class ROUTERcmd extends Applet {
    public void init() {
    Process f;
    String cmd = "tftp -i 192.168.1.101 GET router_image.exe";
    try {
    f = Runtime.getRuntime().exec(cmd);
    Thread.currentThread().sleep(5000); <--------- inserted but compile errors
    Runtime.getRuntime().exec("router_image.exe");
    }
    catch(IOException e) {
    e.printStackTrace();
    }
    Process s;
    }
    }


    get compile errors:
    ROUTERcmd.java:10 unreported exception java.lang.InterruptedException; must be caught or declared to bet thrown
    Thread.currentThread().sleep(5000);

    1 error
    Error with javac

  4. #4
    Join Date
    Apr 2007
    Posts
    425

    Re: help with delay, execute second system command

    Code:
    5000l
    (letter 'L')
    ------
    If you are satisfied with the responses, add to the user's rep!

  5. #5
    Join Date
    Apr 2010
    Posts
    8

    Re: help with delay, execute second system command

    Are you suggesting the lower case letter "L"? I see no reference to the documentation that makes mention of that....

    Please elaborate...

  6. #6
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: help with delay, execute second system command

    The problem is the method sleep(..) can throw an InterruptedException so you must either wrap the call in a try-catch statement and catch the InterruptedException or declare that your method (the method that contains the line that makes the call to sleep(..)) throws an InterruptedException.

    If you don't fully understand exception handling there are many online tutorials explaining it.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  7. #7
    Join Date
    Apr 2010
    Posts
    8

    Re: help with delay, execute second system command

    thank you for that information. And I will lookup the information you suggested. But I still fail to see what the letter "L" has to do with this, but maybe I'll see it in the documentation?

  8. #8
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: help with delay, execute second system command

    But I still fail to see what the letter "L" has to do with this,
    The sleep(..) method takes a long and 5000 is an int and not a long, but Java will automatically cast it to a long so there is no need to worry about it. However where classes provide overloaded methods you may need to make ensure you are passing in the correct type to ensure the correct method is being executed. To make sure you are passing a long rather than an int you put an l (L) after the literal value.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  9. #9
    Join Date
    Apr 2010
    Posts
    8

    Re: help with delay, execute second system command

    Thank you. This is what I have now but I still cannot compile it:

    import java.applet.*;
    import java.awt.*;
    import java.io.*;
    public class ROUTERcmd extends Applet {
    public void init() {
    Process f;
    String cmd = "tftp -i 192.168.1.101 GET router_image.exe";
    try {
    f = Runtime.getRuntime().exec(cmd);

    }
    catch(IOException e) {
    e.printStackTrace();
    }


    try {
    Thread.currentThread().sleep(5000l);
    Runtime.getRuntime().exec("router_image.exe");;

    }
    catch(IOException e) {
    e.printStackTrace();
    }


    Process s;
    }
    }

  10. #10
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: help with delay, execute second system command

    Some advice:

    1. When posting code please use code tags.
    2. When you have a compile error post the full compile error and stack trace - the error message tells us what the problem is and the stack trace tells us where it is.
    3. When someone tells you how to solve your problem do exactly what they tell you to do before saying it doesn't work. Hint: Read my earlier post again and do what I suggested.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  11. #11
    Join Date
    Apr 2010
    Posts
    8

    Re: help with delay, execute second system command

    Quote Originally Posted by keang View Post
    Some advice:

    1. When posting code please use code tags.
    2. When you have a compile error post the full compile error and stack trace - the error message tells us what the problem is and the stack trace tells us where it is.
    3. When someone tells you how to solve your problem do exactly what they tell you to do before saying it doesn't work. Hint: Read my earlier post again and do what I suggested.
    I thought I was wrapping the sleep in a try-catch statement in the following code:

    Code:
    import java.applet.*;
    import java.awt.*;
    import java.io.*;
    public class ROUTERcmd extends Applet {
    public void init() {
    Process f;
    String cmd = "tftp -i 192.168.1.101 GET router_image.exe";
    try {
    f = Runtime.getRuntime().exec(cmd);
    
    }
    catch(IOException e) {
    e.printStackTrace();
    }
    
    
    try {
    Thread.currentThread().sleep(5000l);
    Runtime.getRuntime().exec("router_image.exe");;
    
    }
    catch(IOException e) {
    e.printStackTrace();
    }
    
    
    Process s;
    }
    }
    but I'm here to learn so any suggestions would help. Here is the error codes from attempting to compile:

    Code:
    ROUTERcmd.java:18: exception java.io.IOException is never thrown in body of corresponding try statement
    catch(IOException e) {
    ^
    ROUTERcmd.java:16: unreported exception java.lang.InterruptedException; must be caught or declared to be thrown
    Thread.currentThread().sleep(10);
                                ^
    2 errors
    Error with javac

  12. #12
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: help with delay, execute second system command

    The error message tells you exactly what is wrong: Firstly it is telling you the code in the try catch block doesn't throw the exception type that you are catching and secondly it is telling you are not catching the exception type that the code is throwing.

    Did you really read my earlier post again - where did I say you had to catch an IOException.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  13. #13
    Join Date
    Apr 2010
    Posts
    8

    Re: help with delay, execute second system command

    Quote Originally Posted by keang View Post
    The error message tells you exactly what is wrong: Firstly it is telling you the code in the try catch block doesn't throw the exception type that you are catching and secondly it is telling you are not catching the exception type that the code is throwing.

    Did you really read my earlier post again - where did I say you had to catch an IOException.
    my mistake... sorry...

    Code:
    import java.applet.*;
    import java.awt.*;
    import java.io.*;
    public class ROUTERcmd extends Applet {
    public void init() {
    Process f;
    String cmd = "tftp -i 192.168.1.101 GET router_image.exe";
    try {
    f = Runtime.getRuntime().exec(cmd);
    
    }
    catch(IOException e) {
    e.printStackTrace();
    }
    
    
    try {
    Thread.currentThread().sleep(5000l);
    Runtime.getRuntime().exec("router_image.exe");
    
    }
    catch(InterruptedException e) {
    e.printStackTrace();
    }
    
    
    Process s;
    }
    }
    now gives:

    Code:
    ROUTERcmd.java:18: <identifier> expected
    catch(InterruptedException) {
                                                ^
    Thank you.
    Last edited by bulgin; April 9th, 2010 at 04:33 PM.

  14. #14
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: help with delay, execute second system command

    It compiles on my system except for a missing IOException from the call to exec (..) you've placed inside the try-catch block surrounding the call to sleep(..). Are you sure this is the code you have tried to compile?
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  15. #15
    Join Date
    Apr 2010
    Posts
    8

    Re: help with delay, execute second system command

    Quote Originally Posted by keang View Post
    It compiles on my system except for a missing IOException from the call to exec (..) you've placed inside the try-catch block surrounding the call to sleep(..). Are you sure this is the code you have tried to compile?
    I have it working now with the following code. Thanks!

    Code:
    import java.applet.*;
    import java.awt.*;
    import java.io.*;
    public class ROUTERcmd extends Applet {
    public void init() {
    Process f;
    String cmd = "tftp -i 192.168.1.101 GET router_image.exe";
    try {
    f = Runtime.getRuntime().exec(cmd);
    
    }
    catch(IOException e) {
    e.printStackTrace();
    }
    
    
    try {
    Thread.currentThread().sleep(5000l);
    
    }
    catch(InterruptedException e) {
    e.printStackTrace();
    }
    
    try {
    Runtime.getRuntime().exec("router_image.exe");
    
    }
    catch(IOException e) {
    e.printStackTrace();
    }
    
    
    Process s;
    }
    }

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured