views:

358

answers:

8

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.

A: 

A batch file would do the job. Put the exact command you want executed into a file myProg.bat and run it.

Elie
+5  A: 
  1. When you use -jar, then-cp (and the CLASSPATH variable) will be ignored
  2. Just provide a executable jar. "java -jar TheApp <whateverargumentsyouwant>" shouldn't be too hard (you can have a Class-Path attribute in your jar files manifest, however).
  3. if it is too hard write a GUI
  4. or provide those shell scripts/batch files. Writing those isn't too hard either.
Joachim Sauer
Hmm. Is there a way to tell the JVM: Hey, I want to run the .jar file of an application using this library in a second .jar? (so I can decouple the library implementation from the application)
Jason S
@Jason: answered by editing the answer.
Joachim Sauer
@Jason: Yes. java -cp myAppJar;myLibJar; TheApp arg1 arg2 TheApp is the class name. If you don't want to write something like com.org.som.package.etc.App you can create a class named TheApp in the default package.
OscarRyz
+1  A: 

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

Frank Krueger
A: 

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 )

OscarRyz
+2  A: 

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.

Christoph
Interesting: I will have to check launch4j out. Thanks!
Jason S
+1  A: 

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.

Charlie Martin
+1  A: 

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.

Jason Day
+1  A: 

Another solution is found in the Jakarta Commons, as always : commons-launcher.

Guillaume