JvInitClass calls exit?

tsuraan tsuraan@gmail.com
Fri Dec 9 15:11:00 GMT 2005


I'm trying to write a simple app in c++ using the java lucene
libraries.  Basically, what the app wants to do it open a lucene
index, and then close it.  The problem I have is that when calling a
static method of a java class from c++, the program fails.  It doesn't
throw an exception, and the function being called never reaches its
first line.  My first thought was that maybe the gcj-compiled lucene
was not properly initializing static classes, so I decided to "fix"
this by explicitly calling JvInitClass on the object's class$
variable.  This causes my program to abort there, instead of when I
call the static method of the class I'm trying to use.  So, since gcj
supposedly inserts JvInitClass transparently at the beginning of each
static method, it's looking like JvInitClass is seeing something bad,
and exiting.  The program's return code is 1, if that helps anything.

I'm using gcc-4.1-20051029 to compile these, and lucene 1.4.3.  I have
a small set of patches to make gcjh and gcj less angry.

When this program is run, it prints 1, 2, and then Bye, so it looks
like exit is being called in the JvInitClass for the FSDirectory.  Any
help in debugging would be greatly appreciated.

The source code for my simple test app follows:

#include <java/io/PrintStream.h>
#include <java/lang/Throwable.h>
#include <java/io/IOException.h>
#include <java/io/File.h>
#include <org/apache/lucene/index/IndexReader.h>
#include <org/apache/lucene/store/Directory.h>
#include <org/apache/lucene/store/FSDirectory.h>

#include <stdlib.h>
#include <cstdio>

using namespace org::apache::lucene::index;
using namespace org::apache::lucene::store;
using namespace java::lang;
using namespace java::io;

void bye() {
  printf("Bye!\n");
}

int main() {
  atexit(bye);
  try {
    JvCreateJavaVM(0);
    JvAttachCurrentThread(0, 0);
    JvInitClass(&System::class$);
    System::out->println(JvNewStringLatin1("1"));

    JvInitClass(&Directory::class$);
    System::out->println(JvNewStringLatin1("2"));

    JvInitClass(&FSDirectory::class$);
    System::out->println(JvNewStringLatin1("3"));

    String* path = JvNewStringLatin1("/home/me/index");
    System::out->println(JvNewStringLatin1("4"));

    File *f = new File(path);
    System::out->println(JvNewStringLatin1("5"));

    jboolean create = false;
    Directory *d = FSDirectory::getDirectory(f, create);
    System::out->println(JvNewStringLatin1("6"));

    IndexReader* reader = IndexReader::open(d);
    System::out->println(JvNewStringLatin1("I did it!"));
    reader->close();
    JvDetachCurrentThread();
  } catch(java::io::IOException *e) {
    printf("foo");
    Throwable *t = (Throwable*)e;
    System::out->println(JvNewStringLatin1("I failed!"));
    System::err->println(t->getMessage());
    t->printStackTrace();
  } catch(...) {
    printf("foo");
    System::out->println(JvNewStringLatin1("anything else"));
  }
  return 0;
};
-------------- next part --------------
A non-text attachment was scrubbed...
Name: lucene.patch
Type: application/octet-stream
Size: 2941 bytes
Desc: not available
URL: <http://gcc.gnu.org/pipermail/java/attachments/20051209/6857a6a9/attachment.obj>


More information about the Java mailing list