Linking .jar.so

Benjamin de Dardel benjamin.dedardel@gmail.com
Fri Jul 11 19:43:00 GMT 2008


Hi David,

Thanks for your response : I can compile my tests now.
But I have some errors in the execution.
I don't know what is missing ?

 >> the first one, without -findirect-dispatch option :
- cpp
org::apache::tools::ant::Main * pMain = new org::apache::tools::ant::Main();

$ gcj -shared -fjni -fPIC -Wl,-Bsymbolic /opt/ant/lib/ant-launcher.jar 
-o ant-launcher.jar.so
$ gcj -shared --classpath=/opt/ant/lib/ant-launcher.jar -fjni -fPIC 
-Wl,-Bsymbolic /opt/ant/lib/ant.jar -o ant.jar.so
...
$ ./test.bin
Unhandled Java exception:
java.lang.NoClassDefFoundError: org.apache.tools.ant.Main
   at java.lang.Class.initializeClass(natClass.cc:727)
Caused by: java.lang.NullPointerException
   at java.lang.Class.initializeClass(natClass.cc:717)

 >> the second one, by instrospection :
- cpp
String *className = JvNewStringLatin1("org.apache.tools.ant.Main");
Class::forName( className );

$ gcj -shared -findirect-dispatch -fjni -fPIC -Wl,-Bsymbolic 
/opt/ant/lib/ant.jar -o ant.jar.so
$ gcj -shared -findirect-dispatch -fjni -fPIC -Wl,-Bsymbolic 
/opt/ant/lib/ant-launcher.jar -o ant-launcher.jar.so
...
$ ./test.bin
Hello from C++
Unhandled Java exception:
java.lang.ClassNotFoundException: org::apache::tools::ant::Main
   at java.lang.Class.forName(natClass.cc:101)
   at java.lang.Class.forName(natClass.cc:125)

David Daney a écrit :
>
> Benjamin de Dardel wrote:
>> Hi all,
>>
>> I'm trying to convert .jar to .so and then linking it with a .cpp 
>> program.
>> Here are the steps I did.  I fail to link .o and .so ! May be you 
>> have an idea to solve this problem ?
>>
>> Thanks,
>> Benjamin
>>
>>  >> ERROR
>> $ gcj -o essai3.bin essai3.o -L . -lgcj -lstdc++ ant.jar.so 
>> ant-launcher.jar.so
>> test.o: In function `main':
>> test.cpp:22: undefined reference to `void 
>> org::apache::tools::ant::Main::main(JArray<java::lang::String*>*)'
>> collect2: ld returned 1 exit status
>>
>>  >> STEPS
>> $ gcj -shared -findirect-dispatch -fjni -fPIC -Wl,-Bsymbolic 
>> /opt/ant/lib/ant.jar -o ant.jar.so
>> $ gcj -shared -findirect-dispatch -fjni -fPIC -Wl,-Bsymbolic 
>> /opt/ant/lib/ant-launcher.jar -o ant-launcher.jar.so
>> $ gcjh -v -cni -classpath 
>> /opt/ant/lib/ant.jar:/opt/ant/lib/ant-launcher.jar:/usr/lib/jvm/java-6-sun/jre/lib/rt.jar 
>> org.apache.tools.ant.Main
>> $ g++ -c -g -O -I. test.cpp
>> $ gcj -o essai3.bin essai3.o -L . -lgcj -lstdc++ ant.jar.so 
>> ant-launcher.jar.so
>>
>>  >> Program : test.cpp
>> #include <gcj/cni.h>
>> #include <java/lang/System.h>
>> #include <java/io/PrintStream.h>
>> #include <java/lang/Throwable.h>
>>
>> #include <org/apache/tools/ant/Main.h>
>>
>> int main(int argc, char *argv[])
>> {
>>    using namespace java::lang;
>>
>>    try
>>    {
>>         JvCreateJavaVM(NULL);
>>         JvAttachCurrentThread(NULL, NULL);
>>
>>         String *message = JvNewStringLatin1("Hello from C++");
>>         JvInitClass(&System::class$);
>>         System::out->println(message);
>>              org::apache::tools::ant::Main::main(NULL);
>>        
>
> You cannot use  -findirect-dispatch with CNI like this.
>
> You might be able to use reflection to invoke main, or don't compile 
> the main class with -findirect-dispatch.
>
>
>>         JvDetachCurrentThread();
>>    }
>>    catch (Throwable *t)
>>    {
>>         System::err->println(JvNewStringLatin1("Unhandled Java 
>> exception:"));
>>         t->printStackTrace();
>>    }
>> }
>
>
>




More information about the Java mailing list