The screenshot below shows the error message in German: "Um das java-Befehlszeilenprogramm nutzen zu können, musst du ein Java-Entwicklerpaket installieren." In English it means: "To use the java command-line tool you need to install a JDK".
Since I am a developer, I always installed the JDK on my Mac and I detected that phenomenon very late. Actually the web is full of those traces - however, without a suitable solution in my opinion. Well, I simply don't want to tell my users to install a JDK if a simple JRE is enough. Any existing JRE on the system should do the job in my humble opinion.
Here we go, here is my little bash launcher that tries its best to launch java even if you have installed a JRE only on your Mac:
#!/bin/bash
if [[ ! -z $JAVA_HOME ]]; then
JEXEC=$JAVA_HOME/bin/java
else
LIBEXEC=$(/usr/libexec/java_home 2> /dev/null | head -1)
# is there a JDK?
if [[ ! -z $LIBEXEC ]]; then
JEXEC="$LIBEXEC/bin/java"
else
# is there a JRE?
JRE=/Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java
if [[ -f "$JRE" ]]; then
JEXEC="$JRE"
else
JEXEC=java
fi
fi
fi
"$JEXEC" "$@"
The script checks for the JAVA_HOME environment variable and if that is not set, it checks for any registered JDKs by calling /usr/libexec/java_home and if that didn't return anything, it simply uses the JRE that could be available at a well known path on macOS (tested with both Java 8u131 and Java 9-ea) and if that fails as well, it uses java and if even that fails it means you really don't have any Java installed and you should get the error message above again.
I use the launcher above already as part of the Jacksum macOS Finder integration. See also
http://jacksum.net/de/tutorials/integration_jacksum_osx_finder.html
Feel free to use the launcher script for your Java app as well if it meets your needs.