James was the younger brother of Charles II. He escaped to the continent during the Civil War and had a distinguished military career in the French and Spanish armies before returning to London at the Restoration. 

Because of growing disquiet about James' Roman Catholicism, Charles told him that he must leave England temporarily. After a period of exile in Holland, James went to Scotland as the King's Lord High Commissioner in 1679 and 1680. 

He stayed at the Palace of Holyroodhouse in Edinburgh and culture flourished in the capital under the patronage of his vice-regal Court.

James inherited the throne on Charles II's death in 1685 with little opposition in Scotland. A rising led by the Earl of Argyll was easily suppressed, although suspicions increased that James was trying to introduce Roman Catholicism when he made grants of religious toleration and converted the Abbey Church of Holyrood into a Roman Catholic Chapel Royal (it was also planned to be the Chapel of the Order of the Thistle, which James revived in 1687). 

Nevertheless, Scotland played a largely passive role in the revolution of 1688 until news of events in England and James' flight were followed by the collapse of the Scottish administration in late December.

A mob drove the Jesuits from Holyrood, sacked the Chapel Royal and desecrated the royal tombs. Constitutionally, however, James remained king until 4 April 1689, when the Convention of Estates voted that he had forfeited the crown and offered the throne jointly to William and Mary. 

The Scottish Catholics, led by Viscount Dundee, fought for James at the battle of Killiecrankie in 1689 and won, but Dundee died in the battle and the leaderless Jacobite challenge disintegrated. 

Defeated by William II and III at the Battle of the Boyne in 1690, James spent the rest of his life in exile in France, and died there in 1701.