NORRISTOWN, Pa. — A judge ruled on Tuesday that Bill Cosby’s trial on sexual assault charges could go forward, setting the stage for a potential legal battle over the prosecution’s evidence.

The ruling, by Judge Elizabeth A. McHugh, ended five months of efforts by Cosby’s lawyers to have the charges dismissed.

The decision, after a 3 1/2-hour hearing at the Montgomery County Courthouse here, means that a man who was once one of America’s most beloved entertainers but has been pursued by allegations of sexual misconduct by dozens of women must face at least one of his accusers at trial, probably later this year. Cosby, 78, has denied the allegations.

“This case will move forward,” McHugh told the crowded courtroom. Cosby, who sat flanked by his lawyers throughout the hearing, stood at the end and said, “Thank you.” The judge wished him luck.

The entertainer was charged in December with drugging and sexually assaulting Andrea Constand, a former Temple University staff member with whom Cosby became a mentor and friend. Prosecutors said he took advantage of his counseling role, gave her pills and assaulted her at his home outside Philadelphia in early 2004.

Constand was not in the courtroom on Tuesday. During the hearing, Cosby’s lawyers attacked the prosecution for not calling her as a witness, relying instead on a statement she gave to the police in 2005. A Montgomery County detective read portions of the statement outlining the allegations.

It was a case that many people, including accusers

who had come forward publicly, long thought prosecutors would never bring.

Since the charges were filed, Cosby’s lawyers have battled to have them thrown out. They argue that a former district attorney promised never to prosecute Cosby as a way to induce him to testify in a civil suit brought by Constand in 2005. That suit, in which Cosby spoke freely in deposition testimony, was settled confidentially in 2006.

But at a hearing in February, a trial court judge dismissed the defense’s challenges, opening the way for Tuesday’s hearing, which is standard in the state system.

The ruling is likely to be followed by another battle by Cosby’s legal team to challenge much of the evidence that could be used at trial.

Legal experts said that his lawyers would probably seek to bar Cosby’s deposition from the 2005 civil case because confidentiality was part of the settlement.

Cosby is also expected to fight any attempt to introduce evidence from other women who have come forward to accuse him of drugging and sexually assaulting them.

Significant obstacles to conviction remain, legal experts said, including a lack of forensic evidence. Cosby’s lawyers are also expected to ask why Constand, who now lives in Canada, waited a year before making her accusations to the Pennsylvania authorities.

In addition to the criminal case, Cosby is a defendant in a number of civil defamation suits. They were brought by other women who say he branded them as liars when they came forward during the last two years to say he had assaulted them in the past.