How can I run my java program in a timed loop? -
i trying program run every 5 minutes. can tell me how achieve this? i'm new java, here's have far. program prints processes text file , calls text file display data. nice have run every 5 minutes.
import java.io.*; import java.util.stringtokenizer; public class getprocesses { private string getprocesslistdata() { process p; runtime runtime; string process = null; try { system.out.println("allow me hack system , see have open..."); //this let access windows in order make program possible runtime = runtime.getruntime(); p = runtime.exec("tasklist"); inputstream inputstream = p.getinputstream(); inputstreamreader inputstreamreader = new inputstreamreader(inputstream); bufferedreader bufferedreader = new bufferedreader(inputstreamreader); string line = bufferedreader.readline(); process = "&"; while (line != null) { line = bufferedreader.readline(); process += line + "&"; } //as learned in class, have close whatever open bufferedreader.close(); inputstreamreader.close(); inputstream.close(); //the following output quote portal system.out.println("system has been modified. thankyou , goodbye."); } catch (ioexception e) { system.out.println("uh oh, looks found ioe"); e.printstacktrace(); } return process; } private void showprocessdata() { try { string proc = getprocesslistdata(); outputstreamwriter outputstreamwriter = new outputstreamwriter(new fileoutputstream("processlist.txt")); bufferedwriter bufferedwriter = new bufferedwriter(outputstreamwriter); stringtokenizer st = new stringtokenizer(proc, "&"); while (st.hasmoretokens()) { bufferedwriter.write(st.nexttoken()); bufferedwriter.newline(); } //this close outputstreams bufferedwriter.close(); outputstreamwriter.close(); } catch (ioexception ioe) { ioe.printstacktrace(); } } @suppresswarnings("resource") public static void main(string[] args) throws ioexception { getprocesses gpl = new getprocesses(); gpl.showprocessdata(); bufferedreader in = new bufferedreader(new filereader("processlist.txt")); string line; while((line = in.readline()) != null) { system.out.println(line); } } }
any appreciated, thankyou.
you should set scheduled task (windows) or cron job (unix) launch command "java -jar myprogram.jar" every 5 minutes.
Comments
Post a Comment