  | Mailing List | | Home | | Forum Home | | JBoss - Java Application Server | | Tomcat - JSP/Servlet container | | Struts - A MVC web framework | | iText - An open source PDF Java Library | | JDOM - JDOM XML Parser | | JSP - A mailing list about Java Server Pages specification and reference | | J2EE - A mailing list for Java(tm) 2 Platform, Enterprise Edition | | J2EE Pattern - An interest list for Sun Java Center J2EE Pattern Catalog | | Servlet - A mailing list for discussion about Sun Microsystem's Java Servlet API Technology | |
Struts & Hibernate
|
|
|
  | | | Subject: How to get a String from JNI? | Subject: How to get a String from JNI? 2005-09-09 - By Kurien Joseph
Back This line will go to your C++ code.
jstring str = (env)->NewStringUTF ((char *)"Hello World");
return str;
If you have c then use
jstring str = (*env)->NewStringUTF ((char *)"Hello World");
Looking at your MyNative class... you need not create test1 and test11 will suffice.
-Kurien
__ __
From: A mailing list for Java(tm) 2 Platform, Enterprise Edition [mailto:J2EE -INTEREST@(protected)] On Behalf Of Edward King Sent: Thursday, September 08, 2005 9:36 PM To: J2EE-INTEREST@(protected) Subject: How to get a String from JNI?
I want to get a returned String from JNI, but I don't know how to do it, my code are follows: //MyNative.java public class MyNative{ public String test1(String s){ return test11(s); } private native String test11(String s); static{ System.loadLibrary( "MyNative" ); } public static void main( String[] args ){ MyNative obj = new MyNative(); String c=obj.test1("abc"); System.out.println("--"+c); } }
//MyNative.h /* DO NOT EDIT THIS FILE - it is machine generated */ #include <jni.h> /* Header for class MyNative */ #ifndef _Included_MyNative #define _Included_MyNative #ifdef __cplusplus extern "C" { #endif /* * Class: MyNative * Method: test11 * Signature: (Ljava/lang/String;)Ljava/lang/String; */ JNIEXPORT jstring JNICALL Java_MyNative_test11 (JNIEnv *, jobject, jstring);
#ifdef __cplusplus } #endif #endif
//MyNative.c #include <stdio.h> #include "MyNative.h" JNIEXPORT jstring JNICALL Java_MyNative_test11 (JNIEnv *env, jobject obj, jstring s){ //How to realize return String to Java???,such as return a string "hello world",how to realize this function,please give example code.
}
Thanks in advance!
=========================================================================== To unsubscribe, send email to listserv@(protected) and include in the body of the message "signoff J2EE-INTEREST". For general help, send email to listserv@(protected) .sun.com and include in the body of the message "help".
=========================================================================== To unsubscribe, send email to listserv@(protected) and include in the body of the message "signoff J2EE-INTEREST". For general help, send email to listserv@(protected) and include in the body of the message "help".
<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft -com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:st1= "urn:schemas-microsoft-com:office:smarttags" xmlns="http://www.w3.org/TR/REC -html40">
<head> <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=us-ascii"> <meta name=Generator content="Microsoft Word 11 (filtered medium)"> <!--[if !mso]> <style> v\:* {behavior:url(#default#VML);} o\:* {behavior:url(#default#VML);} w\:* {behavior:url(#default#VML);} .shape {behavior:url(#default#VML);} </style> <![endif]--><o:SmartTagType namespaceuri="urn:schemas-microsoft-com:office:smarttags" name="PersonName" downloadurl="http://www.microsoft.com"/> <!--[if !mso]> <style> st1\:*{behavior:url(#default#ieooui) } </style> <![endif]--> <style> <!-- /* Font Definitions */ @(protected) {font-family:SimSun; panose-1 (See http://ose-1.ora-code.com):2 1 6 0 3 1 1 1 1 1;} @(protected) {font-family:Tahoma; panose-1 (See http://ose-1.ora-code.com):2 11 6 4 3 5 4 4 2 4;} @(protected) {font-family:"\@(protected)";} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {margin:0in; margin-bottom:.0001pt; font-size:12.0pt; font-family:SimSun;} a:link, span.MsoHyperlink {color:blue; text-decoration:underline;} a:visited, span.MsoHyperlinkFollowed {color:purple; text-decoration:underline;} span.EmailStyle17 {mso-style-type:personal-reply; font-family:Arial; color:navy;} @(protected) Section1 {size:8.5in 11.0in; margin:1.0in 1.25in 1.0in 1.25in;} div.Section1 {page:Section1;} --> </style> <!--[if gte mso 9]><xml> <o:shapedefaults v:ext="edit" spidmax="1026" /> </xml><![endif]--><!--[if gte mso 9]><xml> <o:shapelayout v:ext="edit"> <o:idmap v:ext="edit" data="1" /> </o:shapelayout></xml><![endif]--> </head>
<body bgcolor=white lang=EN-US link=blue vlink=purple>
<div class=Section1>
<p class=MsoNormal><font size=2 face="Courier New"><span style='font-size:10 .0pt; font-family:"Courier New"'>This line will go to your C++ code. <o:p></o:p>< /span></font></p>
<p class=MsoNormal><font size=2 face="Courier New"><span style='font-size:10 .0pt; font-family:"Courier New"'>jstring str = (env)->NewStringUTF ((<font color=blue><span style='color:blue'>char</span></font> *)"Hello World" ;);<o:p></o:p></span></font></p>
<p class=MsoNormal><font size=2 face="Courier New"><span style='font-size:10 .0pt; font-family:"Courier New"'>return str;</span></font><font size=2 color=navy face=Arial><span style='font-size:10.0pt;font-family:Arial;color:navy'><o:p></o :p></span></font></p>
<p class=MsoNormal><font size=2 face="Courier New"><span style='font-size:10 .0pt; font-family:"Courier New"'><o:p> </o:p></span></font></p>
<p class=MsoNormal><font size=2 face="Courier New"><span style='font-size:10 .0pt; font-family:"Courier New"'><o:p> </o:p></span></font></p>
<p class=MsoNormal><font size=2 face="Courier New"><span style='font-size:10 .0pt; font-family:"Courier New"'>If you have c then use<o:p></o:p></span></font></p>
<p class=MsoNormal><font size=2 face="Courier New"><span style='font-size:10 .0pt; font-family:"Courier New"'>jstring str = (*env)->NewStringUTF ((<font color=blue><span style='color:blue'>char</span></font> *)"Hello World" ;);<o:p></o:p></span></font></p>
<p class=MsoNormal><font size=2 face="Courier New"><span style='font-size:10 .0pt; font-family:"Courier New"'><o:p> </o:p></span></font></p>
<p class=MsoNormal><font size=2 face="Courier New"><span style='font-size:10 .0pt; font-family:"Courier New"'>Looking at your MyNative class… you need not create test1 and test11 will suffice.<o:p></o:p></span></font></p>
<p class=MsoNormal><font size=2 face="Courier New"><span style='font-size:10 .0pt; font-family:"Courier New"'><o:p> </o:p></span></font></p>
<p class=MsoNormal><font size=2 face="Courier New"><span style='font-size:10 .0pt; font-family:"Courier New"'>-Kurien<o:p></o:p></span></font></p>
<p class=MsoNormal><font size=2 face="Courier New"><span style='font-size:10 .0pt; font-family:"Courier New"'><o:p> </o:p></span></font></p>
<p class=MsoNormal><font size=2 color=navy face=Arial><span style='font-size: 10.0pt;font-family:Arial;color:navy'><o:p> </o:p></span></font></p>
<div>
<div class=MsoNormal align=center style='text-align:center'><font size=3 face=SimSun><span style='font-size:12.0pt'>
<hr size=2 width="100%" align=center tabindex=-1>
</span></font></div>
<p class=MsoNormal><b><font size=2 face=Tahoma><span style='font-size:10.0pt; font-family:Tahoma;font-weight:bold'>From:</span></font></b><font size=2 face=Tahoma><span style='font-size:10.0pt;font-family:Tahoma'> <st1:PersonName w:st="on">A mailing list for Java</st1:PersonName>(tm) 2 Platform, Enterprise Edition [mailto:J2EE-INTEREST@(protected)] <b><span style='font-weight:bold'>On Behalf Of </span></b>Edward King<br> <b><span style='font-weight:bold'>Sent:</span></b> Thursday, September 08, 2005 9:36 PM<br> <b><span style='font-weight:bold'>To:</span></b> J2EE-INTEREST@(protected)<br> <b><span style='font-weight:bold'>Subject:</span></b> How to get a String from JNI?</span></font><o:p></o:p></p>
</div>
<p class=MsoNormal><font size=3 face=SimSun><span style='font-size:12.0pt'><o:p > </o:p></span></font></p>
<div>
<p class=MsoNormal><font size=2 color=blue face="Times New Roman"><span style='font-size:10.0pt;font-family:"Times New Roman";color:blue'>I want to get a returned String from JNI, but I don't know how to do it, my code are follows: <br> //MyNative.java<br> public class MyNative{ <br> public String test1(String s){<br> return test11(s);<br> }<br> private native String test11(String s); <br> static{<br> System.loadLibrary( "MyNative" );<br> } <br> public static void main( String[] args ){<br> MyNative obj = new MyNative();<br> String c=obj.test1("abc");<br> System.out.println("--"+c);<br> }<br> }</span></font><o:p></o:p></p>
</div>
<div>
<p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt;font-family:"Times New Roman"'> </span></font><o:p></o:p></p>
</div>
<div>
<p class=MsoNormal><font size=2 color=blue face="Times New Roman"><span style='font-size:10.0pt;font-family:"Times New Roman";color:blue'>//MyNative.h <br> /* DO NOT EDIT THIS FILE - it is machine generated */<br> #include <jni.h><br> /* Header for class MyNative */<br> #ifndef _Included_MyNative<br> #define _Included_MyNative<br> #ifdef __cplusplus<br> extern "C" {<br> #endif<br> /*<br> * Class: MyNative<br> * Method: test11<br> * Signature: (Ljava/lang/String;)Ljava/lang/String;<br> */<br> JNIEXPORT jstring JNICALL Java_MyNative_test11<br> (JNIEnv *, jobject, jstring);</span></font><o:p></o:p></p>
</div>
<div>
<p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt;font-family:"Times New Roman"'> </span></font><o:p></o:p></p>
</div>
<div>
<p class=MsoNormal><font size=2 color=blue face="Times New Roman"><span style='font-size:10.0pt;font-family:"Times New Roman";color:blue'>#ifdef __cplusplus<br> }<br> #endif<br> #endif</span></font><o:p></o:p></p>
</div>
<div>
<p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt;font-family:"Times New Roman"'> </span></font><o:p></o:p></p>
</div>
<div>
<p class=MsoNormal><font size=2 color=blue face="Times New Roman"><span style='font-size:10.0pt;font-family:"Times New Roman";color:blue'>//MyNative.c <br> #include <stdio.h><br> #include "MyNative.h"<br> JNIEXPORT jstring JNICALL Java_MyNative_test11<br> (JNIEnv *env, jobject obj, jstring s){<br> //How to realize return String to Java???,such as return a string "hello world",how to realize this function,please give example code.</span></font><o:p></o:p></p>
</div>
<div>
<p class=MsoNormal><font size=2 color=blue face="Times New Roman"><span style='font-size:10.0pt;font-family:"Times New Roman";color:blue'>}</span>< /font><o:p></o:p></p>
</div>
<div>
<p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt;font-family:"Times New Roman"'> </span></font><o:p></o:p></p>
</div>
<div>
<p class=MsoNormal><font size=2 color=blue face="Times New Roman"><span style='font-size:10.0pt;font-family:"Times New Roman";color:blue'>Thanks in advance!<o:p></o:p></span></font></p>
</div>
<div>
<p class=MsoNormal><font size=2 color=blue face="Times New Roman"><span style='font-size:10.0pt;font-family:"Times New Roman";color:blue'><o:p> < /o:p></span></font></p>
</div>
</div>
</body>
</html> =========================================================================== To unsubscribe, send email to listserv@(protected) and include in the body of the message "signoff J2EE-INTEREST". For general help, send email to listserv@(protected) and include in the body of the message "help". <p> =========================================================================== To unsubscribe, send email to listserv@(protected) and include in the body of the message "signoff J2EE-INTEREST". For general help, send email to listserv@(protected) and include in the body of the message "help". <p>
|
|
 |