Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

Cannot pass String as JNI jstring parameter

Đang mở
#1,099 7 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
35/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
c, java
Lĩnh vực
backend

Hướng nghiên cứu

Bắt đầu bằng cách theo dõi quá trình chuyển đổi đối số của Function thông qua convertArgument(), đặc biệt là cách xử lý String, OPTION_ALLOW_OBJECTS và JniEnv.CURRENT. Xác minh cách một Java String hiện được chuyển đổi trước khi gọi và xác định liệu các đối tượng jstring có thể được truyền qua mà không làm hỏng các chuyển đổi dành riêng cho JNA hay không. Được xem là hoàn tất khi lệnh gọi JVM_FindLoadedClass được báo cáo có thể nhận một Java String dưới dạng jstring, với độ bao phủ cho hành vi chuyển đổi.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

  1. Version of JNA and related jars
    JNA 5.3.1

  2. Version and vendor of the java virtual machine
    Oracle Corporation Java HotSpot(TM) 64-Bit Server VM 1.8.0_181

  3. Operating system
    Ubuntu 16.04.6

  4. System architecture (CPU type, bitness of the JVM)
    Intel x64, x64 JVM

  5. Complete description of the problem
    I cannot pass String objects to a JNI method for invocation, (even using JniEnv.CURRENT and setting OPTION_ALLOW_OBJECTS), since a String is always converted to NativeString.

    When trying to call JNI methods, JNA invokes convertArgument() just before C method invocation, and String is converted to NativeString before allowObjects is checked. This means that it is impossible to pass the Java String (jstring) object to C (which accepts jstring as a parameter type). For example, JVM_FindLoadedClass(JNIEnv *env, jobject loader, jstring name);

  6. Steps to reproduce

    public
    interface JVM extends com.sun.jna.Library {
         Class JVM_FindLoadedClass(JNIEnv env, Object classLoader, String name);
    }


    Map options = new HashMap();
    options.put(Library.OPTION_ALLOW_OBJECTS, Boolean.TRUE);

    JVM libjvm = Native.load("jvm", JVM.class, options);
    jvm.JVM_FindLoadedClass(JNIEnv.CURRENT, classLoader, className);

It is entirely likely that I don't know what I'm doing, after reading through the documentation and studying the source -- I do not know how to pass a String as a jstring object (not a char* object) to native code. I've tried type converters, but those didn't get me anywhere, since String is always converted to NativeString as an argument...

Because the Function constructor is package scope, as is invoke, I cannot overload invoke to not convert arguments, so I am unable to modify the method invocation (outside of rewriting the class definition at runtime via javassist or something similar)

One solution I thought of, while trying to figure this out, is modify the convertArgument() method so that the check for allowObjects occurs sooner, allowing all Java objects to passed without conversion -- so the check for allowObjects is after JNA specific object types (struct, etc), but before the Java object type conversion (mostly string), instead of where currently is (which is last)....

NOTE: Loading the JVM library for Windows 32 bit, or MACOS is slightly different. The above only loads it on Linux, specifically Ubuntu 16.04.6 x64 with an Oracle JVM (from my testing, this is currently true for all released Oracle JVM's on Linux, but I haven't tested every combination....)

Ngôn ngữ chính
Java
Star
8.9k
Fork
1.7k
Merge trung bình
1 ngày 13 giờ
Pull request đã merge (30 ngày)
1

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của java-native-access/jna

Tất cả issue của java-native-access/jna

Issue tương tự

Thêm issue về Java

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.