PORTLAND, Maine — A police officer patrolling a high school on Monday spotted a man loading a rifle outside a nearby church and disarmed him, possibly stopping a violent encounter, authorities said.

Herbert Jones, 46, made references to pedophiles being inside First Parish Church after being taken into custody in an alley, said Police Capt. Vern Malloch. An Alcoholics Anonymous meeting was under way inside the church at the time.

Officer Steve Black, who was assigned to Portland High School, “probably avoided a tragedy” by noticing the armed man, Malloch said. The officer approached with his weapon drawn and arrested Jones without incident at 8 a.m. Malloch said that, to his knowledge, none of the AA participants was on the state’s sex offender registry.

Jones previously participated in the AA group and was stalking a woman who remained a participant, Malloch told reporters. He had been drinking but passed a field sobriety test, Malloch said.

In addition to the 7 mm rifle, officers found a shotgun, ammunition and four knives inside his BMW convertible, and another rifle at his home, Malloch said.

Jones was charged with terrorizing, stalking, possession of a firearm by a felon and possession of a firearm in a school zone. A passenger in Jones’ car was charged with having a concealed weapon after police found a knife under his seat, Malloch said.

Jail records list no attorneys for the men, who were expected to make initial court appearances on Wednesday, a jail official said. Jones was held on $100,000 bail, while the other man was held on $100 cash bail.