Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Writing a Windows JVM launcher program (http://www.velocityreviews.com/forums/t147828-writing-a-windows-jvm-launcher-program.html)

Paul J. Lucas 11-16-2005 11:31 PM

Writing a Windows JVM launcher program
 
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 11-17-2005 12:49 AM

Re: Writing a Windows JVM launcher program
 
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.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Chris Uppal 11-17-2005 08:08 AM

Re: Writing a Windows JVM launcher program
 
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



Manfred Rosenboom 11-17-2005 08:38 AM

Re: Writing a Windows JVM launcher program
 
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

Paul J. Lucas 11-17-2005 08:00 PM

Re: Writing a Windows JVM launcher program
 
I'm using the JNI interface, i.e., I'm calling JNI_CreareJavaVM(). I'm
not calling the java.exe.

- Paul


Paul J. Lucas 11-17-2005 08:05 PM

Re: Writing a Windows JVM launcher program
 
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


Roedy Green 11-17-2005 08:50 PM

Re: Writing a Windows JVM launcher program
 
On 17 Nov 2005 12:00:45 -0800, "Paul J. Lucas" <google@pauljlucas.org>
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.

Thomas Kellerer 11-17-2005 10:21 PM

Re: Writing a Windows JVM launcher program
 
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

Luke Webber 11-18-2005 05:54 AM

Re: Writing a Windows JVM launcher program
 
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



Chris Uppal 11-18-2005 10:12 AM

Re: Writing a Windows JVM launcher program
 
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);

.........
=======================




All times are GMT. The time now is 02:39 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57