小编use*_*290的帖子

JNI如何访问Java Object(Integer)

我有一个JNI方法来访问返回Integer对象的java方法.我不想返回原始int类型,因为将修改此代码以处理Generic对象.以下是我所拥有的.我无法获得通过的Integer的值.C++方面的输出类似于

value = 0x4016f3d0
Run Code Online (Sandbox Code Playgroud)

如何获得我在C++端传递的Integer对象的实际值?

请帮忙.

谢谢,

-H

GenericPeer.cpp

JNIEXPORT void JNICALL Java_GenericPeer_print (JNIEnv *jenv, jclass jcls, jobject data){
 jclass peerCls = jenv->GetObjectClass(data);
 jmethodID mGetValue = jenv->GetMethodID(peerCls, "getValue","()Ljava/lang/Integer;");
 if(mGetValue == NULL){
   return (-1);
 } 
 jobject value = jenv->CallObjectMethod(data, mGetValue);
 cout<<"value = "<<value<<endl;

}
Run Code Online (Sandbox Code Playgroud)

GenericPeer.java

public class GenericPeer {
 public static native void print(Data d);
 static {
  System.load("/home/usr/workspace/GenericJni/src/libGenericJni.so");
 }
}
Run Code Online (Sandbox Code Playgroud)

Data.java

public class Data {
 private Integer value;
 pubilc Data(Integer v){ 
  this.value = v;
 }
 public Integer getValue() { return value; …
Run Code Online (Sandbox Code Playgroud)

c++ java java-native-interface

12
推荐指数
2
解决办法
2万
查看次数

无法从Java中的命名管道读取

我通过调用mkfifo()命令使用JNI创建命名管道.我使用的是mode = 0666.

我正在尝试使用Java写入管道,但是在写入管道时我遇到了问题.我被困在以下一行并且无法通过它.我也没有收到任何错误.

PrintWriter out = new PrintWriter((new BufferedWriter(new FileWriter(pipePath))));
Run Code Online (Sandbox Code Playgroud)

请帮忙.

问候,

-H

PipePeer.java

import java.io.*;*

public class PipePeer {

    private native int createPipe(String pipeName, int mode);* // --------> native method to call mkfifo(pipeName, mode); 
    static {
        System.load("/home/user/workspace/Pipes/libpipe.so");
    }

    public static void main(String[] args) {

        PipePeer p = new PipePeer();
        PipeImplement pi = new PipeImplement();
        String pipePath = "/home/user/workspace/Pipes/pipeFolderpipe1";
        int mode = 0777;
        int createResult = p.createPipe(pipePath, mode);
        if (createResult == 0)
            System.out.println("Named pipe created successfully");
        else
            System.out.println("Error: couldnot …
Run Code Online (Sandbox Code Playgroud)

java named-pipes

4
推荐指数
1
解决办法
6954
查看次数

数组未正确返回

我试图返回一个简单的数组,但我没有得到正确的结果.我得到以下内容

arr1[0] = 1 
arr1[1] = 32767
Run Code Online (Sandbox Code Playgroud)

结果应该是结果

  arr1[0] = 1 
  arr1[1] = 15
Run Code Online (Sandbox Code Playgroud)

请建议.

int *sum(int a, int b){
  int arr[2];
  int *a1;
  int result = a+b;
  arr[0]= 1;
  arr[1]= result;
  a1 = arr;
  return a1;
}
int main(){  

  int *arr1 =  sum(5,10);
  cout<<"arr1[0] = "<<arr1[0]<<endl;
  cout<<"arr1[1] = "<<arr1[1]<<endl;

  return 0;

}
Run Code Online (Sandbox Code Playgroud)

c++ arrays

3
推荐指数
4
解决办法
181
查看次数

标签 统计

c++ ×2

java ×2

arrays ×1

java-native-interface ×1

named-pipes ×1