C++ dynamic linking and java 
Author Message
 C++ dynamic linking and java

 Hi all,

 I got stuck with the following rather tricky situation, and hope someone
can
 provide me a solution.

 I need to use some native c++ functions in Java. As a toy example, I wrote
 the following code

 ------------------------------------------------------------
 class QueryTest{
     public native void test();
     public static void main(String[] args) {
  new QueryTest().test();
      }
     static {
  System.loadLibrary("QueryTest");
     }
 }
 ------------------------------------------------------------

 Following the usual procedure, I obtained the file QueryTest.h from JNI and
 wrote the file QueryTest.cpp. Now, the problem is that within the
 Java_QueryTest_test function, I call some C++ procedures that do dynamic
 linking in the linux operating environment. Just as an example, I tried to
 simulate the situation as follows:

 ----------------------------------------------------------------------
 #include <stdio.h>
 #include <iostream.h>
 #include "QueryTest.h"
 #include <dlfcn.h>
 #include <stdlib.h>

 JNIEXPORT void JNICALL Java_QueryTest_test (JNIEnv *, jobject){

   void *dl_handle;

   char* command_line = "ld -shared lib/patterns.o lib/libsift.a -o
 /tmp/libtest.so";

   if (system(command_line))
        perror("system");
   else
     {
       cerr << "succesfully performed the system command!" << endl << endl;
   if ((dl_handle = dlopen("/tmp/libtest.so", RTLD_LAZY)) != NULL)
     {
       cerr << "succesfully opened the library /tmp/libtest.so" << endl;
       dlclose(dl_handle);
     }
   else {
     cerr << "Unsuccesful loading!" << endl << endl;
     perror("dlopen");
   }
    }

   return;

 }
 ---------------------------------------------------------------------------
-

 At a given point, a system command (linking a given object code) needs to
be
 issued, and the resulting file is opened as a dynamic link library.

 The call to "dlopen" does not actually work as it should, answering the
 following message:

 --------------------------


 succesfully performed the system command!

 Unsuccesful loading!

 dlopen: No child processes

 ----------------------

 Everything works fine if I don't try to use such functionalities within
 java: that is, if I try to use the above code in a C++ executable file,
 dlopen does not give an error.

 Can anybody give me some reference to solve the above issues?

 Regards

Quote:
> GM



Tue, 23 Nov 2004 22:41:57 GMT  
 C++ dynamic linking and java


Quote:

>  Hi all,

>  I got stuck with the following rather tricky situation, and hope someone
> can
>  provide me a solution.

>  I need to use some native c++ functions in Java. As a toy example, I
wrote
>  the following code

>  ------------------------------------------------------------
>  class QueryTest{
>      public native void test();
>      public static void main(String[] args) {
>   new QueryTest().test();
>       }
>      static {
>   System.loadLibrary("QueryTest");
>      }
>  }
>  ------------------------------------------------------------

>  Following the usual procedure, I obtained the file QueryTest.h from JNI
and
>  wrote the file QueryTest.cpp. Now, the problem is that within the
>  Java_QueryTest_test function, I call some C++ procedures that do dynamic
>  linking in the linux operating environment. Just as an example, I tried
to
>  simulate the situation as follows:

>  ----------------------------------------------------------------------
>  #include <stdio.h>
>  #include <iostream.h>
>  #include "QueryTest.h"
>  #include <dlfcn.h>
>  #include <stdlib.h>

>  JNIEXPORT void JNICALL Java_QueryTest_test (JNIEnv *, jobject){

>    void *dl_handle;

>    char* command_line = "ld -shared lib/patterns.o lib/libsift.a -o
>  /tmp/libtest.so";

>    if (system(command_line))
>         perror("system");
>    else
>      {
>        cerr << "succesfully performed the system command!" << endl <<
endl;
>    if ((dl_handle = dlopen("/tmp/libtest.so", RTLD_LAZY)) != NULL)
>      {
>        cerr << "succesfully opened the library /tmp/libtest.so" << endl;
>        dlclose(dl_handle);
>      }
>    else {
>      cerr << "Unsuccesful loading!" << endl << endl;
>      perror("dlopen");
>    }
>     }

>    return;

>  }
>  -------------------------------------------------------------------------
--
> -

>  At a given point, a system command (linking a given object code) needs to
> be
>  issued, and the resulting file is opened as a dynamic link library.

>  The call to "dlopen" does not actually work as it should, answering the
>  following message:

>  --------------------------


>  succesfully performed the system command!

>  Unsuccesful loading!

>  dlopen: No child processes

>  ----------------------

>  Everything works fine if I don't try to use such functionalities within
>  java: that is, if I try to use the above code in a C++ executable file,
>  dlopen does not give an error.

>  Can anybody give me some reference to solve the above issues?

>  Regards

> > GM

Is your libtest.so in the path of the Java program (i.e. can the Java
program see it at all)?


Tue, 23 Nov 2004 22:48:22 GMT  
 C++ dynamic linking and java

Quote:

>  I got stuck with the following rather tricky situation, and hope
>  someone can provide me a solution.

>  I need to use some native c++ functions in Java. As a toy example,
>  I wrote the following code

In case you hadn't noticed, the name of the newsgroup is
"comp.lang.c".  There is no java, no ++, etc. in there.  

--

   Available for consulting/temporary embedded and systems.
   <http://cbfalconer.home.att.net>  USE worldnet address!



Wed, 24 Nov 2004 03:41:18 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. ;-) INFO TO LINKS ON C,C++,JAVA.

2. Java dll linked to C++

3. dynamic linked list + dynamic struct members

4. Call for Code!Intro Java;COM/DCOM in C++/Java

5. dynamic link to static link...

6. dynamic link -> static link

7. Dynamic arrays not really dynamic in managec C++?

8. Need help of C(/C++) programmer: for porting a tiny Java benchmark test to C(/C++)

9. Update: GC in C++ and Java;SF Bay Area C++,Sat,Mar 8

10. VC++ 4 and Java linking problems

11. MSDN Java links broken?

12. link Java

 

 
Powered by phpBB® Forum Software