Run daemon using JRE

Hey,
I have read read previous posts on this theme, such as this one and this one but I seem to just be able to get this working.

I am trying to include languagetool as a java part and run it as a daemon. I added:

  languagetool:
    plugin: dump
    source: https://languagetool.org/download/LanguageTool-4.7.zip
    organize:
      LanguageTool-4.7: lt
    stage:
      - lt/
    stage-packages:
      - openjdk-8-jdk

Despite the documentation saying it can only use tar-balls as source, the zip files is being decompressed fine and the folder within it is then moved to the folder lt which is present in both stage and prime.

But the corresponding app doesn’t work:

languagetool:
    command: $JAVA_HOME/jre/bin/java -cp $SNAP/lt/languagetool-server.jar org.languagetool.server.HTTPServer --port 8081
    environment:
      JAVA_HOME: $SNAP/usr/lib/jvm/java-1.8.0-openjdk-$SNAP_ARCH
      PATH: $JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH
    daemon: simple
    restart-condition: always
    plugs: [network, network-bind]

The error is:

Failed to generate snap metadata: The specified command ‘$JAVA_HOME/jre/bin/java -cp $SNAP/lt/languagetool-server.jar org.languagetool.server.HTTPServer --port 8081’ defined in the app {‘environment’: {‘PATH’: ‘$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH’, ‘JAVA_HOME’: ‘$SNAP/usr/lib/jvm/java-1.8.0-openjdk-$SNAP_ARCH’}, ‘command’: ‘$JAVA_HOME/jre/bin/java -cp $SNAP/lt/languagetool-server.jar org.languagetool.server.HTTPServer --port 8081’, ‘plugs’: [‘network’, ‘network-bind’]} does not exist.
Ensure that ‘$JAVA_HOME/jre/bin/java -cp $SNAP/lt/languagetool-server.jar org.languagetool.server.HTTPServer --port 8081’ is installed with the correct path.

I tried running snapcraft with snapcraft --use-lxd --debug which let me enter the container, but apparently without the environment variables set, so I cannot check whether $SNAP is what it should be, etc. . Instead of openjdk-8-jdk I also tried with openjdk-11-jre-headless and it gave me the same result. There seems to be little or no information about how to run a java app or how to debug any of this in the snapcraft documentation so it’s very much trial and error it feels like. The full snap recipe can be found here.

since you are adding $JAVA_HOME/jre/bin to your PATH already you should not define the full path in the command … just trim it down to a simple java -cp $SNAP/lt/...

@ogra I tried that first, but it didn’t work, so I added the full path.

did you check that java is actually in the desired path (look in the prime/ dir of your build) ?

I suspected that I should be looking in prime/user/lib/jvm/...but the jvm folder doesn’t exist. Maybe the jdk or jre packages are not installed for some reason?

@ogra This seems to work, but it’s installing a lot of stuff I don’t really need as I don’t use gradle. Using the gradle plugin gave me the jre at runtime and that has so far been the only way I have been able to run a java program:

parts:
  languagetool:
    plugin: gradle
    source: https://languagetool.org/download/LanguageTool-4.7.zip
    override-build: |
      cp -a LanguageTool-4.7 $SNAPCRAFT_STAGE/lt
      cp -a LanguageTool-4.7 $SNAPCRAFT_PRIME/lt

apps:
  languagetool:
    command: java -cp $SNAP/lt/languagetool-server.jar org.languagetool.server.HTTPServer --port 8081
    environment:
      JAVA_HOME: $SNAP/usr/lib/jvm/java-11-openjdk-$SNAP_ARCH
      PATH: $JAVA_HOME/bin:$PATH
    daemon: simple
    restart-condition: always
    plugs: [network, network-bind]

looking at my jtiledownloader package, which also stages openjdk-8-jdk

i see the java binary in:

/snap/jtiledownloader/current/usr/lib/jvm/java-8-openjdk-amd64/bin/java

/snap/jtiledownloader/current should be equivalent to prime/ in the build tree

(note, this package is very old, there are nowadays better wrappers and the like, but perhaps you can steal some ideas from it)

I actually looked at that package first and even though I thought I had done the exact, it just didn’t work, it’s very hard to debug what is actually going on and there seems to be no real documentation.
Maybe this stopped working with core18 at some point and noone noticed?
Now that the fidusewriter snap is somehow magically working, I’m reluctant to fiddle more with it.
Hopefully snapcraft gets a little better at all this so others don’t have to spend as much time on something that should be trivial.

@ogra thanks so much for your help!

hah, you should have mentioned you are building with core18 base … indeed the openjdk-8-jdk might have changed since 16.04 :wink:

I see. I just thought it was the default in 2019 to base everything on the latest LTS. Is that not the case?