When was jail invented?

We don't know. Very possibly it is as old as civilization itself when people started to settle in cities, which means some 5-6,000 years ago. Prison by the way was in ancient times and until well into the Middle Ages NOT a place where you were condemned to remain for a specific time as a punishment, but only a place where you were kept until a judge could decide what had to be done with you. In many cases - even in cases of murder - that was paying a big fine to the victim or his family, banishment from the city or the country, or punishments like mutilation or death.