How set JAVA_HOME?

I run a jar file no problem like this:
command: usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java -jar $SNAP/bin/ajar.jar
Now java won’t be in the same directory for everyone, so I was trying unsuccessfully to do this:

command: $JAVA_HOME/jre/bin/java  $SNAP/bin/ajar.jar
daemon: simple
plugs: [unity7, network, home]
environment:
  JAVA_HOME: "$(jrunscript -e 'java.lang.System.out.println(java.lang.System.getProperty("java.home"));')"

which gives:
did not find expected key on line 19, column 95
Is there a better way of finding $JAVA_HOME, am I using it correctly, and can it default to something if not found?

I do not have a /usr/lib/jvm/default-java folder

In the context of snaps, java will in fact be installed for everyone in exactly the same location inside the snap (except for the architecture). I have a java game snap, and I do this in the command:

command: bash -c "${SNAP}/usr/lib/jvm/java-8-openjdk-${SNAP_ARCH}/bin/java -version ; ${SNAP}/usr/lib/jvm/java-8-openjdk-${SNAP_ARCH}/bin/java -Djava.util.prefs.userRoot=\"$SNAP_USER_DATA\" -jar $SNAP/jar/somejarfile.jar $*"

However, I could also set JAVA_HOME in the environment section using the same path (which would probably be better), as you suggest. But, AFAIK, you don’t need anything fancy. Just hardcode a path to the jvm with SNAP_ARCH and I think you’ll be okay, since you’re controlling exactly which version of the JDK is being installed via build and staging packages.

That is very useful to know. Thanks.