Simple Java application

Hello, I have been trying to package my simple sample application as a snap for some hours. I got various errors, maybe somebody can help me to understand the mistakes I make :slight_smile:

snapcraft.yaml:

name: test-app
version: dev
summary: TODO summary
description: TODO description
base: core22
grade: devel
confinement: devmode
parts:
  app-jar:
    source: ./build/libs/
    source-type: local
    plugin: dump
    stage-packages:
      - openjdk-17-jdk # TODO headless?
apps:
  server:
    command: $JAVA_HOME/bin/java -jar app.jar
    environment:
      JAVA_HOME: $SNAP/usr/lib/jvm/java-17-openjdk-amd64
      PATH: $JAVA_HOME/bin:$PATH

output:

Launching instance...
Executed: skip pull app-jar (already ran)
Executed: skip overlay app-jar (already ran)
Executed: skip build app-jar (already ran)
Executed: skip stage app-jar (already ran)
Executed: skip prime app-jar (already ran)
Executed parts lifecycle
Generated snap metadata
Unable to determine library dependencies for 'usr/lib/jvm/java-17-openjdk-amd64/lib/libnio.so'
Unable to determine library dependencies for 'usr/lib/jvm/java-17-openjdk-amd64/lib/libsctp.so'
Cannot pack snap file: Command '['snap', 'pack', '--filename', 'test-app_dev_amd64.snap', '--compression', 'xz', PosixPath('/root/prime'), PosixPath('/root/project')]' returned non-zero exit status 1. (2023/01/09 16:17:11.345666 container.go:215: in snap "test-app": path "$JAVA_HOME" does not exist
2023/01/09 16:17:11.345698 container.go:215: in snap "test-app": path "$JAVA_HOME/bin/java" does not exist
2023/01/09 16:17:11.345702 container.go:215: in snap "test-app": path "$JAVA_HOME/bin" does not exist
error: cannot pack "/root/prime": snap is unusable due to missing files)
Failed to execute pack in instance.
Full execution log: '/home/norbi/.cache/snapcraft/log/snapcraft-20230109-171657.121156.log'

Thanks for your help in advance.

You are already putting $JAVA_HOME into your $PATH in the environment block, so just remove $JAVA_HOME/bin/ from your command: entry and you should be good

Simply java -jar app.jar gives error message: path “java” does not exist

I tried with ./java -jar app.jar but the same error is displayed: path “java” does not exist

Maybe I need another dependency, or is the java binary at another location? Is there a way to find its location?

Thanks.

That smells like a snapcraft bug to me, it might be a bit too strict with its checks and not take the PATH from environment into account during build… @sergiusens, @cmatsuoka couldn’t handling that be added to the checks in snapcraft?

1 Like

@ogra, thanks for the prompt reply :slight_smile:

Maybe do you have an idea why this is displayed:

Unable to determine library dependencies for 'usr/lib/jvm/java-17-openjdk-amd64/lib/libnio.so'
Unable to determine library dependencies for 'usr/lib/jvm/java-17-openjdk-amd64/lib/libsctp.so'

Maybe there is a connection between these messages and my problem?

Oh, and to work around it you should be able to simply add the path you have in JAVA_HOME, minus $SNAP/ to your command: entry…

Thanks, with the suggested modification the build works: command: /usr/lib/jvm/java-17-openjdk-amd64/bin/java -jar app.jar

Now I get Error: Unable to access jarfile app.jar when executing the snap command, and the same error when using the absolute path of app.jar: command: /usr/lib/jvm/java-17-openjdk-amd64/bin/java -jar /app.jar

The snap contains the file when I list the contents using unsquashfs:

...
squashfs-root/app.jar

OMG, I’ve read that it is not easy to get started with Snapcraft but I didn’t think that I cannot create a simple helloworld-level snap in hours :frowning:

this is not what i suggested … you should have removed “$SNAP/” … i.e. the leading slash as well … so you end up with:

command: usr/lib/jvm/java-17-openjdk-amd64/bin/java -jar app.jar

snapd will execute it in context of $SNAP then and should find the command just fine.

if you keep the slash as you did, it will actually try to find /usr/lib/jvm/… inside your base snap (which is your / at execution time of your snap) and indeed fail since the core22 snap does not ship java :wink:

regarding finding app.jar, here you might need to use $SNAP/app.jar …

Hmmm, interesting because it works with the leading / as well :slight_smile:
The command

command: /usr/lib/jvm/java-17-openjdk-amd64/bin/java -version

results in the following console output:

openjdk version "17.0.5" 2022-10-18
OpenJDK Runtime Environment (build 17.0.5+8-Ubuntu-2ubuntu122.04)
OpenJDK 64-Bit Server VM (build 17.0.5+8-Ubuntu-2ubuntu122.04, mixed mode, sharing)

The output is the same when the leading / is omitted…

@ogra, I really appreciate your help, I’m a little ashamed to ask another one:

Do you have a tip where should I look for the app.jar file in the installed snap?

quoting from above:

1 Like

Sorry, for some reason I slipped through it :thinking:

you might need to use $SNAP/app.jar

It works! Thanks a lot!
Now I have to read the documentation more throughly because this system is more complex than it first seems (from a user’s perspective) :slight_smile:

1 Like

We’ll need to think about the best way to handle this. @snorbi, can you open a snapcraft bug for that so we won’t forget?

1 Like

These are linter warnings, you can disable this message in snapcraft.yaml by adding something like:

lint:
  ignore:
    - library:
      - usr/lib/jvm/java-*/lib/*.so
1 Like