Jason S
Jason S

Reputation: 189626

Java command-line launchers

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

Answers (8)

Guillaume
Guillaume

Reputation: 18865

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

Upvotes: 1

Christoph
Christoph

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

Joachim Sauer
Joachim Sauer

Reputation: 308001

  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.

Upvotes: 5

Jason Day
Jason Day

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

Frank Krueger
Frank Krueger

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

Charlie Martin
Charlie Martin

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

OscarRyz
OscarRyz

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

Elie
Elie

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

Related Questions