A man who opened fire in a German courtroom, killing a prosecutor, was sentenced to life in prison on Thursday for murder and three counts of attempted murder.

The 55-year-old former businessman, identified only as Rudolf U., was in the dock in the southern German city of Dachau on embezzlement charges in January when he suddenly pulled out a weapon and fired three shots at the prosecutor, 31.

Issuing the life sentence, presiding judge Martin Rieder said the prosecutor's death was "senseless" and had caused a "painful loss" for his family.

A life sentence in Germany means a maximum of 15 years, but Rieder said U. should not be given the possibility of parole.

His lawyer said he would appeal.

The killer had also intended to murder the judge in the Dachau courtroom and shown little concern for the lives of his own defence lawyer and a court clerk, Rieder said, finding him also guilty of three counts of attempted murder.

He had acted out of "hatred for the justice system", the court said, citing something U. had told friends: "You need to go in there with a Kalashnikov. I'll bump them off."

The killing shocked Germany and sparked an intense debate about courtroom security.

In April 2009, a 60-year-old gunman killed a female family member and then himself at a courthouse hearing over an inheritance dispute in the southern city of Landshut.

And in July of the same year, a German man stabbed and killed a pregnant headscarved Egyptian woman in the eastern city of Dresden in a case which also provoked outrage in the Muslim world.

That killer, Alex Wiens, was later sentenced to life imprisonment.