Score:0

how to run .war with java to execute inside dir from desktop shortcut

ng flag

This is my case. I have a .war that I execute with java and it only works inside its folder:

cd /opt/myappdir/
java -jar myapp.war

Note: It does not work like that:

java -jar /opt/myappdir/myapp.war

I have created a bash script (sudo /opt/myappdir/run.sh) to launch this command

#!/usr/bin/env bash
cd /opt/myappdir/
java -jar myapp.war

So far everything works. Now the problem:

I want to launch it from a shortcut on my desktop. This is my desktop shortcut to start myapp war

[Desktop Entry]
Encoding=UTF-8
Version=1.0
Name=MyApp
Comment=Run MyApp
Type=Application
Exec=/opt/myappdir/run.sh
Icon=/opt/myappdir/myapp.ico
Path=/opt/myappdir/
Terminal=false

But since myapp.war needs to be run with sudo and inside the myappdir I modified my run.sh script as follows

#!/usr/bin/env bash
cd /opt/myappdir/
dir=$(dirname $(readlink -m $BASH_SOURCE))
pkexec env DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY QT_X11_NO_MITSHM=1 java -jar myapp.war

Why line with dir=$(dirname $(readlink -m $BASH_SOURCE))?

because I need to make sure myapp.war is running inside myappdir

Why line with pkexec?

because when double clicking the desktop shortcut java needs to be run with sudo

out:

Error: Unable to access jarfile myapp.war

someone Here offers a solution to a similar question, but it didn't work for me. (Modifying the proposal:)

#!/usr/bin/env xdg-open
[Desktop Entry]
Version=1.0
Type=Application
Terminal=false
Exec=java -jar 'myapp.war'
Path=/opt/myappdir/
Name=myapp
Comment=myapp
Icon=/opt/myappdir/myapp.ico

how can i fix my run.sh script to run myapp.war with sudo inside myappdir, and call it from desktop shortcut? Thanks

Update

According to what someone explains HERE, pkexec does not run in the same directory (I guess that's the reason why it can't find myapp.war file). So the solution could be to run a command that would force it into the directory to run java, but I've tried and failed

Krackout avatar
it flag
How about adding `Path=/opt/myappdir` key in .desktop file? From the desktop file specification, regarding path key: «if entry is of type Application, the working directory to run the program in».
acgbox avatar
ng flag
@Krackout i have tested and nothing happen. same out. The problem is still `run.sh`
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.