If you write an applet or use a native code compiler then you get an executable file that can be launched by the user by mouse clicking on some icon. However, if you, as some times happens, may have one reson or another to avoid a native compiler for a application then users normally will have to start java.exe or javaw.exe on the commend-line. (I assume the use of the Windows platform)
There is a number of problems with this:
* You must know where java.exe/javaw.exe is located or require that that path is in the system path by changing the autoexec.bat file of the user.
* You start program via a command-line or bat file. This pops up a prompt box momentarily and seems unprofessional.
* The user is not used to starting bat files. They want exe files that they know how to use.
In order to handle these problems I use a few hours last night to write a simple C++ program that reads a class name from a file called launcher.cfg and then starts a Java VM with that class as the main class. You then put in the name of your class in the launcher.cfg file and then click the exe (or a shortcut to it) and the application starts - the only limitation is that you get no console window so it is like using javaw.exe
I have written the code below in full length in case any of you might be interested it having it because it is a nice little handy application.
To compile it you need to compile the code below and link it with jvm.lib included with your java SDK. It requires the Java 2 platform. Furthermore, the directory with jni.h must be in your include path and this is also part of the Java SDK.
If you don't have a C++ compiler or don't know how to use it then mail me and I will send you a binary version (it is only 44KB large) mail to email@example.com
Instructions for use:
Make a file called launcher.cfg in the same directory as launcher.exe. In the first line put the name of the directory where your use classes are. This may be a directory relative to the working directory. If this is the same then just write . (dot) for current directory. Personally I like to put my classes in a subdirectory java, and in that case I write java here. The path given here may not contain spaces.
On the next line or after a space write the full name of the main class. You may delimit packages with either "/" or ".".
Then rename launcher.exe to a name of your own choice. And finally make a shortcut to it where you add your own icon. If you compile the source yourself you can also add the icon to the exe file as a resource.
Note the this launcher application is nothing fancy. It does not support sending standard output to some seperate output window. I personally don't use standard output in my windowed Java applications (anyway only during debugging where launcher.exe is not used) so I didn't bother adding it.
Also note, that if the launcher has problems (e.g. no launcher.cfg file is present or the Java VM cannot be started) it will often suggest to reinstall the application. This is intended to be for the end-user (not you) because the application that uses luancher.exe installation probably is currupted.
#include < windows.h >
#include < windowsx.h >
#include < jni.h >
#include < direct.h >
int APIENTRY WinMain(HINSTANCE hInstance,
// Open config file
FILE* file = fopen("launcher.cfg","rt");
MessageBox(NULL,"Could not find file launcher.cfg. Please\nreinstall the application.","Error loading file",MB_OK);
// Read new directory from config file
int result = _chdir(buffer);
MessageBox(NULL,"Could not find the directory given in launcher.cfg\n" "Please reinstall the application.","Error changing directory",MB_OK);
// Read startup class string from config file
char* p = buffer;
while(*p != NULL)
*p = '/';
// Initialize JavaVM
initArgs.version = JNI_VERSION_1_2; // Find a Java 1.2 VM or newer.
char optionstart = "-Djava.class.path=";
options.optionString = optionfull;
initArgs.options = options;
initArgs.nOptions = 1;
initArgs.ignoreUnrecognized = TRUE;
int res = JNI_CreateJavaVM(&jvm, (void**) &env, &initArgs);
MessageBox(NULL,"Could not start Java. This is most likely because you do not have\n" "a Java 2 compliant virtual machine on your computer. Please download\n" "and install the newest JRE from http://java.sun.com","Error starting Java",MB_OK);
// Call static main function in the loaded class name
jclass launcherClass = env->FindClass(buffer);
MessageBox(NULL,"Could not find the class given in launcher.cfg.\nPlease reinstall the application.","Error starting application",MB_OK);
jmethodID main = env->GetStaticMethodID(launcherClass,"main","([Ljava/lang/StringV");
MessageBox(NULL,"The class given in launcher.cfg does not have a main class.\n" "Please reinstall the application.","Error starting application",MB_OK);
sample launcher.cfg file:
(Arghh - this news thing messes up my code. If you want the code in its full form please mail me and I will send it to you)
Edited by - felonius on February 26, 2001 7:38:13 AM