• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

# [java] A Java application launcher

## 6 posts in this topic

#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)
{
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
0

##### Share on other sites
Under windows, you can get the same effect with the JRE and executable jar files. Just double-click on the executable jar, and javaw is automatically run. It is the equivalent of:

javaw -jar jarName

Of course, if there are multiple executable classes in the jar file, that isn''t so convenient anymore. But it still works as a simple alternative to using an external executable, given that you can put a shortcut to a jar file in any place users would expect a shortcut to any other program.
0

##### Share on other sites
Yup, that is an alternative too, and I also considered it myself (sorry I didn''t mention it above).

However, it didn''t want to use it because users expect to see exe files when starting an application and expect there to be an icon associated with it. Embedding icons in exe files are possible - that is not possible with JAR files.

And then of course, if you don''t use JARs or you have multiple entry points to the JAR, or you have multiple JARs in the same application then then the JAR-exe scheme will not work. And if the user has some alternative implementation of a virtual machine installed then it will not work either.

Furthermore, by editing the code above you are able to add additional options such as extra class paths or system properties and the like.

Anyway, thats for pointing this out c_wraith.

Cheers,
Jacob
0

##### Share on other sites
No prob.. I hinted at the limitations you mentioned, but I didn''t list them explicitly, as perhaps I should have.
0

##### Share on other sites
I have realized a problem with my code:

It only worked because my JRE was in my system path. After removing it, it works no more. To handle this I have extended my app so you can select the directory of the JRE in launcher.cfg.
This means that you need to know where that is. Just be sure it is in the right place I recommend having JRE in a subdirectory in your game directory and then distribute it with your game. Even after uninstalling the JRE from my computer just having that JRE directory in place worked fine. This will also make sure that a JRE that is compatible with your game is used - ie. if the user has multiple JRE versions installed you get the one you want to get not some other implementation.

The drawback is that you will have to install the JRE files with your game (15MB extra) even if the user as one installed already. The JAR solution c_wraith suggested does not have this drawback.

As little extra the new version does not require you to link to jvm.lib when compiling.

If you want the new edited code (or binary) please mail to jacob@marner.dk I will give it away freely (public domain licence == no license... ).

Jacob
0

##### Share on other sites
i''m pretty new to all this java stuff with the environment variables and vm''s but...

if you want to make your bat files look like an exe can''t you just use ShellExecute() or system()? you can have an icon then, and the dos-window is gone

don''t know about the rest though, sorry if i missed the point
0

##### Share on other sites
Hmm, yeah, using ShellExecute() will go very far and may actually be preferable in most cases due to its simplicity. Good idea. I hadn''t thought of that. It proves the use of posting something in a forum - you get good ideas back.

However,
Using the Invocation API is the only way you can: (I may be wrong - correct me if someone knows better)
Be completely sure what VM you will start. If you just start java or javaw on the command line (after setting the path in bat file so it will be available) is no garantee as to what VM will be started. I once tried starting java.exe from one VM but actually another VM started (that I verified by looking a the "java.vm.version" property with System.getProperty())

Jacob
0