LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-10-2006, 03:37 PM   #1
xlinuks
Member
 
Registered: Jan 2006
Location: White House
Distribution: Ubuntu 8.10
Posts: 39

Rep: Reputation: 15
Run java from shellscript


Hello,
I have a little Java program that is supposed to be run with a custom amount of memory, to do this I need to create a shell script (I've created a .bat script for windows with no problem). BUT, under Linux my script runs correctly only from the command line - I need to run it at double click as well. The script is supposed to launch a .jar (Java executable) file that's in the same directory. Here's my script (called "start_java"):
Code:
#! /bin/sh
#cd $PWD
java -Xms64m -Xmx256m -jar ./MyApplication.jar
It runs successfully from the command line like "./start_java" & Enter.
At double click it wont launch - I think it's about specifying the directory where Java should find the "MyApplication.jar" - here is my problem since I don't know how to do it. From the command line we say "./" to specify the current dir, but using the double click method - the application (java) thinks probably that the current folder is the directory where it is installed.
Hope you got me correctly (the CD $PWD didn't work either).
Please help!
 
Old 12-10-2006, 04:35 PM   #2
Brian1
LQ Guru
 
Registered: Jan 2003
Location: Seymour, Indiana
Distribution: Distribution: RHEL 5 with Pieces of this and that. Kernel 2.6.23.1, KDE 3.5.8 and KDE 4.0 beta, Plu
Posts: 5,700

Rep: Reputation: 65
First there is no space in the first line. It is one single word as one might say.
#!/bin/bash

The cd $PWD is actually what? You need to define the string variable $PWD

Brian
 
Old 12-10-2006, 04:59 PM   #3
xlinuks
Member
 
Registered: Jan 2006
Location: White House
Distribution: Ubuntu 8.10
Posts: 39

Original Poster
Rep: Reputation: 15
Thanks for the hint. I realized whats the matter: when using double click the "PWD" variable points to my home dir, instead of the directory from where the shell script has been launched - and I have no clue how to fix this.
Code:
#!/bin/bash
nowdir=$PWD
echo $nowdir >> /home/brian/Desktop/thefile.txt
java -Xms64m -Xmx256m -jar $nowdir/MyApplication.jar
-from the command line I get a file (thefile.txt) on the desktop containing "/home/brian/Desktop"
-by double clicking the shellscript (from the Desktop as well) I get same file with only "/home/brian"
So, my problem is: How to let the shellscript correctly identify the folder it has been launched from?

Last edited by xlinuks; 12-10-2006 at 05:00 PM.
 
Old 12-10-2006, 05:07 PM   #4
Brian1
LQ Guru
 
Registered: Jan 2003
Location: Seymour, Indiana
Distribution: Distribution: RHEL 5 with Pieces of this and that. Kernel 2.6.23.1, KDE 3.5.8 and KDE 4.0 beta, Plu
Posts: 5,700

Rep: Reputation: 65
Is the same file being overwritten?
First run it like you say, then delete the thefile.txt and run it again where you want the second time. If it is correct then the second attempt is not forcing and overwrite.

Brian
 
Old 12-10-2006, 06:38 PM   #5
xlinuks
Member
 
Registered: Jan 2006
Location: White House
Distribution: Ubuntu 8.10
Posts: 39

Original Poster
Rep: Reputation: 15
I create the "thefile.txt" from the shell script just to prove that the shellscript behaves differently when it is being launched by double-click or from the command line - both cases have different understanding about the 'current directory' - which is why I can't run the "MyApplication.jar" file double-clicking the launcher ( the "start_java" file ).
Thanks for your time.

Last edited by xlinuks; 12-10-2006 at 06:39 PM.
 
Old 12-10-2006, 07:46 PM   #6
Electro
LQ Guru
 
Registered: Jan 2002
Posts: 6,042

Rep: Reputation: Disabled
I have the same problem, but I do not rely on desktop icons. State the the PATH environment variable or use the full path to the program.

The reason why the script is behaving strangly. All programs that are loaded gets their own seperate environment because Linux is a multi-tasking and a virtual memory OS. To get the script run in the same environment, you type ". [script name]". The dot says in this environment, run this script.

In your script, I suggest adding . on the same line that reads java before java. This will make it easier for Linux to run the program in the same environment as the script is run.
 
Old 12-11-2006, 02:53 PM   #7
xlinuks
Member
 
Registered: Jan 2006
Location: White House
Distribution: Ubuntu 8.10
Posts: 39

Original Poster
Rep: Reputation: 15
Putting the point before 'java' didn't help either
 
Old 12-11-2006, 03:06 PM   #8
Brian1
LQ Guru
 
Registered: Jan 2003
Location: Seymour, Indiana
Distribution: Distribution: RHEL 5 with Pieces of this and that. Kernel 2.6.23.1, KDE 3.5.8 and KDE 4.0 beta, Plu
Posts: 5,700

Rep: Reputation: 65
Not a very good script writer so out of ideas.

Brian
 
Old 12-11-2006, 03:50 PM   #9
Electro
LQ Guru
 
Registered: Jan 2002
Posts: 6,042

Rep: Reputation: Disabled
Assuming MyApplication.jar is in $HOME/bin/javaprog
Code:
#!/bin/sh
#PATH="/bin:/sbin:/usr/bin:/usr/sbin"
PROGRAM_PATH="$HOME/bin/javaprog"
cd "$PROGRAM_PATH"
java -Xms64m -Xmx256m -jar MyApplication.jar
unset PROGRAM_PATH
Please read my previous post thoroughly.
 
Old 12-12-2006, 02:36 AM   #10
xlinuks
Member
 
Registered: Jan 2006
Location: White House
Distribution: Ubuntu 8.10
Posts: 39

Original Poster
Rep: Reputation: 15
That's the problem,that there's no way to know where the end user might unzip the application.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Help Shellscript paraiso Linux - Newbie 7 05-12-2005 07:25 AM
Lockscreen Shellscript 2 paraiso Linux - General 1 05-11-2005 05:30 PM
shellScript help paraiso Linux - Newbie 2 05-10-2005 07:09 PM
ShellScript sharadgana Programming 1 12-16-2004 01:16 PM
Java applets run fine in Konqueror, but won't run in MS IE. OAnimal Linux - Software 7 12-04-2002 06:32 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:52 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration