-
April 8th, 2010, 04:33 PM
#1
[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;
}
}
-
April 9th, 2010, 06:58 AM
#2
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.
-
April 9th, 2010, 09:57 AM
#3
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
-
April 9th, 2010, 11:48 AM
#4
Re: help with delay, execute second system command
------
If you are satisfied with the responses, add to the user's rep!
-
April 9th, 2010, 01:25 PM
#5
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...
-
April 9th, 2010, 01:50 PM
#6
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.
-
April 9th, 2010, 01:55 PM
#7
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?
-
April 9th, 2010, 02:24 PM
#8
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.
-
April 9th, 2010, 02:29 PM
#9
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;
}
}
-
April 9th, 2010, 03:26 PM
#10
Re: help with delay, execute second system command
Some advice:
- When posting code please use code tags.
- 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.
- 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.
-
April 9th, 2010, 04:14 PM
#11
Re: help with delay, execute second system command
Originally Posted by keang
Some advice:
- When posting code please use code tags.
- 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.
- 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
-
April 9th, 2010, 04:21 PM
#12
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.
-
April 9th, 2010, 04:30 PM
#13
Re: help with delay, execute second system command
Originally Posted by keang
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.
-
April 9th, 2010, 04:43 PM
#14
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?
-
April 9th, 2010, 04:47 PM
#15
Re: help with delay, execute second system command
Originally Posted by keang
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|