structural:scholar_edition
Military career in Europe

San Martín took part in several Spanish campaigns in North Africa, fighting in Melilla and in Oran against the Moors in 1791, among others. His rank was raised to Sub-Lieutenant in 1793, at the age of 15. He began a naval career during the War of the Second Coalition, when Spain was allied with France against Great Britain, during the French Revolution. His ship Santa Dorotea was captured by the Royal Navy, who kept him as a prisoner of war for some time. Soon afterward, he continued to fight in southern Spain, mainly in Cádiz and Gibraltar with the rank of Second Captain of light infantry. He continued to fight Portugal on the side of Spain in the War of the Oranges in 1801. He was promoted to captain in 1804. During his stay in Cádiz he was influenced by the ideas of the Spanish Enlightenment.

At the outbreak of the Peninsular War in 1808, San Martín was named adjutant of Francisco María Solano Ortiz de Rosas. Rosas, suspected of being an afrancesado, was killed by a popular uprising which overran the barracks and dragged his corpse in the streets. San Martín was appointed to the armies of Andalusia, and led a battalion of volunteers. In June 1808 his unit became incorporated into a guerrilla force led by Juan de la Cruz Mourgeón. He was nearly killed during the battle of Arjonilla, but was saved by Sergeant Juan de Dios. On 19 July 1808, Spanish and French forces engaged in the battle of Bailén, a Spanish victory that allowed the Army of Andalusia to attack and seize Madrid. For his actions during this battle, San Martín was awarded a gold medal, and his rank raised to lieutenant colonel. On 16 May 1811, he fought in the battle of Albuera under the command of general William Carr Beresford. By this time, the French armies held most of the Iberian Peninsula under their control, except for Cádiz.

San Martín resigned from the Spanish army, for controversial reasons, and moved to South America, where he joined the Spanish American wars of independence. Historians propose several explanations for this action: the common ones are that he missed his native land, that he was in the employ of the British and the congruence of the goals of both wars. The first explanation suggests that when the wars of independence began San Martín thought that his duty was to return to his country and serve in the military conflict. The second explanation suggests that Britain, which would benefit from the independence of the South American countries, sent San Martín to achieve it. The third suggests that both wars were caused by the conflicts between Enlightenment ideas and absolutism, so San Martín still waged the same war; the wars in the Americas only developed separatist goals after the Spanish Absolutist Restoration.

San Martín was initiated in the Lodge of Rational Knights in 1811. They met at the house of Carlos María de Alvear, other members were José Miguel Carrera, Aldao, Blanco Encalada and other criollos, American-born Spaniards. They agreed to return to their home lands and join the local revolutionary movements. San Martín asked for his retirement from the military, and moved to Britain. He stayed in the country for a short time, and met many other South Americans at a lodge held at the house of Venezuelan general Francisco de Miranda at 27 Grafton Street (now 58 Grafton Way), Bloomsbury, London (the house now has a blue plaque with Miranda's name). Then he sailed to Buenos Aires aboard the British ship George Canning, along with the South Americans Alvear, Francisco José de Vera and Matías Zapiola, and the Spaniards Francisco Chilavert and Eduardo Kailitz. They arrived on 9 March 1812, to serve under the First Triumvirate.