Snapcraft PLUGIN -- jdk (Java runtime) ---> app Error

[snapcraft - parts] “… If you only need to embed a Java runtime, add a part with the jdk type. This will pull a relocatable OpenJDK via the default-jdk Ubuntu package and will set the proper JAVA_HOME and PATH environment variables in wrapper scripts generated by Snapcraft or when running the app locally.”

Hi!

What is the difference between the ‘snapped’ java runtime (jre) and the installed jre?

While my app is running with the installed jre it won’t work with the ‘snapped’ one…
After hours and hours without any solution I decided to take two easy examples from the www —
and …? These converted to ‘jdk-snaps’ did NOT work either (same problem).

Many thanks,
Juergen


You can try it yourself: test.tar.gz
Start test1/test2* with Java installed. Then craft a snap** (test) and try that one.

(*) works fine. Right?
(**) does not work. Right again?

:wink:

Hi there, thanks for taking an interest.

The jdk plugin is mostly here for legacy reasons, we encourage people to go the path of using the ant, gradle[w] or maven plugins to get java based applications working.

A snapped jre in a confined environment with no application to it added is mostly useless (exceptions apply) as it will not be able to see what is on the filesystem by default.

Let’s see if we can approach this differently, tell us what you are trying to get done and let’s see where we can go from there.

i have a snapped java GUI app at https://github.com/ogra1/jtiledownloader perhaps that gives you some ideas (i would do it differently nowadays and it might be that there is also a java-desktop part the could replace the wrapper script by now)

@sergiusens
See “application” test1 and “application” test2 in the attached file (test.tar.gz).

@ogra
How would you do it and what is the right/best way to do it resp.?

I wouldnt use a Makefile but snapcraft scriptlets and would check if there is an existing desktop wrapper that provides the environment the current wrapper script sets up …

The resulting snap would be similar though, it is really up to you how you perfer to buld it in the end. The Makefile setup is definitely fine too, i just dont do it like that personally anymore because it appears cleaner to have everything in snapcraft.yaml if possible.

After all the important bits are to have openjdk-8-jdk in stage-packages and to have the JAVA_HOME and PATH variables set like in the wrapper … since i created the original snap snapcraft grew a lot additional support (like the environment keyword so you wouldnt need a wrapper) to put things directly into snapcraft.yaml

I tried that… It ended up with no Java included in the snap!?

This is done by the jdk plugin already!?
OR ???

Did you actually put it in stage-packages (not in build-packages) ?

The openjdk package installs the java bits in
$SNAP/usr/lib/jvm/java-8-openjdk-amd64/
(and the wrapper should use this as JAVA_HOME)

If you “snap install jtiledownloader” you will actually see the java executable in:

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

Your snap should not behave any different …

@ogra — Yes, I did …

In the meantime my first, easier approach does work: https://git.launchpad.net/ttimer/tree/ubuntu/i386/snap/snapcraft.yaml

Many thanks! :slight_smile:

1 Like

After installing the snap. the openjdk package is not installed in my local machine. i had mentioned it in stage-package.
please help me with this.
@ogra I tried using your snap. Still after installing jtiledownloader , the openjdk package is not installed. Can u help me with this?

if you install my jtiledownloader snap the jdk runtime is in:

/snap/jtiledownloader/current/usr/lib/jvm/

… as it should be … (the openjdk is typically installed inside the snap, not “on your system”)

what are the actual errors you see, do you have a public git tree of your snap source ?

@ogra. jtiledownloader is working fine. Thanks
I want to try doing it with my snap. I will post the query if I have any.

umang@umang-HP-15-Notebook-PC:~/Desktop/work/test14$ snap info jtiledownloader
name: jtiledownloader
summary: A Java application for downloading OpenStreetMap tiles
publisher:
description: |
jTileDownloader allows downloading OpenStreetMap files for offline
map caching
commands:

  • jtiledownloader
    tracking:
    installed: 0.6.2 (x1) 92MB devmode
    refreshed: 2018-01-31 17:07:06 +0530 IST
    channels:
    stable: 0.6.1-1 (2) 86MB -
    candidate: 0.6.1-1 (2) 86MB -
    beta: 0.6.1-1 (2) 86MB -
    edge: 0.6.1-1 (2) 86MB -
    umang@umang-HP-15-Notebook-PC:~/Desktop/work/test14$ jtiledownloader
    jtiledownloader: command not found
    umang@umang-HP-15-Notebook-PC:~/Desktop/work/test14$

i was checking the jtiledownloader. I am getting the error. command not found
@ogra please help me with this.

What is your host OS ?
(typically the snap installs a jtiledownloader link in /snap/bin, some distros do not set this in their PATH variable, so you would have to either call /snap/bin/jtiledownloader directly or calll “snap run jtiledownloader”)

thanks it worked…but now for my application it is not working…

my error is

umang@umang-HP-15-Notebook-PC:~/Desktop/work/test16$ /snap/bin/buga-stock
Error: Unable to access jarfile /snap/buga-stock/x1/buga-stock.jar

yaml file

name: buga-stock
version: “2.1”
summary: A Java application for stock
description: |
stock
confinement: strict

apps:
buga-stock:
command: bin/launcher.sh
plugs: [home, x11, network, network-bind]

parts:
buga-stock:
source: /home/umang/Downloads/buga-stock.zip
plugin: dump
stage-packages: [openjdk-8-jre]
launcher:
source: .
plugin: dump
organize:
launcher: bin/launcher.sh

and shell script file

#!/bin/bash

if [ “$SNAP_ARCH” = “amd64” ]; then
ARCH=“x86_64-linux-gnu”
elif [ “$SNAP_ARCH” = “armhf” ]; then
ARCH=“arm-linux-gnueabihf”
else
ARCH="$SNAP_ARCH-linux-gnu"
fi

export JAVA_HOME=$SNAP/usr/lib/jvm/java-8-openjdk-$SNAP_ARCH
export PATH=$JAVA_HOME/jre/bin:$PATH

Not good, needed for fontconfig

export XDG_DATA_HOME=$SNAP/usr/share

Font Config

export FONTCONFIG_PATH=$SNAP/etc/fonts/config.d
export FONTCONFIG_FILE=$SNAP/etc/fonts/fonts.conf

java -jar -Duser.home=$SNAP_USER_DATA $SNAP/buga-stock.jar

@ogra look into this please.

can you please format your .yaml pastes as code (either mark it in the input editor and click the </> button, or wrap it with three backticks ```at the start and end of the paste ) else this is unreadable …

name: buga-stock
version: "2.1"
summary: A Java application for stock 
description: |
 stock
confinement: strict

apps:
    buga-stock:
        command: bin/launcher.sh
        plugs: [home, x11, network, network-bind]

parts:
    buga-stock:
        source:  /home/umang/Downloads/buga-stock.zip
        plugin: dump
        stage-packages: [openjdk-8-jre]
    launcher:
        source: .
        plugin: dump
        organize:
           launcher: bin/launcher.sh```

does buga-stock.zip actually end up unzipped in your snap ? do you see buga-stock.jar it in /snap/buga-stock/current/ when you install the snap ?

Error:

umang@umang-HP-15-Notebook-PC:~/Desktop/work/test16$ /snap/bin/buga-stock
Error: Unable to access jarfile /snap/buga-stock/x1/buga-stock.jar