Scotland eventually prevailed in their opening World Cup match after defeating South Africa's conquerors, Japan, at Kingsholm.