Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4171442

call to java_main when libjava.so is loaded dynamically dumps core



    • Subcomponent:
    • CPU:
    • OS:


      Name: diC59631 Date: 09/04/98


      At Oracle, we have a product which invokes the appletviewer from a C program, with valid HTML file, which in-turn invokes an Applet.

      The C program creates a new thread. This thread loads the java library libjava.so (javai.dll on Win32), locates the function java_main. It then executes a call to this function with the following arguments (name testjni.html chosen for simplicity).

      'sun.applet.AppletViewer testjni.html'

      testjni.html is a generated HTML file which has the information to invoke a custom applet. This applet communicates with the C program using JNI to run the application.

      This works fine in win32 environment.


       On Solaris, this does not work. The appletviewer exits with a segmentation violation. However, if the java library is linked with the C program it invokes the appletviewer properly and the client applet runs as part of the program.

       Our requirement is to load the JDK dynamically.


        1. JAVAHOME environment variable is set to the local JDK installation, which in my case is '/local/java/solaris/jdk/1_1_4'

        2. I am using green-threads by default

        3. I am not using threads in this example for simplicity

        4. Please forward any work around and alternatives directly to me. Thanks

      Information to repeat the bug:

       I have a created a simple test case with a C program (test.c), Java file (TestJNI.java) and HTML file (testjni.html). These are attached with this bug description.

       Steps to produce the bug:

        1. compile the C file with the following command line

         solaris> cc test.c -DLOAD_DYNAMIC -ldl -o testjni

        2. copy the TestJNI.java file in to the same directory

         solaris> javac TestJNI.java

        3. copy the jnicall.html to the same directory

        4. Execute the C programe

          solaris> testjni

        Result: notice that appletviewer exits with a core dump.

       Files: Total 3

       1. test.c
       2. TestJNI.java
       3. jnicall.html

       #include <stdio.h>
      #include <dlfcn.h>

      #define LIBPATH ((const char*)"LD_LIBRARY_PATH")
      #define JAVALIB ((const char*)"libjava.so")
      #define NUMARGS (3)

      int main(int argc, char **argv)
       int ldpathlen;
       void *handle;
       void (*javamain)(int , char **);
       char *libpath, *javahome, *ldpath;
       char **jargv;

       printf("Fetching %s...\n", LIBPATH);
       libpath = getenv(LIBPATH);
       printf("%s : %s\n", LIBPATH, libpath);

       printf("Fetching JAVAHOME...\n");
       javahome = getenv("JAVAHOME");
       printf("JAVAHOME : %s\n", javahome);

       ldpathlen = strlen(LIBPATH) + strlen(javahome) + 3 +
                   strlen("/lib/sparc/green_threads/") + strlen(":") + strlen(libpath);
       ldpath = (char *)malloc(ldpathlen);
       sprintf(ldpath, "%s=%s:%s/lib/sparc/green_threads", LIBPATH, libpath, javahome);
       if ( putenv(ldpath) != 0 )
        printf("Error in setting %s\n", LIBPATH);
       printf("Setting %s : %s\n", LIBPATH, getenv(LIBPATH));

      #ifdef LOAD_DYNAMIC
       printf("Loading the %s...\n", JAVALIB);
       handle = dlopen("libjava.so", RTLD_NOW);
       if ( handle )
        printf("Loaded the libjava.so...\n");
        printf("Error in loading libjava.so...\n");
        printf("Error : %s\n", dlerror());
        return -1;

       printf("Finding the java_main...\n");
       javamain = dlsym( handle, "java_main");
       if ( javamain )
        printf("Found the java_main...\n");
        printf("Error in finding java_main...\n");
        return -1;

       printf("Calling java_main...\n");
       jargv = (char **) malloc( sizeof(char *) * NUMARGS);
       jargv[0] = (char *)malloc( strlen("java") + 1);
       strcpy( jargv[0], "java");
       jargv[1] = (char *)malloc( strlen("sun.applet.AppletViewer") + 1);
       strcpy( jargv[1], "sun.applet.AppletViewer");
       jargv[2] = (char *)malloc( strlen("jnicall.html") + 1);
       strcpy( jargv[2], "jnicall.html");

      #ifdef LOAD_DYNAMIC
       (*javamain)(NUMARGS, jargv);

      #ifndef LOAD_DYNAMIC
       java_main(NUMARGS, jargv);

       printf("Returned from java_main...\n");

      #ifdef LOAD_DYNAMIC

      import java.applet.Applet;
      import java.awt.*;

      public class TestJNI extends Applet
       public void init()
          System.out.println("Init in the applet");

       public void paint(Graphics g)
         g.drawString("Hello world", 20, 20);

      <applet code=TestJNI width = 200 height = 200>

      Error message:
      SIGSEGV 11* segmentation violation
          si_signo [11]: SIGSEGV 11* segmentation violation
          si_errno [0]: Error 0
          si_code [1]: SEGV_MAPERR [addr: 0x0]
              stackbase=EFFF9858, stackpointer=EFFF9598
      Full thread dump:
          "Finalizer thread" (TID:0xee300220, sys_thread_t:0xef3a0de0, state:R) prio=1
          "Async Garbage Collector" (TID:0xee3001d8, sys_thread_t:0xef3d0de0, state:R)
          "Idle thread" (TID:0xee300190, sys_thread_t:0xef400de0, state:R) prio=0
          "Clock" (TID:0xee3000d0, sys_thread_t:0xef430de0, state:CW) prio=12
          "main" (TID:0xee3000a8, sys_thread_t:0x379f0, state:R) prio=5 *current threa
      Monitor Cache Dump:
      Registered Monitor Dump:
          Thread queue lock: <unowned>
          Name and type hash table lock: <unowned>
          String intern lock: <unowned>
          JNI pinning lock: <unowned>
          JNI global reference lock: <unowned>
          BinClass lock: <unowned>
          Class loading lock: <unowned>
          Java stack lock: <unowned>
          Code rewrite lock: <unowned>
          Heap lock: <unowned>
          Has finalization queue lock: <unowned>
          Finalize me queue lock: <unowned>
          Monitor IO lock: owner "main" (0x379f0, 1 entry)
          Child death monitor: <unowned>
          Event monitor: <unowned>
          I/O monitor: <unowned>
          Alarm monitor: <unowned>
              Waiting to be notified:
          Sbrk lock: <unowned>
          Monitor cache expansion lock: <unowned>
          Monitor registry: owner "main" (0x379f0, 1 entry)
      Thread Alarm Q:
      Abort (core dumped)
      (Review ID: 38061)




            apalanissunw Anand Palaniswamy (Inactive)
            dindrigo Daniel Indrigo (Inactive)
            0 Vote for this issue
            1 Start watching this issue