No, there is no legal basis for a judge to sentence anyone to join the military, But it used to sort of informally happen anyway, a lot actually, and at one time it was especially prevalent with folks steered towards the US Marine Corps.
There has always been a notion for many centuries in many countries that military service at the right time can save a young man who seems to be heading down a criminal path. And history shows it can indeed work, but like anything, not with everyone. I have met and known several senior folks who stayed in the military for wonderful careers who all had the same story. They told me they would be in prison right now if they hadn't joined back in the 1950's or 1960's, and were thankful for getting their life back on track.
The modern inspiration for this idea was the French Foreign Legion. Back in the 1800's, the French were looking for a way to not spill so much of their own French blood in their various colonial wars around the world. They came up with an idea of forming a unit that was only made up of criminals and foreigners who could join and fight for France, and they didn't care much about what their real backgrounds were or if they even joined using their real name. The U.S. version of this had many, many instances (in the thousands) throughout the 1900's where a pro-military judge and local police official who were friends with the local military recruiter would all agree to wink-wink steer a borderline criminal young man into the military to save them. This was done in what amounted to an informal hand-shake plea agreement that would be either made known to the young man through his attorney or by the police dropping a heavy hint. The thinking was everybody would win. The police and judge could skip a trial and free up a space in jail for truly hardened criminals, while getting a known troublemaker out of town for at least a few years. The recruiter would have another body towards his monthly quota, and the young man would get his act together with the assistance of a kindly drill instructor. A good drill instructor can't make you do anything you don't want to do. But he can sure make you wish you had. Towards this end, it was widely known that the Marines had the toughest boot camp training and best record of transformational turnarounds, and so judges were partial to sending these folks there instead of something easier like Navy or Coast Guard boot camp. More than a few of these cops and judges were former Marines themselves.
Today however, things have changed. Recruiters are professionalized and everything they do is heavily monitored and audited. They don't take folks anymore who have criminal records and who really don't want to be there, and they heavily screen for this. Between whiny medical conditions, drug busts and obesity, a modern recruiters task is grim. Only 29% of young Americans are qualified to join the military, which is a bit of an appalling statistic. That isn't to say that the local cop or judge isn't still steering or giving advice on the wink-wink down-low anymore, but it would have to be much more off the radar now. The emphasis nowadays would have to come more from the individual themselves to want to join and self-correct.