In Ancient Castillian, words like caja, bajo, and jaraba were originally spelled with an x, and pronounced as "sh".

In the mid- to late-1700s the spellings were changed from an x to a j, including words like Mejico and Tejas.  During that time, the "j" was actually pronounced as a "j" in English.  Over time, some words (like Mejico, Tejas, Oajaca, and Javier) reverted back to the 'x' spelling, but retained their 'j' pronunciation.  And the letter "j" took on the "h" pronunciation that we know today.

[Here is an excellent article on the subject](http://www.babab.com/no05/expediente_x.htm)