Can't install apt package during install hook

Hello I am running the following to install openjdk inside install hook.

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
apt-get install -y openjdk-8-jre-headless

OUTPUT

/snap/my_app/x1/meta/hooks/install: 14: apt-get: not found

I have also tried installing openjdk-8-jre-headless as stage dependency.

When I run the folowing inside install hook:

update-java-alternatives --jre-headless -s java-1.8.0-openjdk-amd64

I get the following error.

/var/snap/my-app/x1/usr/sbin/update-java-alternatives: line 79: dpkg: command not found
update-alternatives: error: no alternatives for java
update-alternatives: error: no alternatives for jexec
update-alternatives: error: no alternatives for jjs
update-alternatives: error: no alternatives for keytool
update-alternatives: error: no alternatives for orbd
update-alternatives: error: no alternatives for pack200
update-alternatives: error: no alternatives for rmid
update-alternatives: error: no alternatives for rmiregistry
update-alternatives: error: no alternatives for servertool
update-alternatives: error: no alternatives for tnameserv
update-alternatives: error: no alternatives for unpack200
update-alternatives: error: no alternatives for java
update-alternatives: error: no alternatives for jexec
update-alternatives: error: no alternatives for jjs
update-alternatives: error: no alternatives for keytool
update-alternatives: error: no alternatives for orbd
update-alternatives: error: no alternatives for pack200
update-alternatives: error: no alternatives for rmid
update-alternatives: error: no alternatives for rmiregistry
update-alternatives: error: no alternatives for servertool
update-alternatives: error: no alternatives for tnameserv
update-alternatives: error: no alternatives for unpack200

Also the .jinfo file is symlinked and contains the correct path for the java binaries.

PLEASE HELP.

Snaps are not allowed to install traditional debian packages, are you trying to include the jdk package in your snap to use it?

Yes. I am trying to install openjdk-8-jre-headless as a stage package and run the following during install hook.

update-java-alternatives --jre-headless -s java-1.8.0-openjdk-amd64

But I am getting the following error:

/snap/my-app/x1/usr/sbin/update-java-alternatives: line 79: dpkg: command not found
update-alternatives: error: no alternatives for java
update-alternatives: error: no alternatives for jexec
update-alternatives: error: no alternatives for jjs
update-alternatives: error: no alternatives for keytool
update-alternatives: error: no alternatives for orbd
update-alternatives: error: no alternatives for pack200
update-alternatives: error: no alternatives for rmid
update-alternatives: error: no alternatives for rmiregistry
update-alternatives: error: no alternatives for servertool
update-alternatives: error: no alternatives for tnameserv
update-alternatives: error: no alternatives for unpack200
update-alternatives: error: no alternatives for java
update-alternatives: error: no alternatives for jexec
update-alternatives: error: no alternatives for jjs
update-alternatives: error: no alternatives for keytool
update-alternatives: error: no alternatives for orbd
update-alternatives: error: no alternatives for pack200
update-alternatives: error: no alternatives for rmid
update-alternatives: error: no alternatives for rmiregistry
update-alternatives: error: no alternatives for servertool
update-alternatives: error: no alternatives for tnameserv
update-alternatives: error: no alternatives for unpack200

Although dpkg is installed as stage dependency.

My symlinks:

layout:
  /usr/lib/jvm/.java-1.8.0-openjdk-amd64.jinfo:
    symlink: $SNAP_DATA/usr/lib/jvm/.java-1.8.0-openjdk-amd64.jinfo
  /usr/lib/jvm/java-1.8.0-openjdk-amd64:
    symlink: $SNAP_DATA/usr/lib/jvm/java-1.8.0-openjdk-amd64
update-java-alternatives --jre-headless -s java-1.8.0-openjdk-amd64

This will not work from a strictly confined snap by design, this is modifying the host system which is something snaps are not allowed to do

Is it possible to ensure snap services and daemons do not pause and keep running during snap refresh? This is because I don’t want any downtime for my snap service.

You can use refresh-mode: endure in your snap deamon

Thanks for the info so far. One last thing. I am trying to run a command bin/my_app.sh but the bin folder is copied to $SNAP_DATA by install hook to solve “read-only filesystem” error (a Distillery release issue). Is it possible to configure a snap service with command $SNAP_DATA/bin/my_app.sh?

You can have a wrapper script in $SNAP/bin.sh which executes $SNAP_DATA/bin.sh instead but note that by executing files from $SNAP_DATA you are introducing the possibility of your snap breaking if those files on $SNAP_DATA are modified, whereas $SNAP will always be immutable and thus not corruptible.