A man charged with running from cops with a gun was released from custody Saturday after a judge warned him about the danger he allegedly got himself into.

Eric Johnson, 20, was charged with a felony count of unlawful use of a weapon after officers on patrol Friday allegedly saw him suspiciously hold a bulge on his side, Cook County prosecutors said during a bail hearing Saturday.

Johnson was walking with a group in the 3400 block of West Ohio Street when he turned away from the officers to conceal the bulge, prosecutors said. Officers pursed Johnson on foot and saw him pull out a handgun.

Eric Johnson, 20 | Chicago police

He eventually dropped the weapon and a magazine and was arrested shortly after, prosecutors said. He did not have a license to carry the gun.

Judge John Fitzgerald Lyke Jr. released Johnson on his own recognizance, or an I-bond, during a bail hearing at the Leighton Criminal Court Building — but not before giving a short lecture.

“Considering that he was running from police with a gun, a lot can go wrong there,” Judge Lyke said.

Johnson, who lives in East Garfield Park, currently attends automotive school, according to his attorney.