2017-04-21 2 views
0

Ich habe die folgende Funktion in FileOutputStream.java erklärtJNI konvertieren JINT auf native int in C

public native void write(int b) throws IOException; 

ich in this thread gelesen habe, dass die JINT Parameter auf einen nativen für die Umwandlung von int Sie müssen einfach werfen es. Meine C-Code:

JNIEXPORT void JNICALL Java_FileOutputStream_write__I(JNIEnv* jni, jobject obj, jint b){ 
    int native_b = (int)b; 
    printf(b); 
} 

Wenn ich die Funktion in Java aufrufen, bekomme ich folgende Fehlermeldung:

# 
# A fatal error has been detected by the Java Runtime Environment: 
# 
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x00007ffc7e01f3b2, pid=8700, tid=0x00000000000020e4 
# 
# JRE version: Java(TM) SE Runtime Environment (8.0_112-b15) (build 1.8.0_112-b15) 
# Java VM: Java HotSpot(TM) 64-Bit Server VM (25.112-b15 mixed mode windows-amd64 compressed oops) 
# Problematic frame: 
# C [msvcrt.dll+0x4f3b2] 
# 
# Failed to write core dump. Minidumps are not enabled by default on client versions of Windows 
# 
# An error report file with more information is saved as: 
# <my_path>\JNI\hs_err_pid8700.log 
# 
# If you would like to submit a bug report, please visit: 
# http://bugreport.java.com/bugreport/crash.jsp 
# The crash happened outside the Java Virtual Machine in native code. 
# See problematic frame for where to report the bug. 
# 

Also ich denke, meine Besetzung falsch ist. Was muss ich tun, um es richtig zu machen?

Antwort

2

Nichts ist falsch mit dem jint, aber printf erwartet eine Formatzeichenfolge mit Argumenten.

Ihr Code versucht, auf die char * unter der Adresse b zuzugreifen, und da b keine tatsächliche Adresse ist, stürzt es ab. Suchen Sie in der Dokumentation nach printf für Ihren Compiler (oder einfach eine beliebige printf Dokumentation, diese zum Beispiel: https://linux.die.net/man/3/printf).

+0

aah, dummer Fehler ... Änderte es in printf ("% d", b); und es funktioniert ganz gut! Vielen Dank! – Vilib