The nation posted a goods trade surplus of ¥2.46 trillion ($23 billion) in fiscal 2017 as strong overseas demand lifted the country's exports to the second-highest level on record, the Finance Ministry said Wednesday.

The surplus, however, shrank 38.2 percent from a year earlier due to a surge in energy imports such as crude oil, liquefied natural gas and coal. Japan stayed in the black for the second straight year.

Exports gained 10.8 percent to ¥79.22 trillion, up for the first time in three years, as shipments of cars and semiconductor-related equipment surged. Imports rose 13.6 percent to ¥76.77 trillion for the first gain in four years.