public inbox for java@gcc.gnu.org
 help / color / mirror / Atom feed
* Linking .jar.so
@ 2008-07-10 22:15 Benjamin de Dardel
  2008-07-10 22:25 ` David Daney
  0 siblings, 1 reply; 3+ messages in thread
From: Benjamin de Dardel @ 2008-07-10 22:15 UTC (permalink / raw)
  To: java

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);
         

         JvDetachCurrentThread();
    }
    catch (Throwable *t)
    {
         System::err->println(JvNewStringLatin1("Unhandled Java 
exception:"));
         t->printStackTrace();
    }
}

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Linking .jar.so
  2008-07-10 22:15 Linking .jar.so Benjamin de Dardel
@ 2008-07-10 22:25 ` David Daney
  2008-07-11 19:43   ` Benjamin de Dardel
  0 siblings, 1 reply; 3+ messages in thread
From: David Daney @ 2008-07-10 22:25 UTC (permalink / raw)
  To: benjamin.de-dardel; +Cc: java

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();
>    }
> }

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Linking .jar.so
  2008-07-10 22:25 ` David Daney
@ 2008-07-11 19:43   ` Benjamin de Dardel
  0 siblings, 0 replies; 3+ messages in thread
From: Benjamin de Dardel @ 2008-07-11 19:43 UTC (permalink / raw)
  To: David Daney, java

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();
>>    }
>> }
>
>
>


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2008-07-11 19:43 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-07-10 22:15 Linking .jar.so Benjamin de Dardel
2008-07-10 22:25 ` David Daney
2008-07-11 19:43   ` Benjamin de Dardel

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).