Skip navigation.
 
mlRe: delaying JVM launch time
FROM : Jesse Grosjean
DATE : Wed Dec 18 19:18:01 2002

Thanks!

I've changed things around a little and it's working great. If anyone
else is calling java from objective-c I highly recommend loading the
jvm lazily, it improves startup time a lot, and doesn't seem to cause
all that much of a pause when the jvm is finally loaded when needed.

- (NSJavaVirtualMachine *)vm {
    static NSJavaVirtualMachine *vm;
    if (vm == nil) {
        NSMutableString *path = [NSMutableString
stringWithCapacity:100];
        NSString *resourcePath = [[NSBundle bundleForClass:[self
class]] resourcePath];
        [path appendString:[NSJavaVirtualMachine defaultClassPath]];
        [path appendString:@":"];
        [path appendString:resourcePath];
        [path appendString:@"/Java/myJar.jar"];
        vm = [[NSJavaVirtualMachine alloc] initWithClassPath:path];
    }
    return vm;
}
...
    myInstance = [[[[self vm] findClass:@"myClass"] alloc] init];

Jesse

On Wednesday, December 18, 2002, at 07:45  PM, Timothy J. Wood wrote:

>
> On Wednesday, December 18, 2002, at 04:59  AM, Jesse Grosjean wrote:

>>    NSString *path = @"Versions/A/Resources/Java/my.jar";
>>    NSJavaVirtualMachine *vm = [NSJavaVirtualMachine
>> defaultVirtualMachine];
>>    [vm initWithClassPath:[path1 stringByAppendingString:path2]];
>>    _myClass = [[NSClassFromString(@"com.hogbay.myClass") alloc]
>> init];

>
>  +defaultVirtualMachine will return an already initialized object,
> thus, sending a -initWithClassPath: method to it will at best do
> nothing and at worst break.  You might try doing the following:
>
>  -- Call +[NSJavaVirtualMachine defaultClassPath]
>  -- Append your jar path to this
>  -- Call alloc/initWithClassPath: to create a new NSJVM
>
>  Hopefully this will case the +defaultVirtualMachine to return the
> JVM you initialized (which you can check by calling it after you init
> your JVM and comparing pointers).
>
>
>  If that doesn't work, you might look at the OmniJava source that we
> publish at:
>
>     http://www.omnigroup.com/ftp/pub/software/Source/MacOSX/Frameworks/
>
>  This is what we use in OmniWeb to start the JVM, add class path
> elements, extra security goo.  It is probably pretty heavy weight for
> what you want, but you should be able to extract the JNI code to start
> a JVM.
>
>  Of course, it is worth noting that I don't know how this interacts
> with NSJavaVirtualMachine.  We don't use that in OmniWeb since none of
> our app is in Java, we don't care about the bridge -- we always use
> JNI.  Hopefully, NSJVM will realize that the JVM has already been
> created and not do anything.
>
> -tim
>
>
>


Related mailsAuthorDate
mldelaying JVM launch time Jesse Grosjean Dec 18, 05:02
mlRe: delaying JVM launch time Timothy J. Wood Dec 18, 16:46
mlRe: delaying JVM launch time Jesse Grosjean Dec 18, 19:18