Reputation: 189626
I am working on a .jar file library to implement a bunch of helper classes to interface a PC to a piece of external hardware. I'll also add some simple applications, either command-line or GUI, to handle common tasks using the library.
My question is, is there a recommended way to simplify the command-line instantiation of a JVM in a certain specific way?
e.g. instead of requiring a user to type a cryptic error-prone command like:
java -cp TurboBlenderLib.jar -jar TurboBlenderApp.jar -DFoo=Bar arg1 arg2
instead I do
TurboBlenderApp arg1 arg2
?
I suppose I could use shell scripts (incl. win32 Batch Files... gacckkk), it's just that I'm not good at those + I was wondering if there was a more straightforward platform-independent way of running a Java app from the commandline.
Upvotes: 3
Views: 1943
Reputation: 18865
Another solution is found in the Jakarta Commons, as always : commons-launcher.
Upvotes: 1
Reputation: 169533
If you just want to simplify local execution, use a batch file or even just create a custom shortcut. If you want to create a launcher and package the executable jar and required libs for deployment, use something like Launch4j.
Upvotes: 2
Reputation: 308001
-jar
, then-cp
(and the CLASSPATH
variable) will be ignoredjava -jar TheApp <whateverargumentsyouwant>
" shouldn't be too hard (you can have a Class-Path attribute in your jar
files manifest, however).Upvotes: 5
Reputation: 8839
If you use java -jar
to launch your application, you can add additional jars to the classpath by adding a Class-Path
entry to the main jar's manifest. See the jar file spec for more information.
Upvotes: 1
Reputation: 70983
Shell scripts and batch files are the standard way of accomplishing what you want. (Look at any major Java product.)
This is, of course, absolutely pathetic and ridiculous. Go Java.
Your alternative is to write a little C program that does what you need (creates the java process) and compile that for each of your supported platforms. (C is the truly platform independent language).
Either way, you will need to take platform-dependent steps to make your app feel truly at home in the OS. On OS X, you will need to make a .app bundle, on Windows you need to package atleast the icon and version info into an EXE. For Linux, well, shell scripts suffice. :-)
Upvotes: 1
Reputation: 112356
For some reason this trick just doesn't get around. What you need to do is to create a custom manifest for your jar file that defines the Main-class:
property, and include in the jar file all the class files you're using. Then all you need is to run
$ java -jar myapp.jar
On real operating systems, you can even just run the jar file, as they will use the magic number to start it. But if you must, a one-liner batch or shell-script containing that line is all that's needed.
This is described in one of the Java Tutorials.
Upvotes: 1
Reputation: 199205
It's pretty easy:
Write this in TurboBlenderApp.cmd:
java -cp TurboBlenderLib.jar -jar TurboBlenderApp.jar -DFoo=Bar %1 %2
// Bear in mind saua answer about -cp and -jar
Then from the command line you just write:
TurboBlenderApp arg1 arg2
Just the way you wanted.
If in Unix/Linux replace th4e %1 %2 with $1 $2 and make sure your app has execute rights ( If you don't know how to do this, I guess you don't need it either )
Upvotes: 0
Reputation: 13843
A batch file would do the job. Put the exact command you want executed into a file myProg.bat and run it.
Upvotes: 0