Writing a Windows JVM launcher program

Discussion in 'Java' started by Paul J. Lucas, Nov 16, 2005.

  1. 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
     
    Paul J. Lucas, Nov 16, 2005
    #1
    1. Advertising

  2. Paul J. Lucas

    Roedy Green Guest

    On Wed, 16 Nov 2005 23:31:47 GMT,
    (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.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Nov 17, 2005
    #2
    1. Advertising

  3. Paul J. Lucas

    Chris Uppal Guest

    Paul J. Lucas wrote:
    > 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
     
    Chris Uppal, Nov 17, 2005
    #3
  4. 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
     
    Manfred Rosenboom, Nov 17, 2005
    #4
  5. I'm using the JNI interface, i.e., I'm calling JNI_CreareJavaVM(). I'm
    not calling the java.exe.

    - Paul
     
    Paul J. Lucas, Nov 17, 2005
    #5
  6. 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
     
    Paul J. Lucas, Nov 17, 2005
    #6
  7. Paul J. Lucas

    Roedy Green Guest

    On 17 Nov 2005 12:00:45 -0800, "Paul J. Lucas" <>
    wrote, quoted or indirectly quoted someone who said :

    >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.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Nov 17, 2005
    #7
  8. 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
     
    Thomas Kellerer, Nov 17, 2005
    #8
  9. Paul J. Lucas

    Luke Webber Guest

    Paul J. Lucas wrote:
    > 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
     
    Luke Webber, Nov 18, 2005
    #9
  10. Paul J. Lucas

    Chris Uppal Guest

    Paul J. Lucas wrote:

    > 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);

    .........
    =======================
     
    Chris Uppal, Nov 18, 2005
    #10
  11. Luke Webber <> wrote:

    > 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
     
    Paul J. Lucas, Nov 20, 2005
    #11
  12. Paul J. Lucas wrote:

    > Luke Webber <> wrote:
    >
    >>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?)

    --
    Andrew Thompson
    physci, javasaver, 1point1c, lensescapes - athompson.info/andrew
    Currently accepting short and long term contracts - on Earth.
     
    Andrew Thompson, Nov 21, 2005
    #12
  13. Andrew Thompson <> wrote:

    > 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
     
    Paul J. Lucas, Nov 21, 2005
    #13
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kevin Hooke

    Re: Handling both MS JVM and Sun JVM

    Kevin Hooke, Aug 26, 2003, in forum: Java
    Replies:
    2
    Views:
    881
  2. Lasse
    Replies:
    1
    Views:
    702
    Jon A. Cruz
    Jan 5, 2004
  3. Young-Jin Lee

    MS JVM and Sun JVM problem

    Young-Jin Lee, Jan 20, 2004, in forum: Java
    Replies:
    3
    Views:
    579
    Mickey Segal
    Jan 21, 2004
  4. Telicalbook
    Replies:
    3
    Views:
    4,813
    Federico
    Feb 1, 2005
  5. Hiker Hauk

    java program launcher

    Hiker Hauk, Feb 19, 2006, in forum: Java
    Replies:
    6
    Views:
    2,320
    Hiker Hauk
    Feb 20, 2006
Loading...

Share This Page