On Sat, Apr 12, 2014 at 10:36 AM, Jan Knezik <jan.kn@email.cz> wrote:
> 1. Is it possible to compile and get work together main programme and .so library (both written in Java) with GCJ using CNI or JNI?
It is certainly possible to have a Java program split between a main
binary and .so libraries.
However CNI and JNI are for native methods (implemented in C++ or C
respectively), not when both parts are written in Java.
> 2. I have the following code:
>
> public class Test {
> static {
> System.loadLibrary("Test");
> }
>
> public native void hello();
>
> public static void main(String[] args) {
> new Test().hello();
> }
> }
>
> I tried to compile it with "gcj Test.java --main=Test -o Test.bin". I got:
>
> /tmp/ccWZkOjd.o:(.data+0x50): undefined reference to `hidden alias for void Test::hello()'
> /tmp/ccWZkOjd.o:(.data+0xc8): undefined reference to `hidden alias for void Test::hello()'
>
> I did not use "-fjni" so CNI should have been set. Where is the problem, please? Thank you.
For CNI, you need to tell the linker to link against the library
(static or shared) that contains the C++ implementation of hello().
For example, if hello() is in "libTest.so":
gcj Test.java --main=Test -o Test.bin -lTest
You can do the same thing is libTest.so was a library containing Java
code. However, you can't split a single class across different
libraries or implement a "native" method in Java.
Bryce