Ngu Thazin wanted to leave her war-torn country for a better future. She set her sights on Japan.

In Myanmar, she studied Japanese and graduated with a chemistry degree from one of her country’s most prestigious universities. Yet she gladly took a job in Japan changing diapers and bathing residents at a nursing home in a midsize city.

“To be honest, I want to live in Japan because it is safe,” said Ms. Thazin, who hopes eventually to pass an exam that will allow her to work as a licensed caregiver. “And I want to send my family money.”

Japan desperately needs people like Ms. Thazin to fill jobs left open by a declining and aging population. The number of foreign workers has quadrupled since 2007, to more than two million, in a country of 125 million people. Many of these workers escaped low wages, political repression or armed conflict in their home countries.

But even as foreign employees become much more visible in Japan, working as convenience store cashiers, hotel clerks and restaurant servers, they are treated with ambivalence. Politicians remain reluctant to create pathways for foreign workers, especially those in low-skill jobs, to stay indefinitely. That may eventually cost Japan in its competition with neighbors like South Korea and Taiwan, or even places farther afield like Australia and Europe, that are also scrambling to find labor.

The political resistance to immigration in long-insular Japan, as well as a public that is sometimes wary of integrating newcomers, has led to a nebulous legal and support system that makes it difficult for foreigners to put down roots. Foreign-born workers are paid on average about 30 percent less than their Japanese counterparts, according to government data. Fearful of losing their right to stay in Japan, workers often have precarious relationships with their employers, and career advancement can be elusive.