Jump to content

  • Log In with Google      Sign In   
  • Create Account

[java] A Java application launcher


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 felonius   Members   -  Reputation: 122

Like
Likes
Like

Posted 26 February 2001 - 12:33 AM

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

Sponsor:

#2 c_wraith   Members   -  Reputation: 122

Like
Likes
Like

Posted 26 February 2001 - 04:17 AM

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.

#3 felonius   Members   -  Reputation: 122

Like
Likes
Like

Posted 26 February 2001 - 05:11 AM

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

#4 c_wraith   Members   -  Reputation: 122

Like
Likes
Like

Posted 26 February 2001 - 11:12 AM

No prob.. I hinted at the limitations you mentioned, but I didn''t list them explicitly, as perhaps I should have.

#5 felonius   Members   -  Reputation: 122

Like
Likes
Like

Posted 27 February 2001 - 01:02 AM

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

#6 kvh   Members   -  Reputation: 122

Like
Likes
Like

Posted 27 February 2001 - 03:40 AM

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

#7 felonius   Members   -  Reputation: 122

Like
Likes
Like

Posted 27 February 2001 - 04:22 AM

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





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS