by Andrei Cioroianu

Java Tip 58: Fast launcher for Java apps

how-to
Aug 1, 19987 mins

Use a simple double-click of the mouse to launch Java apps from the Windows desktop

A previous Java Tip, “Tip 45: Launch Java applications quickly from Windows 95,” demonstrated the creation of DOS shortcuts for Java applications. But why not use simple double-clicks of the mouse to launch Java apps from the Windows desktop? This article shows you how to use a small C application to run Java applications as easily as you run your native apps.

Java interpreters don’t accept DOS paths as parameters, so you can’t use the standard Windows double-click mechanism. To obtain the name of the Java class, the DOS path of a class file must be split, the directory added to the CLASSPATH, and the “.class” extension eliminated from the file’s name.

I wrote this article with Windows users in mind — particularly those who hate to type long commands in a DOS console. To install my C application, just follow the steps described in the proceeding paragraphs. I know you prefer Java to C, so I did the hard work for you; I wrote the app. All you have to do is use it!

Installing JavaLauncher

I don’t like Windows programming, so I created a simple console application that you must install manually. Here are the steps:

  • Unzip JavaLauncher.zip (see Resources) in a folder. You might call it “C:JL”, for example.

  • Double-click on a class file (let’s call it your_app.class) from Windows Explorer. This may be any of your class files, but it’s preferable that it be an application that doesn’t need parameters. (In other words, it has a main() method that doesn’t use args[].)

  • Windows will ask you what application should be used to “open” the class file, as shown in Figure 1 below.
  • If everything is okay, go on to the next step. If there’s a problem, it’s probably because you tried, in a previous programming effort, to set another “viewer” for class files. If this is so, you must select the Options item of the View menu in the Windows Explorer GUI. Click on the File Types tab and select the item associated with the class files (see Figure 2). You must edit the properties of this item as shown in the “Using drag-and-drop and menu shortcuts” section below.

    Figure 2
  • Type a description like “Java Class File” in the text field of the window, then press the Other button. Another window is shown, as depicted in Figure 3.

  • You must select JavaLauncher.exe from the directory in which you’ve just exploded JavaLauncher.zip. Press the Open button of the window shown in Figure 3.

  • You will return to the window shown in Figure 1. Press the OK button (which should be enabled now). JavaLauncher will try to execute your_app.class using java.exe, so don’t choose format_my_hard_disk.class. On my computer, your_class was the HelloUniverse.class that comes with Java 3D (see Figure 4). This was the first application, which I launched with a double-click of the mouse.

Note: Next time you want to launch a Java app, you need only double-click on the class file.

The directory of java.exe must be in the PATH environment variable. Also the CLASSPATH must be set. See the “Using drag-and-drop and menu shortcuts” section below to see the configuration in which I tested JavaLauncher.

You needn’t be a Windows hacker to follow these steps: it’s easier to do than to describe.

What does JavaLauncher actually do?

JavaLauncher takes the parameters from the command line, and forms a DOS command. If the only parameter is the class file, JavaLauncher uses the java.exe interpreter. However, you can start the parameter list with the name of your favorite interpreter (java, jre, jview), followed by its own parameters. If -classpath isn’t present, a default value is computed, which is formed from the CLASSPATH environment variable (this must be set) and the directory of the class file. The last parameter should be “%1”. Windows will replace “%1” with the name of the selected class file.

You cannot use JavaLauncher to pass parameters to a Java application that needs them. Such apps cannot be launched with a double mouse-click. Use a DOS console for them and first run the DosKey command, which Windows inherited from DOS 5.0. This way you won’t need to type the same command twice (you’ll use the arrow keys to browse the command history).

Using drag-and-drop and menu shortcuts

After you’ve manually registered the JavaLauncher application, you can create shortcuts for your Java apps as easily as you do so for Microsoft Word documents, by using drag and drop.

You can define a few items in the shortcut menu of the class files, one for each JDK version you have:

  1. Select the Options item of the View menu in Windows Explorer. You will see a dialog box.

  2. Click on the File Types tab and select the item associated with the class files (see Figure 3).

  3. Press the Edit button. You will see a new window like that depicted in Figure 5.

    Figure 5
  4. Press the New button. You will see a third dialog box similar to the one shown in Figure 6.

  5. Type a label (for example, “JDK11”) in the first text field.

  6. Type a DOS command in the second text field. It should start with the path of JavaLauncher.exe (for example, C:JLJavaLauncher.exe), followed by the path of the Java interpreter (for example, E:JDK1.1binjava.exe), followed by -classpath, followed by the content of the CLASSPATH environment variable (for example, .;E:JDK1.1libclasses.zip), followed by “%1” (with the quotes).

  7. Press the OK button of the window shown in Figure 6. You will return to the window shown in Figure 5. A new item should appear in the Actions list (for example, JDK11). (If you want to modify the properties of an item — meaning the label and the command — you must select that item from the Actions list and push the Edit button.)

  8. Press the Close button of the window shown in Figure 5 above. You will return to the window shown in Figure 2.

  9. Press the Close button of the window from Figure 2. You will return to Windows Explorer.

Repeat these steps for each JDK version you have. You may include the steps between step 4 (“Press the New button”) and step 7 (“Press the OK button”) in a loop.

Now, with a right-click of the mouse on a class file, you’ll activate a shortcut menu (see Figure 7), from which you may launch your Java apps with any of the available Java versions.

On my computer, I’ve installed JDK 1.0.2, 1.1.5, and 1.2 Beta 3. These are the commands of the items of the menu shortcut:

  • C:JLJavaLauncher.exe E:JDK1.0binjava.exe -classpath.;E:JDK1.0libclasses.zip “%1”

  • C:JLJavaLauncher.exe E:JDK1.1binjava.exe -classpath.;E:JDK1.1libclasses.zip “%1”

  • C:JLJavaLauncher.exeE:JDK1.2binjava.exe-classpath.;E:JDK1.2libclasses.zip; E:Java3Dlibappextj3dutils.jar;E:Java3Dlibsysextj3dcore.jar;E:Java3Dlibsysextvecmath.jar; E:Java3Dlibsysextj3daudio.jar “%1”

Conclusion

JavaLauncher is easy to use and configure, saves you time, and helps you test your applications with multiple JDK versions. JavaLauncher is a wonderful tool to make launching Java applications from the Microsoft Windows desktop as easy as launching any other Windows application.

Andrei Cioroianu holds a B.S. in mathematics-computer science and an M.S. in artificial intelligence. His focus is on 3D graphics (Java 3D), software components (JavaBeans), and user interface (AWT, JFC). You can visit his (a) Java Developer’s Page.