Your first instinct may be to run outside; do not do this. Running out into the streets puts you in danger of falling signs, glass, air conditioners, and other harmful objects. All buildings in Japan are designed to withstand strong earthquakes, and the likelihood of the building collapsing is very low.
In the past, gas leaks caused fires that would ultimately claim more lives than the earthquake itself. Now, all apartment buildings in Japan are designed with easy access to the gas on/off switch. Turn this off as soon as possible.
During a strong earthquake, doors and windows can become bent and warped, making them impossible to open. Prop the nearest exit door open at the first sign of an earthquake to avoid getting trapped inside.
Once the gas is off and the escape route ready, protect yourself by getting under a solid object such as a desk or dinner table.
If you happen to be outside, find cover as soon as possible. It may seem counterproductive to run indoors, but you are far more likely to be hit by falling objects on the street than you are inside a building. Also, be wary of the many vending machines in Japanese cities – they are heavy and can tip over.
This may be easier said than done, but the worst thing you can do during an earthquake is to panic and start running. Secure the area (gas and exit door), find a safe place for cover, and wait it out.