returning strings from native methods

Daniel P. Zepeda dpz@pobox.com
Thu Oct 21 14:42:00 GMT 1999


Hi,
	I've really tried to figure out how to do this from the
documentation, and apparently I'm not smart enough to do it. I want to
return a string created from C++ to my Java program. Can someone point
out what I'm doing wrong? This example is built onto what Glenn posted a
while back.
<+++ sampNat.cc ++++>
#include "sample.h"
#include "c_prog.h"
#include <stream.h>
#include <java/io/PrintStream.h>
#include <java/lang/System.h>
#include <java/lang/String.h>
#include <cni.h>
void sample::myNative(java::lang::String *s)
{
 
 cout << "Hello, C++" << endl;
 myCprog();
 
 // The following line causes a link error for a missing typeinfo
 // unless this file is compiled with '-fno-rtti'
 java::lang::System::out->println(s);
}
java::lang::String *sample::returnString()
{
 jstring s;
 s = JvNewString((jchar *) "This is a string returned from C++", 34 );
 return(s);
}
<+++ end of sampNat.cc +++++>
<++++ sample.java +++++> 
public class sample {
 public native void myNative(String s);
 public native String returnString();
 
 public void myJava(String s) {
	s = s + ", Java";
	System.out.println(s);
 }
 
 public static void main(String args[]) {
	sample x = new sample();
	x.myJava("Hello");
	x.myNative("Hello, Java (from C++)");
	x.myJava("Goodbye");
	String s = x.returnString();
	System.out.println("returnString() returned: " + s);
	
 }
}
<+++ end of sample.java +++>
<+++ Makefile ++++>
sample: sample.o sampNat.o myCprog.o
	/usr/local/bin/gcj -o sample sample.o sampNat.o myCprog.o -lstdc++ --main=sample
sample.o: sample.class
	/usr/local/bin/gcj -c sample.class
sample.class: sample.java
	/usr/local/bin/gcj -C sample.java
sample.h: sample.class
	/usr/local/bin/gcjh sample
sampNat.o: sample.h sampNat.cc
	/usr/local/bin/gcc \
 -I/usr/local/src/libgcj-2.95.1/libjava/include \
 -I/usr/local/src/libgcj-2.95.1/libjava \
 -I/usr/local/src/libgcj-2.95.1-obj/i686-pc-linux-gnu/libjava/include \
 -I/usr/local/src/libgcj-2.95.1-obj/i686-pc-linux-gnu/libjava \
 -fno-rtti \
 -c sampNat.cc
myCprog.o: myCprog.c
	gcc -c myCprog.c
clean:
	rm -f sample sample.o sampNat.o sample.class sample.h myCprog.o
<+++ end of Makefile +++>
These are not relevant to my problem but here they are:
<++ myCprog.c ++>
#include "myCprog.h"
#include <stdio.h>
void myCprog()
{
 printf( "This is from my C program\n" );
}
<++ end of myCprog.c ++>
<++ myCprog.h ++>
void myCprog();
<++ end of myCprog.h++>
<++ c_prog.h ++>
extern "C" {
#include "myCprog.h"
}
<++ end of c_prog.h ++>
When I try it, I get:
@rocinante:/home/dpz/src/CNITest.d> ./sample
Hello, Java
Hello, C++
This is from my C program
Hello, Java (from C++)
Goodbye, Java
returnString() returned: Ti sasrn eundfo +hsi rmm rga
@rocinante:/home/dpz/src/CNITest.d> 
 --
Daniel P. Zepeda
dpz@pobox.com
"In complete darkness, we are all the same. Only our knowledge and wisdom 
separates us there." -- J. Jackson


More information about the Java mailing list

AltStyle によって変換されたページ (->オリジナル) /