BOSTON — Judges in Massachusetts can order people with addiction to remain drug free as a condition of probation and jail defendants for failing to do so, the state’s highest court ruled Monday in a case that garnered national attention amid the deadly opioid epidemic.

The Supreme Judicial Court ruled unanimously that the drug-free probation requirement does not violate the constitutional rights of people with substance use disorder or unfairly penalize them because of a medical condition beyond their control.

The court ruled in the case of Julie Eldred, who was jailed in 2016 after she tested positive for the powerful opioid fentanyl days into her probation on larceny charges. Eldred, who has severe substance use disorder, spent more than a week in jail after relapsing until her lawyer could find a bed for her at a treatment facility.

Ordering defendants to not use drugs as a condition of probation is a common practice nationwide, and observers said a ruling in Eldred’s favor could spark similar challenges elsewhere.

Most addiction specialists — including groups such as the National Institute on Drug Abuse and American Society of Addiction Medicine — view substance use disorder as a brain disease that interferes with a person’s ability to control his or her desire to use drugs.

Eldred’s lawyer argued before the high court in October that her client’s substance use disorder made her powerless to control her desire to use drugs and that jailing her effectively criminalized relapse — which often happens in the recovery process.

But the justices said the defendant’s claims were based partly on untested science. The justices argued that revoking probation doesn’t punish drug use, but the underlying crime, and noted that the judge had first sought to have Eldred placed in a treatment facility before she was jailed.

“The judge was faced with either releasing the defendant and risking that she would suffer an overdose and die, or holding her in custody until a placement at an inpatient treatment facility became available,” wrote Justice David Lowy.

Eldred was charged with larceny for stealing jewelry and sentenced to one year of probation, which allows people to avoid jail or prison if they meet certain conditions. She was receiving outpatient treatment when she relapsed. She violated no other conditions of her probation when she was sent to jail, where she received no treatment.

“Julie Eldred did not ‘choose’ to relapse any more than a person who has hypertension chooses to have high blood pressure, a person who is homeless chooses to sleep in an alley, or a person who is destitute chooses not to pay court-ordered fees or restitution,” Eldred’s attorney’s wrote in court documents.

One of her lawyers didn’t immediately respond to requests for comment Monday.

Massachusetts Attorney General Maura Healey’s office had urged the court to uphold the practice, arguing it can help people who are battling addiction and lower incarceration rates.