Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

JNI and JARs



Hi,
I am having a problem with loading classes in an external JAR from a JNI app. I want to use Microsoft's JDBC driver from an Objective-C++ program, but I can't seem to get the classes to load. I've tried everything I can think of to get this to work, but I have had a very hard time trying to find accurate information on embedding Java in native applications. Sun's JNI tutorial seems to have some outdated information. I am sure it is just some boneheaded mistake I am making, but I have tried searching this list and everything else I could find for what I might be missing and I haven't found it yet. I've tried the exact same code in a Java app (well, except it was written directly in Java) and I can access the database, execute queries, and display results. I've looked at the verbose output for both and they seem identical, except that the Java version loads the com.microsoft classes and the Obj-C++ version throws an exception that it can't find them. Any help would be appreciated it.
The exact error is:
Exception in thread "main" java.lang.ClassNotFoundException: com/ microsoft/jdbc/sqlserver/SQLServerDriver
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:141)


The line of code that causes it is:
env->CallStaticObjectMethod(ClassCls, forNameMID, env->NewStringUTF ("com.microsoft.jdbc.sqlserver.SQLServerDriver"));


I've included the simplest version of the code I can get to reproduce the error at the end of this e-mail. Copy it to a file named "jnitest.mm" and then it can be compiled with the line:
g++ -Wall jnitest.mm -framework Foundation -framework JavaVM -o jnitest


You can get the MS SQL Server 2000 Driver for JDBC SP3 at:
http://download.microsoft.com/download/4/1/d/ 41d3e9c0-64d1-451e-947b-7a4cba273b2d/mssqlserver.tar
Move the tar to its own directory and then open it and the resulting msjdbc.tar file. Just move the JARs (msbase.jar, mssqlserver.jar, msutil.jar) from the lib/ dir into /Library/Java/Extensions and that should be enough to load the class.


It appears that the Java program automatically looks in /Library/Java/ Extensions for additional classes (I don't explicitly mention the directory when running it). Should my JNI instantiation of the JVM also look there automatically (i.e., is my explicit setting of java.class.path necessary)? Also, if anyone else can recommend some good resources on accessing Java classes from native programs, I would really appreciate it. I have had a real hard time tracking anything down.

Thanks,
Nathan Florea

--- jnitest.mm
#import <Foundation/Foundation.h>
#import <JavaVM/jni.h>

int main (int argc, const char * argv[]) {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    JavaVMOption options[2];
    JNIEnv *env;
    JavaVM *jvm;
    JavaVMInitArgs vm_args;
    long status;
    jthrowable exc;

    vm_args.version = JNI_VERSION_1_4;
    JNI_GetDefaultJavaVMInitArgs(&vm_args);

options[0].optionString = "-Djava.class.path=.:/Library/Java/ Extensions/msbase.jar:/Library/Java/Extensions/mssqlserver.jar:/ Library/Java/Extensions/msutil.jar";
options[0].extraInfo = NULL;


    //options[1].optionString = "-verbose";
    //options[1].extraInfo = NULL;

    vm_args.nOptions = 1;
    vm_args.options = options;

    status = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);
    if (status == JNI_ERR) return -1;

jclass ClassCls = env->FindClass("java/lang/Class");
jmethodID forNameMID = env->GetStaticMethodID(ClassCls, "forName", "(Ljava/lang/String;)Ljava/lang/Class;");
env->CallStaticObjectMethod(ClassCls, forNameMID, env- >NewStringUTF("com.microsoft.jdbc.sqlserver.SQLServerDriver"));
exc = env->ExceptionOccurred();
if (exc) {
env->ExceptionDescribe();
env->ExceptionClear();
}


    [pool release];
    return 0;
}
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Java-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/java-dev/email@hidden

This email sent to email@hidden


Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.