This is surprisingly hard, especially if you want to find a full JDK, rather than just a JRE. It is a painful bootstrapping problem, along with that of deciding which is the JDK to use.
As it's pre-Java-boot-phase, it ends up being shell script, and then you fall into the differences between bash, Unix sh, cygwin, OS/X.
What might be good would be to say "use Python" (or similar) for all shell-level operations, so we have a language for the shell-level code that is both standard across platforms and human-readable. The java_home detection could be moved to a python class, and others added, along with tests.
Another thing to consider is having some RPMs/Debs that explicitly bond to openjdk, oraclejdk or others; people install the one they want. The problem with this approach is that you end up creating two exclusive packages, and that complicates package deployment testing