A federal judge on Thursday sentenced Roger Stone, President Trump’s longtime friend and political adviser, to serve three years and four months in prison for impeding a congressional investigation of Russian interference in the 2016 U.S. presidential election.

The penalty from U.S. District Judge Amy Berman Jackson comes after weeks of infighting over the politically charged case that threw the Justice Department into crisis, and it is likely not to be the final word. Even before the sentencing hearing began, Trump seemed to suggest on Twitter that he might pardon Stone. With the proceedings ongoing, Trump questioned whether his ally was being treated fairly. Afterward, he attacked the jury in the case and said he would “love to see Roger exonerated.”

In a lengthy speech before imposing the penalty, Jackson seemed to take aim at Trump, saying Stone “was not prosecuted for standing up for the president; he was prosecuted for covering up for the president.” She also appeared to call out Attorney General William P. Barr, saying his intervention to reduce career prosecutors’ sentencing recommendation was “unprecedented.” But she said the politics surrounding the case had not influenced her decision.

“The truth still exists; the truth still matters,” Jackson said, echoing prosecutors’ closing arguments at trial in November. “Roger Stone’s insistence that it doesn’t, his belligerence, his pride in his own lies are a threat to our most fundamental institutions, to the foundations of our democracy. If it goes unpunished, it will not be a victory for one party or another. Everyone loses.”

She added, “The dismay and disgust at the defendant’s belligerence should transcend party.”

Trump, meanwhile, weighed in from afar — again bucking Barr’s public and private warnings to stop talking about Justice Department criminal cases. As the hearing was ongoing, the president, who was traveling on the West Coast, suggested in a tweet that Stone was being treated unfairly compared with political rivals he wants to see charged with crimes, including former FBI director James B. Comey, former FBI deputy director Andrew McCabe and former secretary of state Hillary Clinton, who ran against him for president in 2016.

Not long after Stone’s hearing ended, Trump made similar comments in an appearance before former inmates in Las Vegas. Trump sought to make common cause over the criminal justice system. “These people know more about bad juries than everybody,” he said. Then he specifically attacked the panel and forewoman who decided Stone’s case.

“This is a woman who was an anti-Trump person, totally,” he said. “Is that a defrauding of the court? You tell me.”

Trump said he was “following this very closely, and I want to see it play out to its fullest because Roger has a very good chance of exoneration in my opinion.” But he added, “I’m not going to do anything in terms of the great powers bestowed upon a president of the United States. I want the process to play out.”

Overnight, Trump had hinted he could pardon Stone, tweeting a video clip in which Fox News host Tucker Carlson said, “President Trump could end this travesty in an instant with a pardon, and there are indications tonight that he will do that.”

Sen. Lindsey O. Graham (R-S.C.), one of Trump’s allies on Capitol Hill, reiterated Trump’s right to grant clemency to his friend.

“Under our system of justice, President Trump has all the legal authority in the world to review this case, in terms of commuting the sentence or pardoning Mr. Stone for the underlying offense,” Graham wrote.

Stone, 67, was convicted by a federal jury on seven counts of lying to Congress and tampering with a witness about his efforts to learn about hacked Democratic emails related to Clinton.

A jury found the longtime GOP operative guilty of lying during testimony to the House Intelligence Committee in September 2017 to conceal his central role in the Trump campaign’s efforts to learn about computer files hacked by Russia and made public by the anti-secrecy group WikiLeaks. Stone also threatened a witness who was an associate of his in an attempt to prevent the man from cooperating with lawmakers.

Stone did not speak in court and showed no visible emotion as the sentence was read. Emerging from the courtroom in a wide-striped suit and polka-dot tie, he appeared calm.

“I have nothing to say,” Stone said. “Thank you.”

Stone, his wife and a large entourage exited the courthouse to a large crowd of photographers, supporters and antagonists. As he climbed into an SUV, protesters shouted “Lock him up!” while supporters yelled “Pardon Roger Stone!”

Stone requested a new trial last week, after Trump suggested the forewoman in Stone’s case had “significant bias.” Jackson, the judge, said previously that she would delay implementing his sentence until she resolves that request. A filing is due from Stone’s defense team Monday. In addition to prison, Jackson ordered Stone to pay a $20,000 fine and serve two years of supervised release. He remains out of prison on bond, and even if he loses his motion for a new trial, he will have at least two weeks to turn himself in — unless an appeal further delays things.

The penalty capped an unusual sentencing hearing in which Jackson sought not only to resolve disputes between prosecutors and defense attorneys, but also to seek answers on the internal Justice Department haggling over what punishment the government would endorse.

The initial team of four career prosecutors recommended that Jackson impose a term of seven to nine years, only to see Trump tweet about the matter and Barr personally intervene. All four prosecutors then quit the case — with one leaving the government entirely — and their replacements filed a new recommendation suggesting that three to four years was “more typical” in cases like Stone’s.

Yet the new prosecutor in court Thursday defended his predecessors and argued for the same stiff sentencing enhancements as they had.

“The Department of Justice and the United States attorney’s office is committed to following the law without fear, favor or political influence,” Assistant U.S. Attorney John Crabb Jr. said. “This prosecution was and this prosecution is righteous.”

Crabb said the court “should impose a substantial period of incarceration,” although he did not propose a specific number of months or years.

At the Justice Department, one senior official expressed relief that the sentence was more in line with Barr’s preferred recommendation than the career prosecutors’ guidance of seven to nine years. “It was messier than we wanted, but we ended up in the same place,” the official said.

That official, like others in this story, spoke on the condition of anonymity to discuss internal deliberations.

Jackson called Stone’s testimony to Congress “plainly false” and “a flat-out lie,” and said his misdirection “shut out important avenues” for lawmakers to investigate. The judge said Stone knew his efforts to obtain damaging information about Clinton “could reflect badly on the president.”

Jackson acknowledged the case’s underlying political tension and pressed Crabb to explain why prosecutors filed two very different sentencing memos. She asked why the Justice Department ultimately chose to recommend bucking the guidelines — when department policies do not allow that without approval from supervisors — and questioned why Crabb was in court at all.

“I fear that you know less about this case than possibly anybody else in the courtroom,” Jackson said. “What is the government’s position today?”

Crabb said the case’s original prosecutors had approval from U.S. Attorney Timothy Shea to make their recommendation and that their filing was “done in good faith.” He said his understanding was there had been a “miscommunication” between Barr and Shea, centered on “what the expectations were from the attorney general and what the appropriate filing would be.” He apologized for the “confusion” — although even in court, his position seemed somewhat muddled.He refused to say whether he had written the softer sentencing recommendation, which bore his signature, because it would expose “internal deliberations.”

Stone had asked for probation, citing his age and lack of a criminal history.

Defense attorney Seth Ginsberg said Stone is “a real person, not a media figure, not a political character, but a real person,” who is soon to be a great-grandfather. He emphasized Stone has “devoted himself” to various causes — including veterans, animal welfare and football players suffering from traumatic brain injuries.

That, he said, is “who Mr. Stone really is — not the larger than life political persona that he plays on TV, but the real person who goes home every day to his wife and his family.

Ginsberg argued that New York City comedian and radio host Randy Credico, the witness Stone was convicted of threatening, understood Stone was “all bark and no bite.” Credico also appealed for leniency, saying in a letter to the court: “Stone, at his core, is an insecure person who craves and recklessly pursues attention. . . . Prison is no remedy.”

Jackson said that although seven to nine years was too harsh, probation would be too light. Stone, she said, had shown “flagrant disrespect for the institutions of government established by the Constitution, including the Congress and this court.”

She read aloud some of Stone’s profane texts threatening to kill Credico and steal his dog.

“The defendant referred to this as banter, which it hardly is,” the judge said. “Nothing about this case was a joke. It wasn’t funny, it wasn’t a stunt and it wasn’t a prank.”

Jackson noted that Stone threatened her personally during the trial and stirred up claims that the process was rigged. Doing so, she said, “willfully increased the risk that someone with even poorer judgment than” Stone would take action and put the entire courthouse in danger.

Barr’s intervention in Stone’s case set off a crisis for the Justice Department, where some worried that Trump — who called the penalty initially suggested by prosecutors “horrible and very unfair” — had pushed his chief law enforcement official to get involved in a criminal case because Stone was the president’s friend. Barr, though, insisted in an ABC News interview that he had made the decision independent of Trump and issued a remarkable public rebuke, saying Trump’s tweets “make it impossible for me to do my job.and to assure the courts … and the department that we’re doing our work with integrity

This week, those close to Barr said the attorney general has told Trump advisers that he has considered resigning over the president’s tweets. But Trump continued to tweet about the Stone case. This week, he suggested his friend deserved a new trial — just as the Justice Department, with Barr’s blessing, made clear it had opposed Stone’s request on that front. Like prosecutors, Barr has called Stone’s prosecution “righteous” and added, “I was happy that he was convicted.”

Senior Justice Department officials are increasingly resigned to the idea that Trump is likely to continue to publicly upbraid the department and the FBI and that it may be a theme of his reelection campaign — an incumbent president bashing his own Justice Department, an official said. But the official was also cautiously optimistic that the president seems to have avoided dragging Barr directly into those discussions in recent days.

A Justice Department spokeswoman declined to comment.

Stone is one of six Trump advisers and confidants who have either been convicted or pleaded guilty in connection with former special counsel Robert S. Mueller III’s investigation. That list includes former Trump campaign chairman Paul Manafort, former deputy campaign chairman Rick Gates, former national security adviser Michael Flynn, former Trump lawyer Michael Cohen and former campaign adviser George Papadopoulos.

In Las Vegas, Trump sought to distance himself from his longtime friend. “I know Roger, but a lot of people know Roger,” he said. “Everybody sort of knows Roger.”

Spencer S. Hsu and John Wagner contributed to this report.