AMMAN, Jordan – An 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 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 one meter in front of Nahed on the stairs of the Supreme Court," a witness told the Associated Press on condition of anonymity. The official Petra News Agency said Hattar was shot three times.

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

Jordanian media 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.

Prosecutors charged the man with premeditated murder, committing a deadly terrorist act and possession of an unlicensed weapon.

The security official, speaking on condition of anonymity, said the suspect said he was motivated by the cartoon, which depicted a bearded man, smoking and in bed with two women, asking God to bring him wine and cashews. All physical depictions of God or the prophet Mohammed, even respectful ones, are forbidden under mainstream Islamic ­tradition.

Government spokesman Mohammad Momani condemned the killing as a ­"heinous crime."

But supporters of Hattar said they held the government responsible for the shooting, accusing Prime Minister Hani al-Mulki of creating a hostile atmosphere that encouraged violence against the writer.

Associated Press