calling java from c++

akos.szalay@sophos.com akos.szalay@sophos.com
Wed Mar 27 04:44:00 GMT 2002


I'm trying to call java from c++ using gcj.
I thought, that the following code should work, but it coredumps
(segmentation fault) on new.
Any idea what's wrong with it ? (any workaround for this problem ?)
I'm running redhat linux 7.1

A'kos


++++++++++++javatest.cpp+++++++++++++

#include <gcj/cni.h>
#include "otherclass.h"

#include <stdio.h>

int main()
{
  printf("Starting...\n");

  otherclass *ttt = new otherclass();

  printf("Otherclass created (%p)\n",ttt);
  printf("Calling otherclass...\n" );
  ttt->fn();
  printf("...And back again, OK\n" );
}

++++++++++++++otherclass.java++++++++++

import java.io.*;
import java.util.*;

public class otherclass
{
    public void fn()
    {
       System.out.println("otherclass running" );
    }

    public otherclass()
    {
      System.out.println("otherclass constructed");
    }

}

+++++++++++++otherclass.h++++++++++++++

#ifndef __otherclass__
#define __otherclass__

#pragma interface

#include <java/lang/Object.h>

extern "Java"
{
  class otherclass;
};

class ::otherclass : public ::java::lang::Object
{
public:
  virtual void fn ();
  otherclass ();

  static ::java::lang::Class class$;
};

#endif /* __otherclass__ */


++++++++++++++++makefile+++++++++++++++++++


SHELL=/bin/bash
GCJ_HOME=/home/joerg/gcc
PATH=$(GCJ_HOME)/bin:$(GCJ_HOME)/lib:/bin:/usr/bin
LD_LIBRARY_PATH=$(GCJ_HOME)/lib
CLASSPATH=$(GCJ_HOME)/lib:$(GCJ_HOME)/lib/gcc-lib/i686-pc-linux-gnu/3.1

build: javatest

javatest: javatest.cpp otherclass.java
        gcj -o javatest otherclass.java javatest.cpp -lstdc++ -lgcj


clean:
        rm -f javatest

run: javatest
        export LD_LIBRARY_PATH=$(LD_LIBRARY_PATH);./javatest





More information about the Java mailing list