Hacktoberfest 2026: los issues que los mantenedores marcaron para octubre, abiertos y aptos para principiantes. Explorar issues de Hacktoberfest

Cannot pass String as JNI jstring parameter

Abierto
#1,099 7 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
35/100
Tipo de issue
Error
Claridad
Bastante claro
Estado de actividad
Estancado
Stack tecnológico
c, java
Área
backend

Línea de trabajo

Empieza siguiendo la conversión de argumentos de Function mediante convertArgument(), especialmente el tratamiento de String, OPTION_ALLOW_OBJECTS y JniEnv.CURRENT. Verifica cómo se convierte actualmente un Java String antes de la invocación y determina si los objetos jstring pueden pasar sin romper las conversiones específicas de JNA. Se considera terminado cuando la llamada JVM_FindLoadedClass indicada puede recibir un Java String como jstring, con cobertura para el comportamiento de conversión.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

  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....)

Lenguaje dominante
Java
Estrellas
8.9k
Forks
1.7k
Merge medio
1 d 13 h
PR fusionados (30 d)
1

Preparar el entorno

Este proyecto no incluye contenedor de desarrollo, Dockerfile ni guía de contribución, así que la configuración corre por tu cuenta: empieza por su README y consulta nuestra guía para la primera contribución para los pasos generales.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de java-native-access/jna

Todos los issues de java-native-access/jna

Issues similares

Más issues de Java

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.