> For the complete documentation index, see [llms.txt](https://sungcheol-kim.gitbook.io/jni-tutorial/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://sungcheol-kim.gitbook.io/jni-tutorial/chapter21.md).

# 자바 클래스 찾기 2/2

패키지 명을 포함한 자바 클래스 이름을 전달해 해당 클래스를 찾는 예제를 작성해 보자.

```
$ vi FindClass2.cpp
```

```cpp
#include <jni.h>
#include <iostream>
#include <algorithm>
#include <string>

/**
 * 클래스 이름으로 클래스를 찾는다.
 */
JNIEXPORT void JNICALL findClass2
(
    JNIEnv        *env,
    jobject        thiz,
    jstring        className
)
{
    //convert java-string to c-string
    const char *cName = env->GetStringUTFChars(className, nullptr);

    std::string name = std::string(cName);
    env->ReleaseStringUTFChars(className, cName);

    //convert com.package.someclass to com/package/someclass
    std::replace(name.begin(), name.end(), '.', '/');

    //find some class
    jclass someClass = env->FindClass(name.c_str());
    if(someClass == nullptr) {
        std::cout << "Failed to find the " << name << " class" << std::endl;
    } else {
        std::cout << "Success to find the " << name << " class" << std::endl;
    }

}

JNIEXPORT jint JNICALL JNI_OnLoad
(
    JavaVM        *vm,
    void            *reserved
)
{
    JNIEnv *env;
    if(vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6)) {
        return -1;
    }

    JNINativeMethod nm[1] = {
        {
            const_cast<char*>("findClass2"),
            const_cast<char*>("(Ljava/lang/String;)V"),
            reinterpret_cast<void*>(findClass2)
        }
    };

    jclass cls = env->FindClass("Client");
    env->RegisterNatives(cls, nm, 1);
    return JNI_VERSION_1_6;
}
```

네이티브 레이어에서 자바 레이어의 클래스를 찾기 위해서 점을 슬래시로 변경해야 한다. 관련 내용이 findClass2 함수에 있다. 이제 이 코드를 컴파일해서 라이브러리로 만든다.

```
$ g++ "-I/System/Library/Frameworks/JavaVM.framework/Versions/Current/Headers/" -std=c++11 -c FindClass2.cpp
$ g++ -dynamiclib -o libfindclass2.jnilib findclass2.o
```

자바 코드에서 라이브러리를 사용해 보자.

```java
public class Client {

    public native void findClass2(String fullClassName);

    public static void main(String[] args) {

        Client client = new Client();

        if(args.length == 0) {
            client.findClass2("java.lang.String");
            client.findClass2("com.earth.Person");
        } else {
            client.findClass2(args[0]);
        }

    }

    static {
        System.loadLibrary("findclass2");
    }
}
```

컴파일하고 실행해 본다.

```
$ javac Client.java
$ java Client
Success to find the java/lang/String class
Failed to find the com/earth/Person class
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://sungcheol-kim.gitbook.io/jni-tutorial/chapter21.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
