An 18-year-old student opened fire in a lecture hall at Heidelberg University in southwestern Germany on Monday, killing a young woman and injuring three others before fleeing the scene and turning the weapon on himself.

The gunman fired shots "wildly" around the amphitheatre, a police spokesman told AFP.

The shooting took place during an organic chemistry class for bioscience students, a course the assailant himself was enrolled in, university president Bernhard Eitel told reporters.

A major police operation was under way at the Heidelberg University after a shooting A major police operation was under way at the Heidelberg University after a shooting Photo: DPA via AFP / R. PRIEBE

The motive for the attack was not immediately clear but police said there were early indications the gunman had previously suffered from psychiatric problems.

The assailant had sent an alarming text message to his father shortly before the rampage.

In the WhatsApp message, the gunman wrote that "people have to be punished now", Mannheim police chief Siegfried Kollmar told a press conference. The message also said: "He doesn't want to be buried in a cemetery, but at sea."

A map of Germany locating Heidelberg A map of Germany locating Heidelberg Photo: AFP / STAFF

The shooting shocked the picturesque town of Heidelberg and left students traumatised.

Thirty people were attending the lecture at the Neuenheimer Feld campus when the gunman burst in and fired multiple shots using "a long weapon", Kollmar said.

Police received the first emergency calls from inside the lecture hall at 12:24 pm and officers were at the scene within 10 minutes, he added.

A map of Germany locating Heidelberg A map of Germany locating Heidelberg Photo: AFP / STAFF

They discovered the gunman's body outside the building, after he killed himself.

The rampage left four people wounded, including a young woman, 23, who died of her injuries in hospital several hours later.

The three others suffered wounds to the legs, back and face, Kollmar said.

A map of Germany locating Heidelberg A map of Germany locating Heidelberg Photo: AFP / STAFF

Chancellor Olaf Scholz voiced shock at the assault, and said his thoughts were with the victims and their relatives. "It breaks my heart to hear such news," he told journalists.

Kollmar said the gunman, who was not previously known to police, had acted alone and that he apparently bought the weapons abroad.

Police discovered the gunman's body outside the lecture hall building, after he killed himself Police discovered the gunman's body outside the lecture hall building, after he killed himself Photo: AFP / Daniel ROLAND

The assailant had carried two rifles onto the campus, said Kollmar, adding that investigators found more than 100 rounds of ammunition in the backpack he was carrying.

Officers have searched the gunman's home in the nearby city of Mannheim, he added.

The shooting triggered a major police operation at the university's Neuenheimer Feld campus, with police on Twitter urging people to steer clear of the area "so that rescue workers and emergency services can travel freely".

Police deployed sniffer dogs around the campus, and investigators were seen examining a rifle lying next to a beige backpack.

The medieval town of Heidelberg in the state of Baden-Wuerttemberg has a population of 160,000 people.

Heidelberg University, founded in 1386, is Germany's oldest university and one of the most prestigious in Europe.

The state's interior minister, Thomas Strobl, expressed his sympathies for those affected by the "terrible event" and urged students in distress to make use of the mental health support on offer.

"Universities and the city of Heidelberg will remain spaces free of fear where science can flourish and where young people can prepare for the rest of their lives," he said.

The university resumed in-person classes in October after months of online lessons because of the coronavirus pandemic.

Germany has been hit in recent years by a spate of attacks, mostly perpetrated by jihadists or far-right militants.

School shootings however are relatively rare in a country with some of the strictest gun laws in Europe.

In 2009, a former pupil killed nine students, three teachers and three passers-by in a school shooting at Winnenden, also in Baden-Wuerttemberg. The gunman then killed himself.

In 2002, a 19-year-old former student, apparently in revenge for having been expelled, gunned down 16 people including 12 teachers and two students at a school in the central German city of Erfurt. He too then killed himself.

Both massacres were carried out with legal weapons and spurred Germany to tighten gun laws.