BackendBench
Coffee
Spot the Bug #43

The five-field cron that Spring rejects

A easy Java puzzle in the scheduling and cron mistakes family. One line below contains a real bug. Try to spot it first, then read why it fails and how to fix it.

easyScheduling and cron mistakesbuggy line: 7

Highlighted: line 7. Play this one as a puzzle without the answer.

What is wrong

Spring cron expressions have six fields and start with seconds. "0 9 * * *" has five, so the application fails at startup with an IllegalStateException. Unix crontab has five fields and is a common source of copying mistakes.

The fix

@Scheduled(cron = "0 0 9 * * *")

How to avoid it next time

Count the fields. Spring: second minute hour day month weekday. Use a cron tool to check what an expression means for your framework.

Want the background? Read @Scheduled jobs in the Java concepts library.

About scheduling and cron mistakes

Cron dialects differ in field count and meaning. Spring has six fields starting with seconds, Unix has five, Quartz needs a question mark in one day field, and time zones decide when midnight actually is.

In code review: Count the fields and check which dialect the framework expects. Confirm the time zone and what the day-of-week field numbers mean in that dialect.

More puzzles in this family

๐Ÿž Play today's puzzleAll 132 bugsโ† #42#44 โ†’

Frequently asked questions

What is the bug in "The five-field cron that Spring rejects"?

Spring cron expressions have six fields and start with seconds. "0 9 * * *" has five, so the application fails at startup with an IllegalStateException. Unix crontab has five fields and is a common source of copying mistakes. The faulty line is line 7: @Scheduled(cron = "0 9 * * *")

How do I fix it?

Replace it with: @Scheduled(cron = "0 0 9 * * *"). Count the fields. Spring: second minute hour day month weekday. Use a cron tool to check what an expression means for your framework.

How do I catch scheduling and cron mistakes in code review?

Count the fields and check which dialect the framework expects. Confirm the time zone and what the day-of-week field numbers mean in that dialect.

Is there a new puzzle every day?

Yes. The daily Spot the Bug puzzle picks one of these snippets for each UTC day, with the difficulty set by the weekday. Your streak is stored only in your browser.

More tools you might need