Snap from .jar

How to snap a jar file?
I am new to snapcraft. When I create a snap it shows “Error: Unable to access jarfile”. I search lot but it didn’t work. some one give me a example for creating snapcraft.yaml file.

1 Like

Hi Siva.

I have a snap package published in the store and it is a Java application. The code is published on Bitbucket. This is the link, so you can check the snapcraft.yaml file and the rest of the structure. I hope this helps!!

https://bitbucket.org/eloy_garcia_pca/wallpaperdownloader/src/master/snap/

Cheers.

hi, I am new to snap. this example is difficult to me, can you give me some simple snap.

Are you using maven to manage the Java application?

If you are, then wallpaperdownloader part should be very similar. The maven plugin will install java and maven during the build process and then will execute maven to build the jar file against the code stored within the source path. The jar will be placed in $SNAP_USER_DATA $SNAP/jar/ path, so you will have to create a simple script like this https://bitbucket.org/eloy_garcia_pca/wallpaperdownloader/src/master/snap/scripts/bin/wallpaperdownloader.sh to launch the application.

If you check this script you’ll see desktop-launch wrapper which will be used because this application is a graphical application and desktop-launcher provides some nice integration between the snap and the desktop. You can see this integration within the snapcraft.yaml file in line after: [desktop-gtk3].

If you are new to snapcraft maybe is a good idea to check some of the tutorials that you can find here filtering by Packaging.

This could be a good starting point:

https://tutorials.ubuntu.com/tutorial/create-your-first-snap#0

I hope this helps :slight_smile:

Cheers!

Thanks for your reply… I am using ant plug-in in my project. But now I want to know how to make a snap from a .jar, so I use the simple example. But it din’t work. My snapcraft.yaml file is:

grade: stable
confinement: strict

apps:
  skide:
     command: java -jar  SnapTest.jar
parts:
  skide:
    source: .
    plugin: jdk

It shows Error: Unable to access jarfile SnapTest.jar. Could you help me this???

Maybe I’m wrong, but I think jdk plugin is not what you want here. First, you have to invoke ant to build your package and then you will have the jar file available. There is a specific plugin for this:

Here you have some examples:

If you only want to test the launching of the jar file, you could use the dump plugin to copy the jar you want into the snap and then it will be available later

More examples of the dump plugin here:

First of all sorry for not clearly explain my problem .

Here in this, I already have a compiled jar file. So I want to snap it. I didn’t use ant for this example.
In this example it gives an error unable to read jar file. Is there any other way to create snapcraft.yaml for this. And also can you give me the syntax for the snapcraft.yaml.
By understanding this I can change my project, So that only I try this example. But it gives error. I do lot of changes in it, but not single one worked.

I don’t know how to create build.xml file (ant) for compiled jar. I hope you understand my problem here.

That is what I was thinking… I supposed it after reading your reply, sorry. As I mentioned before, if this is only a test with a jar stored within your directory then you could try the dump plugin.


If you only want to test the launching of the jar file, you could use the dump plugin to copy the jar you want into the snap and then it will be available later

More examples of the dump plugin here:

I hope this helps!!!

Thank you eloy.garcia.pca.
Now I changed the plug in as dumb.

grade: stable
confinement: strict

apps:
test:
command: java -jar $SNAP/src/SnapTest.jar

parts:
test:
source: .
plugin: dump

Now it shows command-test.wrapper: 4: exec: java: not found
The exec command is exec “java” “-jar” “$SNAP/src/SnapTest.jar” “$@”.
Is it right or wrong?. or am I need to change the command. Pls help with this…

Now you need to include the jre within the snap package in order to be able to use the java command.

This is an example. It is a bit outdated but maybe it can help

Thank you eloy.garcia.pca.

Its working perfectly…

My snapcraft.yaml is:

grade: stable
confinement: strict
apps:
test:
command: usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java -jar $SNAP/src/SnapTest.jar
plugs: [unity7, network, home]
environment:
JAVA_HOME: $SNAP/usr/lib/jvm/default-java
CLASSPATH: $SNAP/jar/SnapTest.jar
PATH: $SNAP/usr/lib/jvm/default-java/bin:$PATH
parts:
test:
source: .
plugin: dump
stage-packages: [openjdk-8-jre, openjdk-8-demo]

Its been a nice conversation with you. I learned lot in this…

Can you tell me how to use external libraries in snap?
I have a compiled jar, and the libraries for that jar. Now how I imported with the snap?..

It is great that your test is working!! :slight_smile:

In my own project, I use maven to create a jar file with all the dependencies included in this jar. I don’t know if you can use ant to do the same thing… I’m not very familiar with it.

If those libraries are packaged in Ubuntu, you could include them as stage-packages. Then, when the code in your jar invokes them, they will be installed in your snap.

Cheers!

I think the dump is also enough for my project. I have all the library files in my local. The library files like postgres_jdbc (external jar files).

But I don’t know how to use this files in snap. By above test example the snap access the jar. But it does not read the libraries.

Hi again!

Typically, when you install java libraries in Debian/Ubuntu they are stored in /usr/share/java. I would try to dump those libraries to this location in the snap package. If it doesn’t work, maybe you should modify the jar’s MANIFEST file and include here the path to those libraries manually copied using the dump plugin.

Hi eloy.garcia.pca…

I tried to put the source jar and the library files in same path to dump it. The snap dump the libraries in the part. But the source didn’t access the library files. And shows NotFoundException in it…

Is there any other way to do it?..

Hi @Siva

Sorry, but the two ideas that I wrote in the previous post are the only ones that come to my mind… Maybe other snapcraft users can help with this : :sob:

Hi eloy.garcia.pca

Sorry for the late reply, I tried to edit source jar’s MANIFEST file. But there is no need to edit that, when you using dump plug-in. Simply move the external jar’s file according to the CLASS_PATH that saved inside the source jar’s MANIFEST file.

It work for me… Once I moved the external jar based on the Class Path.

For Example: Class-Path: lib/javax.mail.jar lib/postgresql-9.0-801.jdbc4.jar.
for this I created the lib folder in the source folder itself.

Good to hear!!! :slight_smile:

I’m happy that you could resolve this problem.

Cheers!!!

Eloy

Thank you Eloy…
It’s done because of your help man…