Running a Java JAR File with Cron
Call java with its absolute path and -jar because cron's PATH usually lacks it. Change into the app directory first so relative config and log paths resolve, and redirect output to catch stack traces.
The crontab line
0 1 * * * cd /opt/app && /usr/bin/java -jar app.jar >> /var/log/app.log 2>&1
Find your java path with which java; on many systems it is /usr/bin/java or under /usr/lib/jvm/.../bin/java.
Set JAVA_HOME and memory flags
0 1 * * * JAVA_HOME=/usr/lib/jvm/java-17 /usr/lib/jvm/java-17/bin/java -Xmx512m -jar /opt/app/app.jar
Pass arguments to the JAR
Application arguments go after the jar: -jar app.jar --profile prod. JVM flags like -Xmx go before -jar.
FAQ
What cron line runs a JAR file?
0 1 * * * cd /opt/app && /usr/bin/java -jar app.jar >> /var/log/app.log 2>&1. Use the absolute java path and cd into the app dir.
Why does java: command not found appear in cron?
Cron's PATH is minimal. Use the full path to java (which java to find it) instead of relying on PATH.
Where do JVM flags go in a cron JAR command?
JVM flags like -Xmx512m go before -jar; application arguments go after the jar filename.