Wales 33-30 Japan
Sam Davies got Wales out of jail with a last-gasp drop goal as Japan were thwarted at the Principality Stadium.
The Ospreys fly-half, who went on as a second-half substitute, struck during the closing seconds to give Wales victory.
Dan Lydiate, Jamie Roberts and Sam Warburton scored tries, while wing Leigh Halfpenny kicked 15 points as Wales triumphed, yet it was a performance that lacked consistency and finesse - and almost produced a colossal upset.
Japan looked a far more threatening outfit with ball in hand as wings Akihito Yamada and Kenki Fukuoka. plus substitute Amanaki Lotoahea, claimed touchdowns, with fly-half Yu Tamura adding three penalties and two conversions and centre Timothy Lafaele converting Yamada's try.
Tamura kicked two penalties as Japan led 6-0, with his second successful shot at goal coming after Wales full-back Liam Williams was rightly sin-binned for obstruction on Yamada.
It amounted to a poor start by Wales, but they responded in the 11th minute after possession was rapidly moved wide and Lydiate touched down for his first try in Wales colours, with Halfpenny adding the conversion.
Centre Jonathan Davies' half-break proved sufficient for midfield partner Roberts - back on Wales duty after being dropped for the Argentina game - to effortlessly finish off a second.
But Japan hit back strongly before the break, even if Wales gifted them a try, Yamada gathering after Gareth Anscombe's wayward pass to claim his 14th try in 17 Tests, with centre Lafaele's conversion making it 14-13 at the break.
Halfpenny extended Wales' lead through an early second-half penalty, and Wales moved further ahead after 52 minutes when Wales skipper Warburton finishing in style after good work from Alun Wyn-Jones.
But Japan hit back immediately through a brilliant touchdown that owed everything to slick passing and rapid handling, with Fukuoka applying a stunning finish in limited space.
Tamura kicked the conversion to trim Wales' advantage to 24-20. Another Halfpenny penalty took Wales seven clear, yet Japan immediately reclaimed field position and they gained a penalty that Tamura landed.
The game was now nip and tuck, as another Halfpenny penalty made it 30-23, but Japan were not to be denied as Lotoahea touched down six minutes from time and Tamura brilliantly converted to make it all square.
Wales pushed as the clock ticked down, and wing Alex Cuthbert knocked on with the line at his mercy, before Davies, who was winning only his second cap, came up trumps amid a frantic finale.