TOKYO — The first time Shinzo Abe, Japan’s prime minister, met President Xi Jinping of China, the body language told the story. Mr. Xi looked dour and stiff, turning away from Mr. Abe with pursed lips, as if someone had forced him into it after months of escalating tensions between their two nations. In the years since, relations have improved but the two leaders have always kept at a distance.

This past weekend, they met again on the sidelines of a regional summit meeting in Vietnam. This time, they shook hands and Mr. Xi flashed a grin.

It was a sign, the Japanese news media suggested, of a potentially momentous shift in the region, with two longtime adversaries drawing closer as the United States under President Trump has created unease among allies about the role the United States will take in the region.

To compensate, Japan and China are inching toward a possible reconciliation as they recognize the altered dynamics around the Pacific Rim.

But with the two Asian powers long divided by disputes over history and territory, as well as testiness over influence in the region, it will take more than a few handshakes — or a smile — to cement a genuine realignment.

In gesturing toward a new friendliness, Japan is motivated in part by the recognition that as the United States retreats, it needs stronger trade with China. Having watched Mr. Trump heap praise on Mr. Xi in Beijing last week, Japan is also propelled by fear that the United States may develop a closer rapport with China that would exclude Japan.

And as China seeks to strengthen its power, it realizes it may have more success exerting authority in the region with Japan as less of a rival. At the same time, Mr. Trump’s visit showed China that the United States is unlikely to get in its way, allowing a more confident Mr. Xi to be more generous toward Japan.

“What Trump represented was a real shock to the system of allies and the world,” said Nick Bisley, professor of international relations at La Trobe University in Melbourne, Australia.

The threat from North Korea is also naturally drawing China and Japan closer, although Mr. Abe has so far hewed closely to Mr. Trump’s approach of calling for more pressure and sanctions.

Mr. Abe appears keenly aware of Mr. Trump’s erratic swings in opinions and loyalties. Japan is naturally wondering if the United States may make some kind of deal with China that could put Japan at a disadvantage, experts said.

“Standing behind that is kind of what you always find in Japan — that underlying fear of abandonment,” said Daniel C. Sneider, a lecturer in East Asian studies at Stanford University.

Image

Leaders at the Asia-Pacific Economic Cooperation meeting in Danang. Japan’s move toward China reflects a recognition that China is supplanting the United States as the leader of free trade in the region.CreditPool photo by Jorge Silva

“All of that is within the broader context of huge Japanese mistrust and long-term apprehension about the growth of Chinese power,” Mr. Sneider added. “They understand that the Chinese see themselves as the natural hegemon, and in the Chinese worldview, the Japanese should be subordinate to them.”

As Mr. Abe and Mr. Xi take the first steps toward a better relationship, many obstacles remain.

Neither country has given any ground in a territorial dispute over a set of islands in the East China Sea, known in Japan as the Senkaku and in China as the Diaoyu. China also still regularly objects to what it perceives as any sign that Japan is returning to its militaristic past, and carefully watches the debate in Japan about how much to develop the military’s capabilities.

In both countries, the public views the other with suspicion, fanned in some cases by negative media coverage. “I don’t think I see how they can make drastic improvements, given the public opinion base,” said Ezra Vogel, a professor emeritus of social sciences at Harvard who is working on a book about Sino-Japan relations. “Less than 10 percent on either side have favorable things to say about each other.”

In China, after five years of relegating Japan to the diplomatic freezer, Mr. Xi decided a richer and stronger China had more to gain from smoother relations with Mr. Abe than continued hostility, Chinese analysts said.

“We attach importance on our relationship with Japan,” said Ren Xiao, professor of international relations at Fudan University in Shanghai. “And now both sides want good relations.”

And as Japan tries to carve out its own sphere of influence, particularly in Southeast Asia, Mr. Xi is following the old precept of keeping his enemies close.

While China applies a mixture of coercion, diplomacy and money to pull nations to its side, Japan is offering maritime and development assistance to countries like Vietnam and the Philippines.

Mr. Xi “needs to make sure Japan doesn’t emerge as a plausible third power in the region,” said Rana Mitter, a professor of history at the University of Oxford, and a specialist in China-Japan relations.

When Mr. Trump was in Beijing, Mr. Xi made clear his ambitions for China to replace the United States as the dominant power in the Western Pacific.

“The Pacific Ocean is big enough to accommodate both China and the United States,” Mr. Xi said with Mr. Trump beside him, an assertion that some in the region and Washington see as shorthand for China’s taking advantage of an American retreat.

Image

A group of disputed islands in the East China Sea, known as the Senkaku in Japan and the Diaoyu in China, shown in 2012.CreditKyodo/Reuters

Mr. Trump helped reinforce that perception by failing to press China on its military buildup in the South China Sea, according to analysts.

By warming up to Mr. Abe, Mr. Xi is getting the Japanese leader used to the idea of China as the primary power in East Asia, said Hugh White, an Australian strategic expert and author of a coming magazine essay, “Without America: Australia in the New Asia.”

“China making nice to Tokyo?,” Mr. White said. “The objective is to show the Americans can’t be relied on.”

Experts said Mr. Abe may also be under pressure from Japanese businesses, which want to work with China to develop projects around the region.

“Japanese companies want to open their own opportunities to join with Chinese projects in Eurasia,” said Shin Kawashima, professor of political and diplomatic history at the University of Tokyo. “So Japanese companies have required Abe to improve relations with China.”

Mr. Abe has been pressing for a change in relations between Beijing and Tokyo since he started his second term as prime minister in 2012. Earlier this year, the Japanese sent a senior politician to a Beijing conference hosted by Mr. Xi for the “One Belt, One Road” initiative, Mr. Xi’s signature infrastructure program designed to magnify China’s influence in Asia and Europe.

For its part, China refrained this summer from sending a fleet of fishing vessels to the waters around the disputed islands in the East China Sea — contrary to what it had in past years.

During the talks in Danang, Mr. Abe proposed that he and Mr. Xi visit each other’s country next year.

When Mr. Xi smiled during the photo opportunity, the Japanese news media trumpeted it as a breakthrough. “That smile was very different from the ‘salty response’ that Mr. Xi had shown in the past four meetings,” a commentator on Fuji Television noted while comparing pictures from earlier encounters between the two leaders.

Mr. Abe told reporters that Mr. Xi described the talks as ushering in a “new start of Japan-China relations.” Two days later, Mr. Abe also met with the Chinese premier, Li Keqiang, at a separate meeting in Manila, an unusual diplomatic twofer.

But history has shown that such meetings may not produce longstanding results. In 2008, when Hu Jintao, China’s president at the time, met in Japan with Yasuo Fukuda, then prime minister, they released a statement promising to “cooperate to enhance peace and friendship over the long term.” That commitment lasted just two years, until the dispute over the islands heated up.

“Unfortunately, although a reset of Sino-Japanese relations would certainly be welcome, the forecast isn’t very good,” said Kristi Govella, a postdoctoral fellow in Harvard’s Program on U.S.-Japan Relations. “It would take a great deal of political will for Abe and Xi to make headway.”