Isn't direct fire in naval battle vastly inferior to missiles? Horizon of the sea being roughly 3 miles away on a perfectly clear day, inability for the projectile to refine its trajectory between firing and approaching the target, etc.
It is being developed as a defensive weapon. Mainly against USVs and drones but it is hoped it might have some utility against HGVs too.
JMSDF can't afford to be wasting SAMs (in anti-air or anti-surface mode) if it faces a drone swarm while simultaneously coming under attack from a salvo of AShMs.
The rate of fire of the existing 127mm gun is too low. EDIT- I'm actually not sure if fire rate is an issue, but the rail gun would have better range than the conventional 127mm gun.
Japan recognizes the importance of range in naval battles, which is why the Type 17 AShM is being developed to replace the outdated SSM-1B.
Of course, all of this would make little difference in an all-out conflict given the quantitative superiority the PLA possesses. Even if the railgun could somehow also be effective against HGVs, that isn't going to matter when 052Ds and 055s will be launched large salvoes of them. Same for drones, which could be mass produced in gargantuan numbers.
IMO it is still an admirable effort to respond to the changing threat environment, compared to the US Navy with its infamous "fighting the Houthis is like a warm up for fighting China" mindset and the DOD with its horrendously low requests for SM-3 production.