SAN FRANCISCO — A federal judge has tossed out a climate change lawsuit San Francisco and Oakland filed against five oil companies.

U.S. District Judge William H. Alsup on Monday granted Chevron’s motion to dismiss the suit, ruling the issue requires foreign and domestic policy decisions that are outside the scope of the courts.

The lawsuit sought to recover costs to pay for sea walls and other shoreline defenses to protect against rising sea levels that could inundate airport runways, roads, and coastal homes and businesses. The cities also accused the oil companies of hiding for decades that they knew fossil fuel use contributed to global warming and sea level rise.

“We of course are disappointed by the court’s ruling. We believe our lawsuit presents valid claims and these defendants must be held accountable for misleading the American people about the catastrophic risks to human beings and all forms of life on this planet caused by fossil fuel-driven global warming and sea level rise,” Oakland City Attorney Barbara Parker said.

Oakland is reviewing whether to file an appeal.

“In sum, this order accepts the science behind global warming. So do both sides. The dangers raised in the complaints are very real,” Alsup wrote in a 16-page order. “But those dangers are worldwide. Their causes are worldwide. The benefits of fossil fuels are worldwide. The problem deserves a solution on a more vast scale than can be supplied by a district judge or jury in a public nuisance case.”

Chevron has also requested courts dismiss similar lawsuits filed by other U.S. cities and counties, including in King County, Washington and New York. Chevron and experts representing San Francisco and Oakland testified before Judge Alsup during a “tutorial” hearing in March. Exxon, Shell, BP and Conoco Phillips were also defendants.

“Reliable, affordable energy is not a public nuisance but a public necessity,” said R. Hewitt Pate, Chevron’s vice president and general counsel. “Tackling the difficult international policy issues of climate change requires honest and constructive discussion. Using lawsuits to vilify the men and women who provide the energy we all need is neither honest nor constructive.”

Richard Wiles, executive director of Center for Climate Integrity, said he was disappointed by the ruling.

“The case before the judge was never about how to solve global climate change but simply whether oil and gas producers should help San Francisco and Oakland pay for the costs of adapting to it,” Wiles said. “By kicking the case to a do-nothing Congress and a climate-denying White House, the court essentially ruled that taxpayers alone should pay the massive costs of adapting to climate change.”