Japan fell into red ink in goods trade for the first time in two months in July as the cost of energy imports surged, government data showed Thursday.

The trade deficit came to 231.2 billion yen ($2.1 billion) following a revised 720.8 billion yen surplus in June, according to a preliminary report by the Finance Ministry.

Exports grew 3.9 percent from a year earlier to 6.75 trillion yen amid an increase in demand for electronic components from the rest of Asia, but were overshadowed by a 14.6 percent rise in imports to 6.98 trillion yen.

Crude oil imports jumped in value by 40.3 percent to 763.5 billion yen despite falling in volume amid climbing prices on the global market.

The deficit was also widened by an increase in pharmaceuticals coming from Ireland and aircraft manufactured in Germany, bringing imports from the European Union to a record-high 889.6 billion yen.

On balance, Japan logged a 122.8 billion yen deficit against the region despite a rise in exports of construction and excavation machinery.

Against the United States, Japan marked a surplus of 502.7 billion yen, down 22.1 percent from a year prior due to a fall in automobile exports.

Exports of steel, on which President Donald Trump placed a 25 percent tariff in March, were also down in both value and volume, though a ministry official who briefed reporters said the figures do not deviate much from data from the past several years.

Koya Miyamae, an economist at SMBC Nikko Securities Inc., said weak exports could hamper the world's third largest economy in the third quarter of 2018.

"Judging from the data currently available, overseas demand could drag down growth in July-September," he said, adding that if the recent earthquake and torrential rains in western Japan were to blame for a temporary slowdown, exports will likely pick up again in August.

Against its biggest trade partner, China, Japan saw its deficit narrow 17.8 percent to 212.0 billion yen on robust demand for semiconductors and equipment for manufacturing liquid crystal displays used in smartphones and other devices.

For the whole of Asia, Japan had a surplus of 533.4 billion yen, up 15.2 percent.

The figures were compiled on a customs-cleared basis.