LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-18-2004, 05:58 AM   #1
GenericProdigy
LQ Newbie
 
Registered: Aug 2004
Location: London, UK
Distribution: SuSE Linux 9.0, Opetec Linux 1.0
Posts: 9

Rep: Reputation: 0
Java Shell / Batch Scripts


I'm looking to distribute applications in Java. The issue I have is that Java Archives (JARs) won't run normally like native applications and I've worked with enough users to understand that a good script (or EXE, preferably C++) is required.

What I would like to do on both Windows and Linux (primarily Linux right now) is to have a script file that I can distribute with my applications that will work on any system with Java on. When the script is executed it will run my Java applications. Any hints and tips on how to do this?
 
Old 11-18-2004, 06:23 AM   #2
nhs
Member
 
Registered: Aug 2003
Location: Edinburgh, Scotland
Distribution: Gentoo
Posts: 246

Rep: Reputation: 30
The simplest solution would be

#!/bin/bash
java -jar <filename.jar>

however this assumes that Java is installed and in the path you could use

java -jar <filename.jar> || echo "Error running JAR file. Is JAVA installed and in your PATH?"

More elaborate versions might use a case "$?" statement to differentiate error codes, etc. e.g.

#!/bin/bash
java -jar <filename.jar>
case "$?" in
0)
exit
;;
127)
echo "Java interpreter not found"
;;
*)
echo "Unknown error"
;;
esac

All of these scripts assume that bash is installed in /bin (although I think that is a fairly safe assumption these days). Adding cases to the case statement for other error codes the java interpreter produces would not be a bad thing either (I don't work with java much).
 
Old 11-18-2004, 06:28 AM   #3
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,493

Rep: Reputation: 355Reputation: 355Reputation: 355Reputation: 355
Code:
java -jar yourapp.jar
should work on both unix and windows.
You just need to have a manifest file in your jar for this to work.
 
Old 11-18-2004, 08:33 AM   #4
darkRoom
Member
 
Registered: Mar 2004
Location: Valencia, espaņa
Distribution: Slack, Gentoo, Custom
Posts: 162

Rep: Reputation: 30
You can use netbeans to make jar files, quickly and simply. As jiliagre said you'll need a manifest file and you'll have to write it yourself. Fortunately it need only be 2 or 3 lines minimum, most importantly you need to specify the main class of your program.

Code:
Main-Class: classname
You can additionally add security by sealing packages in a jar so all packages defined will have to be part of the jar.

Its one of the things i really hate about java (although its more the fault of m'soft). You make a jar and a windows user has to install runtime, then they have to set their enviroment variables, then they have to register the jar extension. Only then can they double click a jar to execute it. I guess by this time the average windows user is lost . . .

Good java installers do exist but they aren't free. Maybe when java is (more) popular it'll be easier to deploy. I hope.

And a windows batch script, save *.bat

Code:
PATH=.;C:\JDK1.4\bin;%PATH%
SET CLASSPATH=.
//compile probably not required
javac myProg.java
java myProg
Course unless you can make a check to see what java the user has installed theres no point in setting their path, if you can check their system and append the information into a batch file it could get them out of having to set their enviroment variables - but i dunno if this is possible with a batch file.


Last edited by darkRoom; 11-18-2004 at 08:47 AM.
 
Old 11-18-2004, 09:00 AM   #5
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 61
For Unix/Linux, you could distribute a .bin -like file. Basically, it works like that:
You have a file you want to somehow install. Let's say the file you need to install is called "FILE.EXT" and is 123456 bytes in size, and the application needed for installing it is called "Installer".

You create a shell script myScript.sh like that:
Code:
#!/bin/bash
tail --bytes=123456 "$0" >/tmp/FILE.EXT
Installer /tmp/FILE.EXT
exit 0
or, if your Installer does not rely on the file extension:
Code:
#!/bin/bash
Installer <(tail --bytes=123456 "$0")
exit 0
thus avoiding the temporary file.
Then you create the bin like that:
Code:
$ cat myScript.sh FILE.EXT >fileToDistribute.bin
$ chmod +x fileToDistribute.bin
Yves.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Running shell scripts from Java cmpich Programming 1 04-19-2005 04:36 AM
running shell scripts using Java niravk8 Programming 2 02-09-2005 09:42 AM
Java with Shell Scripts lazkyr Programming 1 07-30-2004 04:30 PM
shell scripts and installing java.. scoob8000 Linux - Software 2 07-12-2003 11:13 AM
Writing Scripts or Batch files? Nicksan Linux - Software 1 06-29-2003 11:49 PM


All times are GMT -5. The time now is 09:03 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration