Indonesia’s disaster mitigation agency said residents on the Banten coast should “immediately evacuate to higher ground”.
The United States Geological Survey (USGS) said it was centered 151km (94 miles) from Banten province, off the island’s southwest coast, and hit at a depth of 42.8km (26.5 miles).
Buildings in Jakarta swayed for nearly a minute during the evening quake.
TV footage showed workers running out of high-rise buildings and offices.
Radio and TV reports said people felt the strong quake in Banten province and in Lampung province along the southern part of Sumatra island.
The temblor caused a panic among residents in several cities and villages, but there were no immediate reports of injuries or damage
The quake could also be felt in other cities such as Yogyakarta on Java island.
“It was so scary,” said Gustiani Pratiwi, who was carrying two children near an apartment block in Jakarta when she felt the quake strongly.
Last year, a tsunami hit the city of Palu in Sulawesi island, killing thousands, while a crater collapse at the Anak Krakatau volcano triggered a tsunami that killed at least 430 people in an area near the latest quake.
At Carita beach in Banten, which was affected by the Anak Krakatau quake, a resident described the alarm in the area.
“We are panicking a lot,” Sandi, a resident of Carita beach, told Metro TV by telephone.