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? |
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). |
Code:
java -jar yourapp.jar You just need to have a manifest file in your jar for this to work. |
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 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% |
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 Code:
#!/bin/bash Then you create the bin like that: Code:
$ cat myScript.sh FILE.EXT >fileToDistribute.bin |
All times are GMT -5. The time now is 10:03 PM. |