Java Forum / General / November 2005
Writing a Windows JVM launcher program
Paul J. Lucas - 16 Nov 2005 23:31 GMT I want to launch a JVM from a tiny Windows launcher application. I have a JDK installed in C:\j2sdk1.4.2_09. I'm compiling/linking by doing: g++ -mno-cygwin -L/cygdrive/c/j2sdk1.4.2_09/lib -o JavaAppLauncher *.o -ljvm
and that links. When I run it, it complains that it can't find jvm.dll. How do I specify the location of that? I *do* have JAVA_HOME set.
- Paul
Roedy Green - 17 Nov 2005 00:49 GMT On Wed, 16 Nov 2005 23:31:47 GMT, pauljlucas.removethis@removethistoo.mac.com (Paul J. Lucas) wrote, quoted or indirectly quoted someone who said :
>g++ -mno-cygwin -L/cygdrive/c/j2sdk1.4.2_09/lib -o JavaAppLauncher *.o -ljvm let's see the C code.
you need to launch c:\j2sdk1.4.2_09/bin/java.exe
You may have remnants of another JRE/JDK installed confusing things.
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
Paul J. Lucas - 17 Nov 2005 20:00 GMT I'm using the JNI interface, i.e., I'm calling JNI_CreareJavaVM(). I'm not calling the java.exe.
- Paul
Roedy Green - 17 Nov 2005 20:50 GMT >I'm using the JNI interface, i.e., I'm calling JNI_CreareJavaVM(). I'm >not calling the java.exe. In windows, I believe requires the registry to be pointing to the correct JVM and for the appropriate Java.exe to be first on the path somewhere and for an appropriate SET classpath.
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
Thomas Kellerer - 17 Nov 2005 22:21 GMT Roedy Green wrote on 17.11.2005 21:50:
> In windows, I believe requires the registry to be pointing to the > correct JVM and for the appropriate Java.exe to be first on the path > somewhere and for an appropriate SET classpath. The registry is only required to find the JVM (i.e. the JDK install directory) once you have that, the registry is not needed any longer.
I had no troubles using the source for java.exe as a blueprint for my own launcher.
Thomas
Chris Uppal - 17 Nov 2005 08:08 GMT > I want to launch a JVM from a tiny Windows launcher > application. I have a JDK installed in C:\j2sdk1.4.2_09. I'm [quoted text clipped - 5 lines] > and that links. When I run it, it complains that it can't find > jvm.dll. How do I specify the location of that? One of the following:
0) Run your program from the same directory as the DLL -- not a long-term solution ;-)
1) Put the DLL is on your %Path% -- NOT recommended.
2) Hardwire the location of the DLL. Not ideal, but better than (0) or (1), and probably OK if your application includes a private JRE installation.
3) Use an application-specific configuration file or similar. Not ideal but better than (2).
4) Look in the registry. The keys under: HKLM\SOFTWARE\JavaSoft\Java Runtime Environment\ are probably self-explanatory, especially if you have more than one JDK or JRE installed. Note that other vendors' implementations follow irritatingly different patterns of registry usage.
I use (4), with optional override by (3), myself.
You shouldn't have to mess around with -L flag, since the DLL loading is best handled dynamically, so the stub lib is valueless (never used it myself, but afaik, it just hardwires loading the DLL from the %Path%).
-- chris
Paul J. Lucas - 17 Nov 2005 20:05 GMT I'm including a private JRE installation. So appending
:jre/bin/client to PATH works. However, it works only if I started the executable from the directory it's in. If I double-click the icon, it can't find the dll. I assume this is because Windows doesn't "cd" to the directory where the executable is prior to launching it.
So how do I get the directory of the current executable so I can do a chdir() there?
- Paul
Chris Uppal - 18 Nov 2005 10:12 GMT > So how do I get the directory of the current executable > so I can do a chdir() there? I see I've mislead you. Sorry. I meant that you should (IMO) use explicit DLL loading rather than relying on the primitive and inflexible (%Path% dependent) stuff which is hard-coded into the stub lib. It's only a matter of a couple of extra lines of code (well, maybe half a dozen) and insulates you from all sorts of hidden nastiness. Respect the D in DLL and it'll be your friend ;-)
Anyway, here's some example code that I posted a couple of weeks ago. It lacks error checking, but should provide useful pointers (or do I mean references? ;-)
I'm not sure what the mingw equivalent of the first two lines would be, but since dynamically loading DLLs is /the/ central operation of any Windows program, there must be an equivalent. With luck the code'll work as-is. I repeat that you shouldn't need any special (to JNI) -l or -L flags for this to work -- you are using DLLs not static libs, so the linker has nothing to do.
-- chris
======================= #define WIN32_LEAN_AND_MEAN #include <windows.h>
#include <jni.h>
typedef __declspec(dllimport) jint (__stdcall * CreateJavaVMFunc)( JavaVM**, void**, void*);
// called with the full path to the JVM dll as its only parameter int main(int argc, char** argv) { // get lib entrypoint HINSTANCE lib = LoadLibrary(argv[1]); CreateJavaVMFunc createJavaVM = (CreateJavaVMFunc)GetProcAddress( lib, "JNI_CreateJavaVM");
// start JVM JavaVMInitArgs initArgs; JavaVMOption options[2] = { { "-verbose:jni" } , { "-Xcheck:jni" } }; initArgs.version = JNI_VERSION_1_2; initArgs.nOptions = 0; initArgs.options = options; initArgs.ignoreUnrecognized = true; JNIEnv *jniEnv; JavaVM *javaVM; createJavaVM(&javaVM, (void**)&jniEnv, &initArgs);
........ =======================
Manfred Rosenboom - 17 Nov 2005 08:38 GMT If you have installed the JDK 1.4.2 with the Sources (file src.zip), you will find the source code of the java.exe in the directory launcher of the file src.zip.
Best, Manfred
Luke Webber - 18 Nov 2005 05:54 GMT > I want to launch a JVM from a tiny Windows launcher > application. I have a JDK installed in C:\j2sdk1.4.2_09. I'm [quoted text clipped - 5 lines] > jvm.dll. How do I specify the location of that? I *do* have > JAVA_HOME set. Nobody has asked the most important question yet, and that is "Why?". Why do you need the launcher program? Have you checked out JSmooth? It creates a Windows .exe from a .jar file...
http://jsmooth.sourceforge.net/
Cheers, Luke
Paul J. Lucas - 20 Nov 2005 21:26 GMT > Nobody has asked the most important question yet, and that is "Why?". > Why do you need the launcher program? Because none of the ready-made ones, AFAIK, allow you do something like: when launching, set the value of the -Xmx parameter to a percentage of the amount of physical memory in the machine.
- Paul
Andrew Thompson - 21 Nov 2005 01:20 GMT >>Nobody has asked the most important question yet, and that is "Why?". >>Why do you need the launcher program? Excellent question. Far too many people here seem to get caught up in the 'interestingness of an absraction/question' rather than asking if the question 'makes any sense'.
> Because none of the ready-made ones, AFAIK, allow you do > something like: when launching, set the value of the -Xmx > parameter to a percentage of the amount of physical memory in > the machine. What use is that? Are you saying you can get your application to work on '40% of the memory available on a 64 Meg machine'?
Better, you could use a small Java program that asks the *user* how much RAM they want to dedicate to the application and call an actively generated JNLP specifying that amount.
(..and have we discussed that indent you use?)
 Signature Andrew Thompson physci, javasaver, 1point1c, lensescapes - athompson.info/andrew Currently accepting short and long term contracts - on Earth.
Paul J. Lucas - 21 Nov 2005 04:28 GMT > What use is that? Are you saying you can get your application > to work on '40% of the memory available on a 64 Meg machine'? No. The launcher also insists on a minimum.
> Better, you could use a small Java program that asks the *user* how > much RAM they want to dedicate to the application ... A Java program does do that very thing. In fact, that Java program is the application itself in a Preference dialog. If the user changes that preference, it takes effect on the next application launch.
Of course in order for it to take effect, it has to be specified to the JVM. By the time the Java program is running, it's too late to change it. Hence, the custom launcher reads the stored preference and supplies the proper -Xmx option.
> (..and have we discussed that indent you use?) If your newsreader can't handle it, your newsreader is broken. Seriously. Use a better one. My newsreader shows text verbatim (and tabs are indented 1 tab-stop as any reasonable newsreader would do).
- Paul
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|