JERUSALEM — Hundreds of thousands of ultra-Orthodox Jews rallied in Jerusalem on Sunday to protest emerging legislation that could end their sweeping exemption from military service.

Access to Jerusalem was blocked. Government offices, schools and courthouses closed early, and public transportation was halted to accommodate the mass prayer called by rabbinical leaders.

Under heavy police protection, the crowd of demonstrators swayed in prayer or danced to express their opposition to a military draft that many decried as a “war against religion.”

For decades, Israel’s ultra-Orthodox have been effectively exempt from military service. Those dedicating their life to Jewish scholarship in religious schools called yeshivas could defer military service indefinitely.

But the law allowing this arrangement expired in 2012 after the Supreme Court ruled it unconstitutional, and efforts to replace it with new legislation foundered until recently.

Under the new bill, the number of yeshiva students enrolling in military or other service would gradually increase in coming years, restricting exemptions to outstanding scholars only.

Proponents maintain that would reduce inequality and offer a gateway for more ultra-Orthodox to join Israel’s mainstream workforce. But many among the ultra-Orthodox reject the government’s plan as a political war against their way of life.