How to Schedule a Java Program Using Cron Job

  • Post last modified:January 23, 2023
  • Reading time:3 mins read

Use Case

  • In previous article, We have built SFTP Client that mainly read file content which is on the SFTP server.
  • In this article We will automate it and run it every hour to get the latest content of the file on the SFTP server.

Cron Job

  • We can use Cron Job on the server to schedule the execution of the java application.
  • At first we need to package our application as jar file.
  • We need to build the package with dependencies and for that, we need to use the below plugin and add it to the build tag.
<build>
        <finalName>sftp-download</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.1.1</version>

                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <mainClass>SFTPClientWithUSRPWD</mainClass> <!-- main class name -->
                        </manifest>
                    </archive>
                </configuration>

                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
  • Now if we run the maven package command it will generate a jar file with dependency in the target folder.
#remove previous build
mvn clean 

# build jar file
mvn package
  • We can see jar file with dependencies has been created
  • Now we can run this jar file and it will get executed and print the content of the file as shown below.
  • Our code works perfectly fine.

Create bash file

  • We will create a bash file that will execute the above jar execution command.
  • The below code executes the jar file and logs the execution status code to cron_logs.txt.
  • We can verify this by looking at the file contents if the code executed successfully or not.
#!/bin/bash
java -jar /Users/surajmishra/workspace/java-aapl-schedule/target/sftp-download-jar-with-dependencies.jar
echo "executed with code $?" >> /Users/surajmishra/workspace/java-aapl-schedule/schedule/cron_logs.txt

Create a cron job

  • Now we have built a jar file with dependency and all we have to do is to run this jar file on a defined schedule.
  • We can use Crontab, 
crontab -e
  • this will open file in editor mode, we can type our cron expression and command that we want to execute.
  • In the below crontab, we are executing the bash file every 5 minutes. So every 5 minutes we will read the sftp-server file content.
  • After some time if we see the log file we should see the log content.
  • Status code 0 means our cron job executed successfully.

Conclusion

  • In this article, we used a cron job to execute the java application on a defined schedule periodically.
  • In future articles, we will cover some other approaches to achieve the same result.

Bestseller Course

If you want to upskill your Java, you should definitely check out this bestseller course . ( 750,000 + students already enrolled, 4.5 stars rating )

Leave a Reply