This discussion is archived
7 Replies Latest reply: Nov 21, 2012 1:29 AM by EJP RSS

JNI-> Find caller class directory

975317 Newbie
Currently Being Moderated
Hello!

My native method in dll on C++ is:

JNIEXPORT jstring JNICALL Java_Auth_checkClient(JNIEnv *env, jclass cls, jint serverid)
{

}

How can I find out who called this method Java_Auth_checkClient. That is, I need the absolute path to the file (in this case, the jar), which calls Java_Auth_checkClient in dll.

thanks in advance
  • 1. Re: JNI-> Find caller class directory
    EJP Guru
    Currently Being Moderated
    You can get the caller class name from first element of Thread.getStackTrace(), and you can get the location from which any class was loaded via Class.getProtectionDomain().getCodeSource.getLocation().

    But you can't get from the class name to the class, except by loading it again, which might yield a different location.
  • 2. Re: JNI-> Find caller class directory
    975317 Newbie
    Currently Being Moderated
    I try to get path with Class.getProtectionDomain().getCodeSource.getLocation()

    But I encountered a problem: on the last line java crash.

    JNIEXPORT jstring JNICALL Java_Auth_checkClient(JNIEnv *env, jclass cls, jint serverid)
    {
    // get reference on Class
    jmethodID mid = env->GetMethodID(cls, "getClass", "()Ljava/lang/Class;");
    jobject objclass = (jobject)env->CallObjectMethod(cls,mid);
    jclass clsclass = env->GetObjectClass(objclass);

    // get reference on ProtectionDomain from Class
    jmethodID midPD = env->GetMethodID(clsclass, "getProtectionDomain", "()Ljava/security/ProtectionDomain;");
    jobject objPD = (jobject)env->CallObjectMethod( objclass, midPD);
    jclass clsPD = env->GetObjectClass( objPD);

    // get reference on CodeSource from ProtectionDomain
    jmethodID midCS = env->GetMethodID(clsPD, "getCodeSource", "()Ljava/security/CodeSource;");
    jobject objCS = (jobject)env->CallObjectMethod(clsPD,midCS); <<objCS=0
    jclass clscs = env->GetObjectClass(objCS); <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<JAVA CRASH ON THIS LINE
    }

    Tell me what I'm doing wrong, maybe give me some example

    Thanks in advance
  • 3. Re: JNI-> Find caller class directory
    EJP Guru
    Currently Being Moderated
    I try to get path with Class.getProtectionDomain().getCodeSource.getLocation()
    Why? I already told you you can't get the calling object's class, only its name.
    But I encountered a problem: on the last line java crash.
    jobject objCS = (jobject)env->CallObjectMethod(objPD,midCS); <<objCS=0
    jclass clscs = env->GetObjectClass(objCS);
    You have no business trying to get the class of a null (non-existent) object, so of course* it crashes. However as the technique I mentioned has a fatal gap in it where the calling class should be, which I also mentioned, I have no idea why you are wasting time like this in the first place.
    Tell me what I'm doing wrong
    (1) Not reading what you've been told here properly and (2) wasting time as a result.
    maybe give me some example
    You can't have an example of something that doesn't work.
  • 4. Re: JNI-> Find caller class directory
    975317 Newbie
    Currently Being Moderated
    EJP wrote:
    I try to get path with Class.getProtectionDomain().getCodeSource.getLocation()
    Why? I already told you you can't get the calling object's class, only its name.
    But I encountered a problem: on the last line java crash.
    jobject objCS = (jobject)env->CallObjectMethod(objPD,midCS); <<objCS=0
    jclass clscs = env->GetObjectClass(objCS);
    You have no business trying to get the class of a null (non-existent) object, so of course* it crashes. However as the technique I mentioned has a fatal gap in it where the calling class should be, which I also mentioned, I have no idea why you are wasting time like this in the first place.
    Tell me what I'm doing wrong
    (1) Not reading what you've been told here properly and (2) wasting time as a result.
    maybe give me some example
    You can't have an example of something that doesn't work.
    I write protection, and I need to know, from what file it calls the library dll. If the call is not from my file jar, then it should fail.

    So I need to know, from what file is invoked. This is generally a really create one in my case and how? Help me please.
  • 5. Re: JNI-> Find caller class directory
    EJP Guru
    Currently Being Moderated
    I have already answered your question. What part of it didn't you understand?
  • 6. Re: JNI-> Find caller class directory
    975317 Newbie
    Currently Being Moderated
    EJP wrote:
    I have already answered your question. What part of it didn't you understand?
    You can get the caller class name from first element of Thread.getStackTrace(), and you can get the location from which any class was loaded via Class.getProtectionDomain().getCodeSource.getLocation().
    I don't understand how to do it..
  • 7. Re: JNI-> Find caller class directory
    EJP Guru
    Currently Being Moderated
    The only thing you need to understand from my first reply is that you can't do it. It utterly amazes me that you didn't appreciate that when I said it.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points