Japan posted a 43 billion yen ($300 million) trade surplus in June, returning to the black for the first time in nearly two years amid robust U.S.-bound exports and falling energy import costs, government data showed Thursday.

For the first six months of 2023, resource-poor Japan ran a trade deficit of 6.96 trillion yen, though it was almost 13 percent lower than a year ago.

In June, robust U.S. demand for cars and construction machinery boosted Japan's overall exports by 1.5 percent to 8.74 trillion yen, the largest ever for the month. Imports dropped 12.9 percent to 8.70 trillion yen as those of crude oil, coal and liquefied natural gas all fell.

Exports tend to rise in June after overseas shipments slow in May when Japan has its Golden Week holiday period, according to analysts.

"It'll be difficult for export growth to accelerate when demand from China as well as the United States and Europe is all expected to stagnate," said Kota Suzuki, an economist at Daiwa Securities.

"China's recovery, in particular, has been slower than previously expected and it's unlikely that increased China-bound shipments will drive growth in Japanese exports," he added

Japan's trade surplus with the United States expanded 37.4 percent to 817.50 billion yen as exports rose 11.7 percent to 1.74 trillion yen, a record for June, the Finance Ministry said in a preliminary report.

Imports slipped 4.2 percent to 921.29 billion yen as those of crude oil and wheat fell.

The country had a trade deficit of 442.83 billion yen with China, down 7.6 percent, as exports fell more than imports due to slackening demand for steel and chips in the world's second-largest economy.

Japan's trade balance with the rest of Asia came to its first surplus in two months of 280.80 billion yen while a trade deficit of 26.24 billion yen was reported with the European Union.

Japan has been at the mercy of surging energy import costs that have been magnified by the yen's weakness. The dollar has been strong against the yen, reflecting the widening interest rate gap between the United States and Japan.

Crude oil prices dropped 25.0 percent in June from a year ago in yen terms.

"The value of imports will likely be on a downtrend in line with falling commodity prices while export growth slows. In such an environment, Japan will likely report trade deficits going forward," Suzuki added.

Ongoing monetary tightening by the United States and other major economies has fueled worries about a global economic slowdown, which would be a blow to Japanese exporters that form the backbone of the world's third-largest economy.

For the first half of 2023, exports gained 3.1 percent to 47.35 trillion yen, buoyed by cars, construction machinery and ships. Imports rose 0.7 percent to 54.31 trillion yen, with coal a major contributor.

Japan's first-half trade surplus increased 20.7 percent to 3.49 trillion yen with the United States while it saw a roughly 1.5-fold increase in its trade deficit to 3.65 trillion yen with China, according to the ministry data.