Cosby's lawyers had argued for two days during a pretrial hearing that a criminal case had been barred by a promise made in 2005 by then-District Attorney Bruce Castor never to prosecute the renowned entertainer.
The judge on Wednesday ruled "there was no basis to grant the relief requested" by Cosby.
A spokesman for Cosby's legal team, Andrew Wyatt, told CNN his attorneys would appeal the judge's determination. "The decision reached by the court was wrong," he said.
Judge Steven O'Neill also ruled that prosecutor, newly elected District Attorney Kevin Steele, could stay on the case.
The case, in which former Temple University employee Andrea Constand accuses the TV star of assaulting her in his home in 2004, will go forward.
Cosby, who has not entered a plea, was charged December 30 with aggravated indecent assault against Constand, who went to authorities in 2005.
Bruce Castor, the Montgomery County district attorney at the time, did not file sexual assault charges against Cosby, citing "insufficient credible and admissible evidence."
Cosby was charged in the case in December, 11 years after the state initially declined to prosecute.
In addition to denying the allegations
, Cosby argues he's being improperly prosecuted based on testimony he gave during a civil suit -- testimony his defense says was given only because the state closed the criminal case in 2005.
But the district attorney's office reopened the investigation based on "new evidence" that emerged from the unsealing of Cosby's deposition in Constand's civil suit.
Before court ended Wednesday, the judge scheduled a preliminary hearing on March 8.
Lawyer John "Jack" P. Schmitt, part of a legal team that represented Cosby in 2005, testified Wednesday it was clear to him that a 2005 news release from Castor -- one announcing the prosecutor wouldn't file charges against Cosby in connection with a woman's allegations -- was a written and irrevocable agreement that the comedian would never be prosecuted.
"We certainly wouldn't have let him (Cosby) sit for a deposition" if he were still subject to criminal prosecution in the case, Schmitt said.
In addition, Schmitt said, Cosby's lead attorney in the Constand case, Walter Phillips, "got assurances from Mr. Castor that this was an irrevocable decision that he had made." Phillips has since died.
On cross-examination, Schmitt acknowledged Wednesday there was no other document besides the news release saying that no charges would be filed.
The prosecution said the defense hadn't proved its claim.
"They (the defense) are saying there is an explicit agreement, and they are saying this (press release) document is the explicit agreement and in that document itself says it will revisit the decision," a prosecutor argued Wednesday.
Later in the day, the prosecution called Constand's lawyer, Dolores Troiani, who said she was not aware in 2005 of any decision or agreement that the state would never prosecute Cosby.
"My understanding of the press release was that he was not going to prosecute at this time, but if there was additional information, he would change his mind," she testified.
She said she didn't hear until September 2015 of any assertion that Cosby could not ever be prosecuted.
On Tuesday, Castor testified he made the decision not to prosecute Cosby. He said he did so in part because he didn't believe the case was strong enough for a conviction.
Castor also said he thought the move might help Constand successfully sue the entertainer. He essentially conceded that the only reason Cosby testified in Constand's subsequent 2005 civil suit
-- the testimony that new prosecutors are now using to charge him -- was because he took any potential prosecution off the table.
When Castor declined to prosecute in 2005, he said, Cosby couldn't claim Fifth Amendment protection against self-incrimination in the civil suit.
But Castor told Cosby's attorneys Tuesday: "I'm not on your team. I want them (the prosecutors who've charged Cosby) to win."
Other legal matters for Cosby
Also Tuesday in a separate case, a woman dropped a civil suit
against the legendary comedian in a California court. Chloe Goins, now 24, had alleged Cosby sexually assaulted her at the Playboy Mansion in 2008.
She filed a court document allowing the suit to be dismissed.
Last month, a prosecutor in Los Angeles County declined to pursue a criminal case, saying evidence was insufficient to prove a crime was committed. Investigators learned Cosby was in New York the weekend of the event at which Goins accused Cosby of assaulting her.
Criminal case in Pennsylvania
According to an affidavit, Constand alleged Cosby drugged and sexually assaulted her in his Pennsylvania home in early 2004 after inviting her there to discuss her professional future.
Constand, a former director of operations for Temple University's women's basketball team, met Cosby in 2002 when, according to prosecution documents, she believed he sought her out to be a "mentor" and "sincere friend."
Constand's civil suit alleged battery, sexual assault and intentional infliction of emotional distress.
The suit was settled in 2006 with a confidential settlement agreement but not before a judge compelled Cosby to answer questions under oath.
That deposition was sealed for the last decade, but a federal judge unsealed parts of it last summer.
The district attorney's office then cited "new evidence" from the deposition to reopen the case and brought the charges against Cosby.
That new evidence, according to prosecutors, centers on a question from Constand's attorney Dolores Troiani and Cosby's response during the sworn testimony:
Q. "When you got the Quaaludes
, was it in your mind that you were going to use these Quaaludes for young women that you wanted to have sex with?"
Cosby later stated in the deposition he was only referring to one unnamed woman.
According to Cosby's 2005 deposition testimony, he admitted sexual contact with Constand but said it was consensual.
Castor: I had bound Pennsylvania to my decision
On Tuesday, Castor said he warned his successor in 2015 -- then-District Attorney Risa Vetri Ferman -- that the case had been "put to rest."
Castor testified that when he heard speculation last year that Ferman might charge Cosby, he reminded her by email that he'd already committed the state not to prosecute the comedian.
"I knew I had bound the commonwealth as a representative of the sovereign," Castor said.
He further explained to Cosby's attorneys Tuesday why he didn't pursue charges. He said:
• There were inconsistencies in Constand's statements to police. "Within days, she changed the day when it happened from March back to January '04."
• Those inconsistencies "would affect her credibility at trial."
• Constand contacted a civil lawyer before taking her allegations to police, which also hurt her credibility. Castor said that Cosby's lawyer -- Walter Phillips -- told him that Constand and her mother were trying to get money from Cosby in exchange for not going to police.
• "Making Mr. Cosby pay money to Ms. Constand (through a civil suit) was the best I was going to be able to set the stage for."
But Castor said he believed Constand.
"What I think and what is provable in a courtroom is two different things," Castor said. "... What I think is that Andrea Constand was inappropriately touched by Mr. Cosby."
Steele, the new district attorney, has argued a non-prosecution agreement never existed, saying in legal filings his office has no such documentation and can't find anyone who does.
Cosby has yet to have his preliminary hearing and formal arraignment in the case.