Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

Cannot pass String as JNI jstring parameter

Aperta
#1,099 7 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
35/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Ferma
Stack tecnologico
c, java
Ambito
backend

Direzione di ricerca

Inizia tracciando la conversione degli argomenti di Function tramite convertArgument(), in particolare la gestione di String, OPTION_ALLOW_OBJECTS e JniEnv.CURRENT. Verifica come viene convertito attualmente un Java String prima dell’invocazione e determina se gli oggetti jstring possono passare senza interrompere le conversioni specifiche di JNA. Il lavoro è concluso quando la chiamata JVM_FindLoadedClass segnalata può ricevere un Java String come jstring, con una copertura del comportamento di conversione.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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

Lingua principale
Java
Stelle
8.9k
Fork
1.7k
Merge medio
1g 13h
PR unite (30g)
1

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di java-native-access/jna

Tutte le issue di java-native-access/jna

Issue simili

Altre issue su Java

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.