An off-duty state court officer shot and killed an armed man who attempted to rob him outside a Dunkin’ Donuts in Queens, police said.

The 50-year-old officer was wearing a uniform and headed to his weekend job as a security guard at about 8:45 p.m. Saturday when he was confronted by the would-be robber near the coffee shop in the Jamaica neighborhood, the Daily News reported.

As the officer was about to step into his parked car, the man pulled out a gun and demanded cash and the officer’s weapon.

The two men struggled and the officer shot the robber, a man in his 20s, in the chest.

The man, whose identity wasn’t disclosed by police, was taken to Jamaica Hospital where he died.

The Queens District Attorney’s Office is expected to investigate the incident.

The officer, who works at the State Supreme Court on Staten Island, was taken to a local hospital with chest pains.