BOSTON — A man who climbed through razor wire to escape from a detention center in Rhode Island, stole a car and may have tried to rob two banks was captured in Massachusetts on Thursday by a state police trooper who chased him and grabbed him as he tried to leap over a fence, authorities said.

Massachusetts state police and the U.S. marshal in Rhode Island said James Morales was captured in Somerville, about an hour's drive north of the privately run Wyatt Detention Facility in Central Falls, where he escaped five days earlier.

Earlier Thursday, authorities said they were investigating whether Morales was the man who tried to rob a bank in Cambridge, Morales' hometown.

State police spokesman David Procopio said troopers were in the area after a second attempted bank robbery in neighboring Somerville when a trooper spotted Morales walking. They said Morales ran, the trooper chased him for about two blocks and he was captured as he tried to jump a fence.

Morales was set to be booked at a state police barracks in Medford on Thursday evening, authorities said. He couldn't be reached for comment while in custody.

Morales is a former Army reservist who was being held at Wyatt, which has about 800 inmates, on charges he stole 16 guns from a U.S. Army Reserve Center in Worcester. He also was facing child rape charges.

Morales, 35, escaped on Saturday by climbing a basketball hoop to reach a rooftop, cutting through a fence and climbing through razor wire, authorities said. Police think Morales stole a car in Attleboro, Massachusetts; the car was found Sunday in Framingham, Massachusetts, less than an hour north of the prison.

Procopio said authorities were concerned because not all of the handguns Morales is charged with stealing have been recovered.

"We could not rule out that he had access to weapons, and as time went on he was probably growing increasingly desperate as well, and that would add to the danger to the public," he said. "We are thrilled and elated that he is off the street and will be going back behind bars where he belongs."

This surveillance photo released by the Federal Bureau of Investigation shows a man who attempted to rob a Bank of America branch Thursday, Jan. 5, 2017, in Cambridge, Mass. The FBI said the suspect resembles recently escaped federal prisoner James Morales, who escaped from the Wyatt Detention Facility in Central Falls, R.I., on Saturday, Dec. 31, 2016. (Federal Bureau of Investigation via AP)
This surveillance photo released by the Federal Bureau of Investigation shows a man who attempted to rob a Bank of America branch Thursday, Jan. 5, 2017, in Cambridge, Mass. The FBI said the suspect resembles recently escaped federal prisoner James Morales, who escaped from the Wyatt Detention Facility in Central Falls, R.I., on Saturday, Dec. 31, 2016. (Federal Bureau of Investigation via AP)

This surveillance photo released by the Federal Bureau of Investigation shows a man who attempted to rob a Bank of America branch Thursday, Jan. 5, 2017, in Cambridge, Mass. The FBI said the suspect resembles recently escaped federal prisoner James Morales, who escaped from the Wyatt Detention Facility in Central Falls, R.I., on Saturday, Dec. 31, 2016.

Photo Credit: Federal Bureau of Investigation via AP

Procopio said authorities have confirmed Morales was the person who tried to rob the Somerville bank but have not identified him as the person in the earlier attempted robbery in Cambridge.

Cambridge sent a text message alert to residents on Thursday telling them law enforcement was seeking a bank robbery suspect who resembled Morales.

In both attempted robberies, the would-be robber handed a teller a note demanding money. There were no reports of a weapon being shown.

Somerville Mayor Joseph Curtatone said officials were told Thursday morning that Morales could be in the area.

"We're just very grateful that nobody was hurt, our officers and safe and our neighborhood is safe," he said.

Associated Press writer Michelle Smith in Providence, Rhode Island, contributed to this report.