It seems to me that China should be able to beat Japan in a war by sheer numbers alone. So, how has Japan been able to compete and in many cases win against a much larger country and populated country than itself.
Japan has only been directly competing against China for less than a century. Hardly "so long".
For the vast majority of its history, pre 1860's, Japan has been a minor blip on the Chinese radar, except for the war in Korea in the late 17th century, which Japan lost.
The reason why Japan competed successfully against China starting in the late 19th century is the same reason why England, France, US and all the other countries competed successfully. It's far too complex a topic to fully treat, but it generally boils down to government corruption and inability to modernize.