2017-04-21 76 views
0

我在FileOutputStream.javaJNI轉換jint爲本地INT用C

public native void write(int b) throws IOException; 

我讀過的this thread,對於jint參數轉換爲本地詮釋你只需要投中聲明下面的函數它。我的C代碼:

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

如果我稱之爲Java中的功能,我收到以下錯誤信息:

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

所以,我想我的演員是錯誤的。我需要做些什麼才能讓它正確?

回答

2

鑄造jint沒有錯,但printf需要一個帶參數的格式字符串。

您的代碼嘗試訪問地址爲bchar *,並且因爲b不是其崩潰的實際地址。查找文件printf爲您的編譯器(或任何printf文檔,這一個例如:https://linux.die.net/man/3/printf)。

+0

aah,愚蠢的錯誤...將其更改爲printf(「%d」,b);它工作得很好!非常感謝! – Vilib