Review your favorite Linux distribution.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 11-18-2004, 06:58 AM   #1
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, 07:23 AM   #2
Registered: Aug 2003
Location: Edinburgh, Scotland
Distribution: Gentoo
Posts: 246

Rep: Reputation: 30
The simplest solution would be

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.

java -jar <filename.jar>
case "$?" in
echo "Java interpreter not found"
echo "Unknown error"

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, 07:28 AM   #3
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Ubuntu/WSL
Posts: 9,785

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
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, 09:33 AM   #4
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.

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

//compile probably not required
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 09:47 AM.
Old 11-18-2004, 10:00 AM   #5
Senior Member
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 66
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 like that:
tail --bytes=123456 "$0" >/tmp/FILE.EXT
Installer /tmp/FILE.EXT
exit 0
or, if your Installer does not rely on the file extension:
Installer <(tail --bytes=123456 "$0")
exit 0
thus avoiding the temporary file.
Then you create the bin like that:
$ cat FILE.EXT >fileToDistribute.bin
$ chmod +x fileToDistribute.bin


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 05:36 AM
running shell scripts using Java niravk8 Programming 2 02-09-2005 10:42 AM
Java with Shell Scripts lazkyr Programming 1 07-30-2004 05:30 PM
shell scripts and installing java.. scoob8000 Linux - Software 2 07-12-2003 12:13 PM
Writing Scripts or Batch files? Nicksan Linux - Software 1 06-30-2003 12:49 AM > Forums > Non-*NIX Forums > Programming

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

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