Writing a Windows JVM launcher program

P

Paul J. Lucas

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
 
R

Roedy Green

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.
 
C

Chris Uppal

Paul said:
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?

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
 
M

Manfred Rosenboom

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
 
P

Paul J. Lucas

I'm using the JNI interface, i.e., I'm calling JNI_CreareJavaVM(). I'm
not calling the java.exe.

- Paul
 
P

Paul J. Lucas

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
 
R

Roedy Green

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.
 
T

Thomas Kellerer

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
 
L

Luke Webber

Paul said:
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.

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
 
C

Chris Uppal

Paul said:
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);

.........
=======================
 
P

Paul J. Lucas

Luke Webber said:
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
 
A

Andrew Thompson

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?)
 
P

Paul J. Lucas

Andrew Thompson said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top