BANGKOK—A Thai court sentenced a university student to 2.5 years in prison on Tuesday for posting a message on Facebook that the court said insulted the country’s king.

A Criminal Court judge found 24-year-old Akkaradet Eiamsuwan guilty of violating Thailand’s lese majeste law, which punishes people who defame, insult or threaten the monarchy.

The ruling said Akkaradet used an alias to post the message on Facebook in March. He was arrested in Bangkok in June and has been in jail since then.

The court said it reduced an original sentence of five years’ imprisonment to 2.5 years because the defendant had confessed to the offence.

Thailand’s lese majeste law is the world’s harshest, providing for jail terms of three to 15 years.

The role of the monarchy has come under closer scrutiny in recent years due to concerns about succession when 86-year-old King Bhumibol Adulyadej reaches the end of his reign. The political turmoil that Thailand has suffered over the past eight years, with various political factions contending for power, is linked to such concerns.

Prime Minister Prayuth Chan-ocha — who led a military coup as army commander in May — is an ardent defender of the monarchy, having served for years in an infantry regiment known as the Queen’s Guard. Prayuth vowed to use a variety of means to halt “those making impetuous, careless comments or with bad intentions undermine the nation’s major institution.”

Read more about: