The New York Police Department's surveillance of Muslims in New Jersey was a lawful path to prevent terrorism, a federal judge ruled Thursday.

Eight Muslim plaintiffs charged the programs were unconstitutional because they focused on religion, national origin, and race.

The lawsuit also accused the police of spying on people at mosques, restaurants, and schools in the Garden State since 2002.

But U.S. District Judge William Martini dismissed the case, saying it was unlikely the plaintiffs were targeted solely because of their religion.

"The more likely explanation for the surveillance was to locate budding terrorist conspiracies," he wrote. "The police could not have monitored New Jersey for Muslim terrorist activities without monitoring the Muslim community itself."