Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to convert static String return into JNI

 
Jhovarie Guiang
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello i now this is basic and i hope some one helps please read.

Sample #1
--> Start
//this is a java code that calls the JNI to print "Hello World"
class HelloWorld {
static {
//System.loadLibrary("HelloWorld");
System.load("C:/HelloWorld.dll");
}
private native void print();

public static void main(String[] args) {
new HelloWorld().print();
}
}

#include <jni.h>
#include <stdio.h>
#include "HelloWorld.h"

JNIEXPORT void JNICALL
Java_HelloWorld_print(JNIEnv *env, jobject obj)
{
printf("Hello Java Native Interface.\n");
return;
}
--> End

And i want to do is that java will get the value of string variables from JNI and it will print hello world. for example in using java like this.

Sample #2
--> Start
public class classOne {
public static void main(String[]args){
String stringfrom_classTwo = classTwo.pblic_static_str();
System.out.println(stringfrom_classTwo);
}
}

// how to convert classTwo into a JNI?
public class classTwo {
public static String public_static_str(){
String str = "Hello World";
return str;
}
}
--> End
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

You want to use the NewStringUTF() function, which you can see an example of here. That link is to an online copy of the JNI programmer's manual and guide -- it should answer any questions you have.
 
Jhovarie Guiang
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i cant understand the tutorials from that link can any one post a sample code using NewStringUTF() and access it on java string variable?

im telling that code like this

//java code


JNI code
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic