Press enter to see results or esc to cancel.


How to run a task periodically in Java without any Java IDE

In this video I have explained solution for your following queries,
1. How can you run a java program without any JAVA Editor.
2. Schedule any task with your java program.
3. What is the use of Runnable Jar File.
4. How can you create Runnable Jar File.

  1. The below code with create notepad file with date and time inside that.
    Create_NotePad.java

    
    package com.chillyfacts.com;
    import java.io.PrintWriter;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.TimeZone;
    public class Create_NotePad {
        public static void main(String[] args) {    	
        	TimeZone.setDefault(TimeZone.getTimeZone("Asia/Qatar"));
    	SimpleDateFormat format = new SimpleDateFormat("MM_dd_yyyy_hh_mm_ss");
    	Date today4 = new Date();
    	String file_name = format.format(today4);
        	PrintWriter writer;
            try {
                writer = new PrintWriter("e:\\cron_job\\file_"+file_name+".txt", "UTF-8");
                format = new SimpleDateFormat("MM-dd-yyyy hh:mm:ss");
                String current_time = format.format(today4);  	
                writer.println(current_time);
                writer.close();
                System.out.println("finished");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
  2. Now to create a task to run the program in intervals so that the text file will be generated automatically, You have to compress the project in to a runnable jar file.
    To create rannable jar file,
    Right Click on project > Export > Runnable jar file

    
    
  3. Now create a bat file to run the command to execute jar file automatically.
    The command for executing runnable jar file through command prompt is,

    java -jar jar-name.jar

    So the bat file in the example shown in the video will be

  4. Now create a task in windows task scheduler with a custom interval for opening the bat file.

    The task scheduler will open the bat file and will generate the text file on regular intervals

  5. Download the projects shown in video here,

    Create_Note_Cron_Job.rar
    MySQL_DB.rar

Comments

Leave a Comment