Execute Jar from Bash Script

Forum rules
Before you post please read this

Execute Jar from Bash Script

Postby bulsatar on Fri Nov 01, 2013 8:31 pm

Evening Everyone,

Trying to create a bash script so I can make a jar file semi-executable with a shortcut on the desktop. The file is apophysis-j-28.jar (can be found here http://jfbouzereau.free.fr/apophysis.html). All of the bash I have looked at after googling says that java -jar path/file.jar should work.

Contents of my bash file (called apophysis.sh and also chmod to be executable and in the same dir as the jar)
Code: Select all
#!/bin/bash
java -jar ./apophysis-j-28.jar


appreciate any help!
bulsatar
Level 1
Level 1
 
Posts: 46
Joined: Sun May 26, 2013 1:05 am

Linux Mint is funded by ads and donations.
 

Re: Execute Jar from Bash Script

Postby karlchen on Fri Nov 01, 2013 8:47 pm

Hello, bulsatar.

Can you explain what the problem with this script apophysis.sh is?
I assume that if you proceed like this it should work:
Code: Select all
cd /full/pathname/where/apophysis.sh/lives
./apophysis.sh

Replace /full/pathname/where/apophysis.sh/lives by the full pathname of the folder where the script apophysis.sh and the jar file apophysis-j-28.jar live.

Example:
Folder: /home/bulsatar/apophysis
If this were the full pathname of the folder, then you would execute this:
Code: Select all
cd /home/bulsatar/apophysis
./apophysis.sh


If you proceed accordingly - using the real foldername of course - does it work?
If it does not which error message do you receive?

Kind regards,
Karl
Image
Enlightened by Lucid Lynx, enchanted by Maya, impressed by Petra.
User avatar
karlchen
Level 9
Level 9
 
Posts: 2520
Joined: Sat Dec 31, 2011 7:21 am

Re: Execute Jar from Bash Script

Postby bulsatar on Fri Nov 01, 2013 8:57 pm

no errors and nothing appears. I click on the script, it asks what I want to do so I tell it to Run and then nothing happens. I tried your suggestion of cd ing. same thing happened.

Code: Select all
#!/bin/bash
cd /home/bulsatar/apophysis-j-28
java -jar ./apophysis-j-28.jar
bulsatar
Level 1
Level 1
 
Posts: 46
Joined: Sun May 26, 2013 1:05 am

Re: Execute Jar from Bash Script

Postby karlchen on Fri Nov 01, 2013 9:18 pm

Hello, bulsatar.

In this case let us make sure what your commands inside the script do at the command prompt, before trying to launch the script by double clicking its name from inside a graphical file-manager.
By the way, which file-manager do you use? Caja, Nautilus, Nemo, yet another?

Open a terminal window. At the command prompt execute the commands that are in your script:
Code: Select all
cd /home/bulsatar/apophysis-j-28
does this command work? Or does it return an error message?

Provided the first command worked, execute this command:
Code: Select all
java -jar ./apophysis-j-28.jar
What does it do?

Kind regards,
Karl
Image
Enlightened by Lucid Lynx, enchanted by Maya, impressed by Petra.
User avatar
karlchen
Level 9
Level 9
 
Posts: 2520
Joined: Sat Dec 31, 2011 7:21 am

Re: Execute Jar from Bash Script

Postby bulsatar on Fri Nov 01, 2013 9:23 pm

from the cli it works and opens up the program....am I missing something in the bash file then??
bulsatar
Level 1
Level 1
 
Posts: 46
Joined: Sun May 26, 2013 1:05 am

Re: Execute Jar from Bash Script

Postby karlchen on Fri Nov 01, 2013 9:52 pm

Hello, bulsatar.

The fact that both commands worked correctly is a good sign. The content of the bash script seems to be correct.
You stated that the shell script ./apophysis.sh were stored in the same folder as your jar file apophysis-j-28.jar.
What happens if you execute the script from the commandline? I.e. inside the terminal window execute the commandline
Code: Select all
/home/bulsatar/apophysis-j-28/apophysis.sh
Does this work, too?

Karl
Image
Enlightened by Lucid Lynx, enchanted by Maya, impressed by Petra.
User avatar
karlchen
Level 9
Level 9
 
Posts: 2520
Joined: Sat Dec 31, 2011 7:21 am

Re: Execute Jar from Bash Script

Postby karlchen on Sat Nov 02, 2013 9:21 am

<excursion: a working example>

Hello, bulsatar.

Here is a working example, taken fom my own machine.

Java programme to be launched: /opt/lxmed/LXMenuEditor.jar (Cf. [1])
Script folder: /home/karl/Scripts
Script name: run_lxmed.sh
Script content:
Code: Select all
#!/bin/bash
java -jar /opt/lxmed/LXMenuEditor.jar

Make script executable:
Code: Select all
chmod +x /home/karl/Scripts/run_lxmed.sh
Could have been done inside my graphical filemanager Thunar as well by
+ navigating to the folder /home/karl/Scripts
+ right-clicking on run_lxmed.sh
+ selecting "Properties" from the context menu
+ going to the tab "Privileges"
+ ticking the option: [X] File may be run as a programme
+ clicking [Close]

Launching run_lxmed.sh from inside Thunar:
+ Open Thunar filemanager
+ Navigate to the folder /home/karl/Scripts
+ Right-click on run_lxmed.sh
+ Select "Execute" from the context menu

Result:
A few moments later the application window "LXDE Main Menu Editor" will be displayed on the screen.

I cannot quite imagine why the corresponding steps should not work when applied to your script.

</excursion: a working example>
Image
Enlightened by Lucid Lynx, enchanted by Maya, impressed by Petra.
User avatar
karlchen
Level 9
Level 9
 
Posts: 2520
Joined: Sat Dec 31, 2011 7:21 am

Re: Execute Jar from Bash Script

Postby karlchen on Sat Nov 02, 2013 9:51 am

<example applied to your programme apophysis-j-28>

Hello, bulsatar.

Downloaded the file apophysis-j-28.zip and extracted it to the folder /home/bulsatar/apophysis-j-28.

Java programme to be launched: /home/bulsatar/apophysis-j-28/apophysis-j-28.jar
Script folder: /home/bulsatar/apophysis-j-28
Script name: apophysis.sh
Script content:
Code: Select all
#!/bin/bash
java -jar /home/bulsatar/apophysis-j-28/apophysis-j-28.jar

Make the script executable:
Code: Select all
chmod +x /home/bulsatar/apophysis-j-28/apophysis.sh

Launching apophysis.sh from inside the filemanager:
+ Open filemanager
+ Navigate to the folder /home/bulsatar/apophysis-j-28
+ Right-click on apophysis.sh
+ Select "Execute" from the context menu

Result:
A few moments later the application window "apophysis-j 2.8" will be displayed on the screen.

apophysis-j 2.8.png
Apophysys-j 2.8


Note:
In case the need should turn up to allow the jar programme to allocate more memory, in this case you should modify add the Java -Xms and -Xmx commandline options to your script, thus telling Java to use a minum heap size of -Xms MB and a maximum heap size of -Xmx MB.
Code: Select all
#!/bin/bash
java -Xms64M -Xmx512M -jar /home/bulsatar/apophysis-j-28/apophysis-j-28.jar

HTH,
Karl

</example applied to your programme apophysis-j-28>
Image
Enlightened by Lucid Lynx, enchanted by Maya, impressed by Petra.
User avatar
karlchen
Level 9
Level 9
 
Posts: 2520
Joined: Sat Dec 31, 2011 7:21 am

Re: Execute Jar from Bash Script

Postby bulsatar on Sat Nov 02, 2013 10:03 am

Tried your example Karl and go the same results. The single java -jar line was the first way I tried it before running all the other ways.

When I ran the bash script from the cli, this is what I got, an actual error :D

bash: /home/bulsatar/apophysis-j-28/apophysis.sh: /bin/bash^M: bad interpreter: No such file or directory

Edit~~ when I copy and paste the line in the bash to the cli, it runs perfectly. There are only 2 lines of code...will I need to reinstall bash? is that even possible???
bulsatar
Level 1
Level 1
 
Posts: 46
Joined: Sun May 26, 2013 1:05 am

Re: Execute Jar from Bash Script

Postby bulsatar on Sat Nov 02, 2013 10:50 am

Saw the extra characters after the bash identifier ^M. Searched and found that sometimes editors put characters in? I made a new document, copied everything over directly and it worked fine.

Weird...

Thanks for everyone's help!
bulsatar
Level 1
Level 1
 
Posts: 46
Joined: Sun May 26, 2013 1:05 am

Re: Execute Jar from Bash Script

Postby karlchen on Sat Nov 02, 2013 10:53 am

Hello, Bulsatar.

The ^M in the error message identifies the root problem:
For whichever reason you have created the bash script file with an editor that writes its file in Windows notation. This means it codes linebreaks like on a typing machine as carriage return plus linefeed (CR LF). Linux, however, expects to only get linefeed (LF) at the end of each line. Else bash will react in a very confused way and complain about random things.

Solution:
Use a (Linux) editor to convert the script file into Linux text format. Then run it again.

Note:
My (Windows) editor notepad2 will display in the status line "CR+LF" (Windows format), "CR" (Mac format) or "LF" (Linux format). Clicking on this flag will switch through the 3 different line ending modes.
In case you have not got an editor which can convert the file, then create a new script using gedit, pluma, nano, vi oder whichever maybe your favourite Linux editor.

Kind regards,
Karl
--
P.S.:
Just realized that our two posts crossed each other and that you have already found out about the line end pitfall CR+LF vs. LF.
Great.
Image
Enlightened by Lucid Lynx, enchanted by Maya, impressed by Petra.
User avatar
karlchen
Level 9
Level 9
 
Posts: 2520
Joined: Sat Dec 31, 2011 7:21 am

Linux Mint is funded by ads and donations.
 

Return to Scripts & Bash

Who is online

Users browsing this forum: No registered users and 3 guests