TOKYO — Japan on Thursday reported a record annual trade deficit in 2012, the second straight year in the red for an exporting nation that has long built its wealth on its vast trading surpluses.

The annual trade gap of 6.93 trillion yen (about $78 billion) was brought about by surging fuel imports and a continued slide in machinery shipments and other mainstay exports. The deficit underscores the challenges Prime Minister Shinzo Abe faces as he tries to lift the world’s third-largest economy after the United States and China out of years of stagnation.

The deficit also brings to the forefront the risks that accompany Mr. Abe’s bid to revive the economy through government spending, which will add to Japan’s public debt, already more than twice the size of its economy. For years, export surpluses helped Japan finance that enormous debt without having to turn to foreign investors.

But that delicate balance is now unraveling. The global economic crisis set off a fall in Japanese exports, and also caused the yen to strengthen, weighing on the country’s competitiveness and recovery. The prolonged shuttering of the country’s nuclear reactors in the wake of the Fukushima crisis has led to a spike in Japan’s imports of oil and gas. A bitter territorial spat with China has hurt exports to Japan’s biggest trading partner.