# JNI & Cocoa



## phil (Mar 15, 2001)

Can a Java application call a cocoa library ?
I'd like to create a library using the cocoa features and call this library from my Java application using JNI.

For test purpose, I created a simple library which just loads a nib, but all I got is this exception : 
"[NSJavaVirtualMachine initWithClassPath:] - cannot instantiate a Java virtual machine"

the entry point in the library looks like that :
JNIEXPORT void JNICALL Java_hello_test(JNIENV*env,jclass jc)
{
    [NSBundle loadNibNamed"sample" owner:nil];
}

By the way, will I need to call [NSApp run] as if it was an application ?


Any help appreciated.


----------



## cbaron (Mar 23, 2001)

Java has been made for portability, that means a Java app can work on any computer and that's why you MUSN'T use elements of the Cocoa API with your Java apps.

Therefor, Cocoa API includes Java, so you can build your Cocoa apps in Java


----------



## cbaron (Mar 23, 2001)

If you still want to use Cocoa API with your Java apps, you have to import cocoa.AppKit and cocoa.fundations (the import method)


----------



## strobe (Mar 24, 2001)

You must also be crazy not to use Objective-C


----------



## phil (Mar 26, 2001)

Thanks for your advice about portability but if I need to use JNI, there IS a reason : that's because I need to call system dependant functions that are not available through Java.

Anyway, I managed to use Java Bridge to do what I want.


----------



## sovtek (Jul 30, 2008)

We have the need to do this as well and we do have a good reason. 
Reason: cocoa has great libraries in coregraphics to generate thumbnails for pdf files however cocoa is total crap when for things like running an xml-rpc server. So in this case we need our Java xml rpc server to receive calls and pipe them into cocoa cleanly. Is there any way to do this?


----------

