Score:0

How to configure .desktop file to run .jar file exactly as if run from terminal

cn flag

I have a java program that works correctly when I run java -jar "lizzie.jar" from the terminal, but when I double click it in Nautilus it runs (i.e. UI shows) but returns the error "No such file or directory" for one of the files (./leelaz) it depends on to work properly.

I have created a .desktop file in /home/username/.local/share/applications with the following contents:

#!/usr/bin/env xdg-open
[Desktop Entry]
Name=Lizzie
Type=Application
Exec=java -jar "/home/username/opt/Lizzie/lizzie.jar"
Icon=/home/username/opt/Lizzie/lizzie_custom_logo.png
Comment=Lizzie
Terminal=false

Running this yields the same error as double-clicking.

I found and read this post, and I've tried using both Terminal=true and Terminal=false. The file lizzie.jar is set to allow executing as program and both when running from Nautilus and terminal I am not root. This might of course be an issue for Lizzie and not Ubuntu, but I think I should be able to generate the same behavior as if run from terminal.

Dist: Ubuntu 18.04.6 LTS; Kernel: 5.4.0-99-generic

java -version returns:

openjdk version "11.0.13" 2021-10-19
OpenJDK Runtime Environment (build 11.0.13+8-Ubuntu-0ubuntu1.18.04)
OpenJDK 64-Bit Server VM (build 11.0.13+8-Ubuntu-0ubuntu1.18.04, mixed mode, sharing)

Question: What is the likely reason the behavior from double-clicking and running the .desktop is different from terminal, and how may I configure a .desktop file that gives exactly the same behavior as if I ran java -jar lizzie.jar from the terminal?

N.B. The program runs in both cases, but it can't access the dependency ./leelaz in the case of double-clicking or using .desktop. Lizzie then reports: Cannot run program "./leelaz": error=2, No such file or directory. But, the file ./leelaz is there, and again, everything works when run from terminal.

(In case of interest, the application is Lizzie, an interface for the Go AI Leela Zero.)

Martin Thornton avatar
cn flag
Does this answer your question? [How can I start an executable jar file from a desktop file in Lubuntu?](https://askubuntu.com/questions/684581/how-can-i-start-an-executable-jar-file-from-a-desktop-file-in-lubuntu)
hr flag
Possibly related: [Why isn't tilde recognised as home folder in this case?](https://askubuntu.com/questions/1192981/why-isnt-tilde-recognised-as-home-folder-in-this-case)
Christopher.L avatar
cn flag
@MartinThornton I'm afraid it doesn't, and I have edited the question; I only wrote '~' in the path to avoid writing my username, but ofcourse I can just write anything, I just thought they were equivalent. The actual path is complete in the .desktop file.
Christopher.L avatar
cn flag
Also, as pointed out, the app does start, i.e. the gui of the application shows, but I get an error. This is why I wrote that it might be a question for the Lizzie-devs, but I think it is strange that it does work from the terminal, so that I feel I should be able to fix it.
Score:1
cn flag

What is the likely reason the behavior from double-clicking and running the .desktop is different from terminal

When successfully running java -jar "lizzie.jar", your current working directory is the same as the directory where the file lizzie.jar resides. Otherwise, lizzie.jar would not be found.

lizzie.jar apparently expects to find files it needs in its own directory. You therefore need to make sure to set the current directory to that where "lizzie.jar" resides.

how may I configure a .desktop file that gives exactly the same behavior as if I ran java -jar lizzie.jar from the terminal?

Add the following desktop entry to the .desktop launcher:

Path=/home/username/opt/Lizzie/
Christopher.L avatar
cn flag
This solved my problem! I am a bit curious as to why double-clicking the .jar file gave the same behavior though; wouldn't the working directory just be set to the directory of the file I am double clicking?
vanadium avatar
cn flag
Launching a file by double clicking launches it in the directory where it resides.
Christopher.L avatar
cn flag
Exactly, and this is `/home/username/opt/Lizzie/` , i.e. the same as the path I set in .desktop which made the desktop-launcher work.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.