NEW BRUNSWICK, N.J. — A judge offered a spirited defense on Wednesday of the 30-day jail sentence he imposed on Dharun Ravi, the former Rutgers University student convicted of using a webcam to spy on his roommate having sex with another man.

No matter how “unconscionable” Mr. Ravi’s conduct, Judge Glenn Berman said in a court hearing, “I can’t find it in me to remand him to state prison that houses people convicted of offenses such as murder, armed robbery and rape.”

“I don’t believe that that fits this case,” he continued. “I believe that he has to be punished, and he will be.”

Mr. Ravi was convicted in March of all 15 charges against him. Last week, in addition to the jail term, Judge Berman sentenced him to 300 hours of community service, three years’ probation and $10,000 to be paid to a fund that helps victims of bias crimes.