A federal judge ordered a halt to construction of President Trump’s border wall Friday, saying Trump exceeded his legal authority by mandating a project that Congress has refused to pay for.

U.S. District Judge Haywood Gilliam of Oakland, the first judge to consider legal challenges to Trump’s order, granted requests by environmental and immigrant advocates for an injunction against transferring funds from other federal programs to build the first stage of the wall, in Arizona and Texas.

“Congress’ absolute control over federal expenditures — even when that control may frustrate the desires of the executive branch regarding initiatives it views as important — is not a bug in our constitutional system,” Gilliam wrote. “It is a feature of that system, and an essential one.”

Principles of government “dating back to the earliest days of our Republic,” Gilliam said, demonstrate that “when Congress declines the executive’s request to appropriate funds, the executive (may not) ... simply find a way to spend those funds.”

Trump has proposed redirecting $8.1 billion from other projects, mostly military, to build a wall at the Mexican border. The judge’s ruling blocks the diversion of $1 billion in military funds to the Arizona and Texas phases.

Gilliam said he would consider the legality of additional spending once the administration issues its final plans for the funds. He denied an injunction sought by California and other states against those projects, saying the states had not yet shown that the funding would cause them “irreparable harm.”

Attorney Dror Ladin of the American Civil Liberties Union, which represents the Sierra Club and the immigrant-advocacy group Southern Border Communities Coalition, said the ruling is “a win for our system of checks and balances.”

Attempts to reach the White House and the Justice Department for comment late Friday were not successful. The administration is likely to appeal the ruling, and the dispute could reach the Supreme Court by the end of the year.

Trump, who had promised during his presidential campaign that Mexico would pay for a wall on the southern U.S. border, sought $5.7 billion in funding from Congress last year. When lawmakers approved only $1.375 billion for limited barrier construction, the president vetoed an appropriations bill and closed down many government operations for 35 days, starting Dec. 22 — the longest shutdown in U.S. history.

He then declared a national emergency over illegal immigration on Feb. 15 and said he would fund the wall with $8.1 billion already in the budget for other purposes, mostly Defense Department programs.

Lawyers for the administration cited the president’s broad emergency powers under federal law and said military funds had been used to build fences near the border for 30 years. They also argued that the lack of congressional approval of the $5.7 billion sought by Trump did not amount to a rejection of funding for the wall, because Congress did not expressly forbid the use of Defense Department funds for wall construction.

Gilliam disagreed.

“The reality is that Congress was presented with — and declined to grant — a $5.7 billion request for border barrier construction,” he said.

He also rejected Justice Department arguments that the funding was authorized by a law allowing redirection of the Pentagon budget to meet “unforeseen military requirements.”

Describing the wall as “unforeseen” contradicts “the administration’s multiple requests for funding for exactly that purpose dating back to at least early 2018,” Gilliam said. Citing another law that authorizes funding for military construction projects, he said it was “unclear how border barrier construction could reasonably constitute a military construction project.”

On another issue, Gilliam said environmental groups and others who visit the border areas had the right to sue because the planned construction would “lead to a substantial change in the environment,” which would “harm their ability to recreate in and otherwise enjoy public land along the border.”

Sierra Club attorney Gloria Smith praised the ruling.

“Walls divide neighborhoods, worsen dangerous flooding, destroy lands and wildlife, and waste resources that should instead be used on the infrastructure these communities truly need,” she said in a statement. “Yet again, the American people have had to look to our courts for a check on President Trump’s unlawful power grabs.”

Gov. Gavin Newsom also commended the ruling.

“America should be building bridges — not walls,” the California Democrat said. “California has said all along that the federal government should be focused on the real threats our state faces like wildfires and drug trafficking.”

A previous version of this story misstated U.S. District Judge Haywood Gilliam’s first name. The story has been altered to reflect this change.

Bob Egelko is a San Francisco Chronicle staff writer. Email: begelko@sfchronicle.com Twitter: @BobEgelko