His knowledge of the Malay language as well as his wit and ability, gained him favour with Lord Minto, Governor-General of India, and he was sent to Malacca. Then, in 1811, after the invasion and annexation of the Kingdom of Holland by France during Napoleon's war, Raffles had no choice but to leave the country. He mounted a military expedition against the Dutch and French in Java, Indonesia. The war was swiftly conducted by Admiral Robert Stopford, General Frederick Augustus Wetherall, and Colonel Rollo Gillespie, who led a well-organized army against an army of mostly French conscripts with little proper leadership. The previous Dutch governor, Herman Willem Daendels, had built a well-defended fortification at Meester Cornelis (now Jatinegara), and at the time, the governor, Jan Willem Janssens (who, coincidentally, surrendered to the British at the Cape Colony), mounted a brave but ultimately futile defence at the fortress. The British, led by Colonel Gillespie, stormed the fort and captured it within three hours. Janssens attempted to escape inland but was captured. The British invasion of Java took a total of forty-five days, during which Raffles was appointed the Lieutenant-Governor by Lord Minto before hostilities formally ceased.
He was appointed assistant secretary to the new Governor of Penang in 1805 and married Olivia Mariamne Fancourt, a widow who was formerly married to Jacob Cassivelaun Fancourt, an assistant surgeon in Madras who had died in 1800. At this time he also made the acquaintance of Thomas Otho Travers, who would accompany him for the next twenty years.