Running a Java program from Nautilus

Hi, newcomer here but long-time GNU/Linux user (I started in 1993). So far, I like Clear Linux a lot, even though it’s still a bit rough on the edges compared to say Ubuntu or Fedora.
My question: I installed Java 8 and have an executable JAR file. I can execute from the command-line but in the GUI, Nautilus only proposes me to extract it as an archive. I would welcome some help here :slight_smile:

Can you right-click and open with another application? That should also allow you to change the default program JAR files are opened with: https://help.gnome.org/users/gnome-help/stable/files-open.html

Yes I know this but it unfortunately doesn’t work. When clicking “View All Applications”, I’m not proposed to use Java. I tried the action called “Run Software” but it doesn’t do anything visible. I have Java 8 installed and I also set the executable bit to true on the JAR file in question, in case there was a problem here, but to no avail. Running from the command line with java -jar works perfectly.
The MIME type is identified as “Java archive” (application/x-java-archive), which seems good.

It looks like Nautilus loads the options based on the *.desktop files found in /usr/share/applications. There is not one for Java. (I’d guess most security folks will say it’s a good thing that files aren’t passed to java by default.)

You could define one locally under ~/.local/share/applications/ for java to pass the file to it.

Source:
https://askubuntu.com/questions/162612/how-can-i-add-an-application-to-the-list-of-open-with-applications
https://stackoverflow.com/a/46002376

As @puneetse said, Nautilus lists applications according to the MimeType they have in there *.desktop files.
In I did a `grep -iR ‘.jar’ /usr/share/mime’ and below is the output

./application/x-java-archive.xml:  <glob pattern="*.jar"/>
./globs:application/x-java-archive:*.jar
./globs2:50:application/x-java-archive:*.jar
./packages/freedesktop.org.xml:    <glob pattern="*.jar"/>

In /usr/share/mime/application/x-java-archive, it says

  <alias type="application/x-jar"/>
  <alias type="application/java-archive"/>
  <glob pattern="*.jar"/>

Then it turns out on my system the only application that opens jar is org.gnome.FileRoller.desktop, which is the archive manager of gnome.

So I made a desktop file myself with the following

[Desktop Entry]
Type=Application
Version=1.0
Name=Jar Opener
# This is for opening files, not launching the notebook from a menu
NoDisplay=true
Exec=java -jar %f
Terminal=true
MimeType=application/x-java-archive;

It’s also available here.

Place it under ~/.local/share/applications and run `update-desktop-database ~/.local/share/applications’ and you’re good to go.

P.S. I have to specify the directory when update-desktop-database, otherwise it doesn’t work, because it’s not in $XDG_DATA_DIRS. Maybe we should patch filesystem package to include this directory.

1 Like

It works, thank you very much.

1 Like