LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Java Shell / Batch Scripts (https://www.linuxquestions.org/questions/programming-9/java-shell-batch-scripts-256356/)

GenericProdigy 11-18-2004 05:58 AM

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?

nhs 11-18-2004 06:23 AM

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).

jlliagre 11-18-2004 06:28 AM

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.

darkRoom 11-18-2004 08:33 AM

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.


theYinYeti 11-18-2004 09:00 AM

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.


All times are GMT -5. The time now is 10:03 PM.