A Japanese boy abandoned in a dense forest by his parents for being naughty was found alive and unharmed on Friday, nearly a week after his disappearance set off a massive search that kept the nation riveted.

Seven-year-old Yamato Tanooka was discovered in a building on a Japanese military base around 4 km (2.5 miles) from where he disappeared last Saturday after his parents left him by the side of a road, reportedly as discipline for throwing stones at cars.

Source: b"A member of Japan Ground Self-Defense Force (JGSDF) shows the mattress which a 7-year-old boy who went missing on May 28, 2016 after being left behind by his parents, was found alive, used inside a building at JGSDF's Komagatake exercise area, in Shikabe town | Source: Reuters"

"One of our soldiers was preparing for drills this morning and opened the door of a building on the base, and there he was," a member of Japan's Self-Defence Forces told NHK national television.

"When he asked 'are you Yamato?' the boy said yes. Then he said he was hungry, so the soldier gave him some water, bread and riceballs."

Yamato was taken to hospital for checks but was healthy except for low body temperature and would be kept overnight as a precaution, a doctor told a news conference.

The boy's parents first said he disappeared while they foraged for edible plants, but later told police they had left him by the road to discipline him after he threw stones at people and cars.

They said when they drove back a few minutes later the boy had disappeared. The area is so remote that residents of the region say they rarely go through it.

Source: b"A building where a 7-year-old boy who went missing on May 28, 2016 \xc2\xa0is seen at Japan Ground Self-Defense Force's Komagatake exercise area, in Shikabe town on the northernmost Japanese main island of Hokkaido, Japan | Source: Reuters"

The boy somehow managed to survive for nearly a week in the densely forested area, where night temperatures fell as low as 7 degrees Celsius (45 Fahrenheit) and heavy rain had fallen.

Yamato said that he had "walked through the mountains" until he found the building, broadcaster NHK said. He drank water and slept on mattresses spread on the floor.

The search for Yamato gripped Japan. At its peak, it involved several hundred rescuers beating through heavy bush and shouting, as well as soldiers on motorbikes and police on horseback.

News programmes gave regular updates throughout the week, and NHK sent a news flash when he was found.

The incident set off a flood of social media comment, with most posters lambasting the parents for their carelessness. On Friday, most expressed relief.

"To be honest, I was certain of a sad result. As the parent of a 7-year-old myself, all I can say is that I am really, really happy," one wrote.