Bug 8685 - CNI Invocation of Java code from C++ example fails
Summary: CNI Invocation of Java code from C++ example fails
Status: ASSIGNED
Alias: None
Product: gcc
Classification: Unclassified
Component: libgcj (show other bugs)
Version: 3.3
: P3 normal
Target Milestone: ---
Assignee: Tom Tromey
URL:
Keywords: documentation
Depends on:
Blocks:
 
Reported: 2002-11-22 17:16 UTC by tjmather
Modified: 2005-09-16 00:56 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2005-09-16 00:56:21


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description tjmather 2002-11-22 17:16:01 UTC
// output from gdb

(gdb) run
Starting program: /home/tjmather/poifilter-0.01/a.out

Program received signal SIGSEGV, Segmentation fault.
0x4094fa73 in strlen () from /lib/i686/libc.so.6
(gdb) bt
#0  0x4094fa73 in strlen () from /lib/i686/libc.so.6
#1  0x403a86f4 in java::lang::Runtime::insertSystemProperties(java::util::Properties*) (
    newprops=0x406582e0) at gcj/cni.h:64
#2  0x403bec47 in java.lang.Runtime.__U3c_clinit__U3e_() () at java/lang/Runtime.java:111
#3  0x403a2239 in java::lang::Class::initializeClass() (this=0x406582e0) at java/lang/natClass.cc:786
#4  0x40546fac in _Jv_InitClass (klass=0x0) at java/lang/Class.h:257
#5  0x403c73b8 in java.lang.System.__U3c_clinit__U3e_() () at java/lang/System.java:82
#6  0x403a2239 in java::lang::Class::initializeClass() (this=0x8049c40) at java/lang/natClass.cc:786
#7  0x40546fac in _Jv_InitClass (klass=0x0) at java/lang/Class.h:257
#8  0x403c6ca8 in java.lang.System.getProperty(java.lang.String) (key=0x1) at java/lang/System.java:366
#9  0x403c9f42 in java.lang.Throwable.__U3c_clinit__U3e_() () at java/lang/Throwable.java:403
#10 0x403a2239 in java::lang::Class::initializeClass() (this=0x8049c40) at java/lang/natClass.cc:786
#11 0x403a2275 in java::lang::Class::initializeClass() (this=0x8049c40) at java/lang/Class.h:257
#12 0x403a2275 in java::lang::Class::initializeClass() (this=0x8049c40) at java/lang/Class.h:257
#13 0x403a2275 in java::lang::Class::initializeClass() (this=0x8049c40) at java/lang/Class.h:257
#14 0x403829f1 in _Jv_AllocObjectNoFinalizer (klass=0x8049c40, size=20) at java/lang/Class.h:257
#15 0x40382a2c in _Jv_AllocObject (klass=0x1, size=0) at prims.cc:394
#16 0x40383923 in _Jv_CreateJavaVM(void*) () at prims.cc:916
#17 0x08048917 in JvCreateJavaVM(void*) ()
#18 0x08048820 in main ()
#19 0x408f3082 in __libc_start_main () from /lib/i686/libc.so.6

Release:
gcc version 3.3 20021120 (experimental)

Environment:
Linux 2.4.18 (Mandrake 9.0rc3)

How-To-Repeat:
Use latest GCC/libgcj CVS, then run (see below for test.cc)

$ c++ test.cc -lgcj
$ ./a.out
Segmentation fault

// test.cc
#include <gcj/cni.h>
#include <java/lang/System.h>
#include <java/io/PrintStream.h>
#include <java/lang/Throwable.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);

    JvDetachCurrentThread();
  }
  catch (Throwable *t)
  {
    System::err->println(JvNewStringLatin1("Unhandled Java exception:"));
    t->printStackTrace();
  }
}
Comment 1 Tom Tromey 2002-12-02 20:07:39 UTC
Responsible-Changed-From-To: unassigned->tromey
Responsible-Changed-Why: I'm handling this.
Comment 2 Tom Tromey 2002-12-02 20:07:39 UTC
State-Changed-From-To: open->analyzed
State-Changed-Why: The problem is that the executable name isn't set.
    One workaround is to call _Jv_ThisExecutable before calling
    JvCreateJavaVM.  Any argument is fine, though argv[0] is best.
    
    This is still bad, though, since that isn't really part
    of the API.  I'm not sure what we should do here.
    I'll ask on-list.
Comment 3 Andrew Pinski 2003-11-29 03:13:25 UTC
Can still happen on the mainline but only on targets that do not have /proc/${PID}/exe like darwin 
(for some reason it does not).