AMMAN, Jordan — A prominent and outspoken Jordanian writer on Sunday was shot dead in front of the courthouse where he had been on trial for posting a cartoon deemed offensive to Islam on social media.

A Jordanian security official said the shooter was a former imam, or prayer leader, at a local mosque, and said the man had been motivated by his anger over the cartoon posted to Facebook by writer Nahed Hattar. The shooting was the latest in a string of deadly security lapses in Jordan. Witnesses and police said Hattar, 56, was preparing to enter the courthouse for a hearing when the gunman shot him at close range.

“He was standing at a short distance of about (1 yard) in front of Nahed on the stairs of the Supreme Court,” a witness told the Associated Press on condition of anonymity, fearing repercussions. The official Petra News Agency said Hattar was shot three times.

The witness said the shooter, who was immediately arrested, was wearing a long grey robe and long beard characteristic of conservative Muslims.

Jordanian media, citing anonymous officials, identified the shooter as Riad Abdullah, 49, a former imam in northern Hashmi, a poor neighborhood in Amman. The reports said Abdullah had recently returned from a trip abroad, but gave no further details.