The question before the court is not a question of "Is Jadhav guilty". Nor is it "Should Jadhav be executed". The court is not investigating a criminal case, as that is a matter for the national courts.
The court only rules on international law, and this case is about access to consular relations. The court has jurisdiction based on treaty agreements of both India and Pakistan. The treaty states:
Disputes arising out of the interpretation or application of the
Convention shall lie within the compulsory jurisdiction of the
International Court of Justice.
Pakistan did object but the court, having considered these objections, found them to be without value. The objections were technical in nature (that India had not offered an alternative dispute resolution forum within two months of notifying Pakistan, had failed to establish Mr Jadhav's nationality) and had not aided Pakistan in the investigation of Mr Jadhav) and not that the court had no jurisdiction in general. Pakistan accepts that it has treaty obligations that allow for referral to the ICJ in regards of access to consular relations.
You should carefully read the judgement for a deeper understanding of how the jurisdiction of the court was established in this case, by treaty.