In the Uniform Code of Military Justice, adultery is a violation only when it harms or brings discredit on the armed forces. Even then, numerous experts on military law said this week, it is rarely prosecuted as an isolated act without aggravating circumstances — such as involving a subordinate or a security breach — and for that reason, a case is very unlikely to be brought against retired Gen. David H. Petraeus.

Although Petraeus retired from the Army last year, he remains under the jurisdiction of the military code so long as he collects a paycheck from the military such as a pension. But to face a court-martial, a retired soldier would have to be recalled to active duty and prosecuted.

“I’ve never seen adultery charged as a stand-alone offense,” said Gary Solis, a former Marine prosecutor, judge advocate and now an adjunct professor of law at Georgetown University. “And the chance he would be brought back to active duty for court-martial is zero. Not because he’s General Petraeus, but very seldom are officers brought back unless the conduct has a link to the military.”

Eugene R. Fidell, who teaches military law at Yale University, said, “It’s very unlikely he would be disciplined for either current or past conduct.”

The situation for Marine Gen. John R. Allen, the current commander of U.S. and NATO troops in Afghanistan, is different because he remains on active duty. Unlike in the case of Petraeus, who acknowledged adultery, there have been no suggestions that Allen’s relationship with a woman in Tampa involved anything more than an exchange of e-mails.

His links to the woman, socialite Jill Kelley, came to light as part of the FBI investigation of Petraeus and his mistress, Paula Broadwell. Pentagon officials said Allen met Kelley while serving as deputy commander of the U.S. Central Command in Tampa and exchanged hundreds of ­e-mails with her.

One case that illustrates the type of aggravating circumstances that would land a general in a military court is the prosecution of Brig. Gen. Jeffrey A. Sinclair. He is facing a military grand jury at Fort Bragg, N.C., on charges of forcible sodomy, adultery, and having inappropriate relationships with four female subordinates and one civilian. Prosecutors said that when subordinates objected to Sinclair’s treatment of women, he responded, “I’m a general; I’ll do whatever the [expletive] I want.”

Sinclair admits the adultery accusations, but not the sexual assault charges, according to media coverage of a preliminary hearing in the court-martial process last week. His attorney said that adultery and fraternization would be punishable by a written reprimand, but the sexual assault charges could carry a sentence of up to life in prison.

Rather than a court-martial, several experts suggested that reducing a general’s rank, even in retirement, is a possible disciplinary step. They pointed to the case of Air Force Maj. Gen. Thomas J. Fiscus, who was convicted in 2004 of conduct unbecoming an officer and other offenses after engaging in improper relationships with more than a dozen women. He was demoted to colonel, which was a significant reduction in pay and pension.

And on Tuesday, Defense Secretary Leon E. Panetta stripped a star from four-star Army Gen. William “Kip” Ward, the former commander of U.S. forces in Africa, for spending thousands of dollars on lavish travel and other unauthorized expenses. The move will cost Ward about $30,000 a year in retirement pay.

Fidell said another alternative is to revoke someone’s security clearance, a devastating blow for military or private employment. Fidell said he has seen examples of people “repeatedly losing their clearance for adultery” unconnected to any other offense.

Petraeus had an extramarital affair with Broadwell, an Army reservist who co-wrote a book about the general and spent extensive time with him during its preparation. According to associates of Petraeus, he has said that the affair occurred after he retired from the Army in August 2011. FBI agents searched Broadwell’s home in Charlotte and reportedly found classified material. A security violation such as that could pose more serious trouble for Petraeus.

“That could be a real problem,” Solis said. “If classified material in her house is traceable to Petraeus’s CIA status, then we have a new ballgame.”

The classified-document issue could involve the civilian, rather than military, justice system, said Jonathan Turley, a law professor at George Washington University who has tried many cases under military law. “As a general rule, once you retire, they tend to let these matters be handled by other authorities,” Turley said.

Turley said classified-material breaches are often “brushed under the rug. Most of the time, the person is reprimanded. This city is awash in documents that bear classified markings.”