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 jacob@marner.dk
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.
Source:
#define WIN32_LEAN_AND_MEAN
#include < windows.h >
#include < windowsx.h >
#include < jni.h >
#include < direct.h >
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// Open config file
FILE* file = fopen("launcher.cfg","rt");
if (file==NULL)
{
MessageBox(NULL,"Could not find file launcher.cfg. Please\nreinstall the application.","Error loading file",MB_OK);
return 1;
}
// Read new directory from config file
char buffer[1000];
fscanf(file,"%s",buffer);
int result = _chdir(buffer);
if (result!=0)
{
MessageBox(NULL,"Could not find the directory given in launcher.cfg\n" "Please reinstall the application.","Error changing directory",MB_OK);
return 5;
}
// Read startup class string from config file
fscanf(file,"%s",buffer);
char* p = buffer;
while(*p != NULL)
{
if (*p=='.')
*p = '/';
p++;
}
// Initialize JavaVM
JavaVM *jvm;
JNIEnv* env;
JavaVMInitArgs initArgs;
initArgs.version = JNI_VERSION_1_2; // Find a Java 1.2 VM or newer.
JavaVMOption options[1];
char optionstart[] = "-Djava.class.path=";
char optionend[1000];
_getcwd(optionend,1000);
char optionfull[1000];
strcpy(optionfull,optionstart);
strcat(optionfull,optionend);
options[0].optionString = optionfull;
initArgs.options = options;
initArgs.nOptions = 1;
initArgs.ignoreUnrecognized = TRUE;
int res = JNI_CreateJavaVM(&jvm, (void**) &env, &initArgs);
if (res!=0)
{
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);
return 2;
}
// Call static main function in the loaded class name
jclass launcherClass = env->FindClass(buffer);
if (launcherClass==NULL)
{
MessageBox(NULL,"Could not find the class given in launcher.cfg.\nPlease reinstall the application.","Error starting application",MB_OK);
return 3;
}
jmethodID main = env->GetStaticMethodID(launcherClass,"main","([Ljava/lang/StringV");
if (main==NULL)
{
MessageBox(NULL,"The class given in launcher.cfg does not have a main class.\n" "Please reinstall the application.","Error starting application",MB_OK);
return 4;
}
env->CallStaticVoidMethod(launcherClass,main);
return 0;
}
sample launcher.cfg file:
java
dk.rolemaker.RoleMaker
enjoy,
Jacob Marner
jacob@marner.dk
www.rolemaker.dk
(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