Japan filed an official protest, complaining that Chinese authorities failed to stop the violence.
The Shanghai government, however, blamed Japan for the protest, saying it was sparked by "Japan's wrong attitudes and actions on a series of issues such as its history of agression," the official Xinhua news agency quoted government spokeswoman Jiao Yang as saying late Saturday.
Jiao called for calm and asked residents not to participate in unauthorized demonstrations.
Thousands of people also took part in peaceful protest marches in two other cities, defying government demands for restraint. In Beijing, hundreds of police blanketed Tiananmen Square in the center of the Chinese capital to block a planned protest.
Japan's Embassy said two Japanese were injured in Shanghai after being surrounded by a group of Chinese, Kyodo News agency reported. The extent of their injuries was not immediately clear.
Police in riot helmets kept protesters away from the consulate building in Shanghai but let protesters throw eggs and rocks. A group of young men broke the windows of a Nissan sedan and flipped it onto its roof while the windows of at least two other Japanese cars were shattered.
Protesters smashed windows at more than a dozen Japanese-style noodle shops and bars, many of them Chinese-owned. Others broke the windows of a police car, chanting "Kill the Japanese!", after a rumor spread that a man sitting inside was Japanese. The car drove away before the crowd could grab him.
The violence followed a march from City Hall to the consulate by about 5,000 people. They carried banners saying in English, "Say No to Japan in the Security Council" and chanted "Japanese pigs get out!"
A sign outside the consulate said, "Be Vicious Toward Japanese Devils."
Japan's Foreign Ministry denounced the protesters' "destructive and violent actions" and said it had "strongly protested" to the Chinese government.
"Even though information was available beforehand to infer that there would be a demonstration, nothing was done to prevent it," the ministry said in a statement.
Tensions between Tokyo and Beijing have been fueled by disagreement over the U.N. Security Council, gas resources in disputed seas and new Japanese textbooks that critics say gloss over Japan's wartime offenses. Many Chinese believe Japan has never truly shown remorse for atrocities committed during its brutal pre-World War II invasion of China.
Chinese activists have also pushed for a boycott of Japanese goods.
The destruction in Shanghai came a week after protesters smashed windows at the Japanese Embassy in Beijing last weekend and followed numerous protests in other Chinese cities.
In Japan, police were investigating an envelope of white powder sent to the Chinese Embassy in Tokyo. Mazda, Suzuki and Toshiba canceled nonessential business trips to China, while other companies told employees in the country to take safety precautions.
A protest march in Hangzhou, southwest of Shanghai, attracted 10,000 people who shouted "slogans condemning Japanese militarism," the official Xinhua News Agency reported. In Tianjin, east of Beijing, about 2,000 protesters held a peaceful one-hour march.
State television made no mention of the protests in its main evening news report, apparently reflecting fears by Communist leaders that they could further damage already sour relations with Tokyo or encourage others to take to the street to protest corruption or demand political reforms.
Japan's foreign minister was preparing to fly to Beijing on Sunday for talks aimed at defusing the tensions. Japan warned its citizens in China about possible danger before the protests. The United States issued a similar warning.
Some suggested Beijing permitted the protests last weekend to support a campaign to block Tokyo's Security Council bid.
Beijing is alarmed at a proposal to give a permanent Security Council seat to Japan, which it regards as a regional rival. Such status carries veto power over U.N. actions and is now held by only five governments — China, the United States, Britain, France and Russia.
Yet Beijing is also eager to preserve economic relations with Japan, which the Chinese Ministry of Commerce says has US$47.9 billion (euro36.8 billion) invested in China.
In Shanghai, police didn't try to stop the protest, though state newspapers said no one had received permission to hold one. At one point, police posted a sign saying, "March route this way."
The march in Shanghai was the first in China's commercial capital in the recent wave of anti-Japanese protests.
"The Chinese people are angry," said one marcher, Michael Teng, a graduate student at Donghua University. "We will play along with Japan and smile nicely at them, but they have to know they have a large, angry neighbor."