Linking C, Fortran to be Called by Java

Andrew Stone astone@brightlane.com
Wed May 22 12:53:00 GMT 2002


Hello all,

First off, I apologize for the length of this email, but I figured the more
information the better...


I'm trying get Java to communicate to Fortran via C.  I have been googling
for the past few days and now feel like I'm running myself around in circles
trying to do -basically- a proof of concept.

I could be way off, but I think I'm missing/misusing the compiler options
needed..

Here's what I have:
gcc version 2.95.2 19991024 (release)
g77 version 2.95.2 19991024 (release) (from FSF-g77 version 0.5.25 19991024
(release))

My Java source (jniCheck.java):

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

public class jniCheck
{

  public double calcCheck(double hours, double rate, double net)
  {
    c_calculate(hours,rate,net);
    return net;
  }

  public static void main(String [] args)
  {
    double hours = 12;
    double rate = 34.50;
    double net  = 0;

    System.out.println("1 hours: "+ hours);
    System.out.println("1 rate: "+ rate);
    System.out.println("1 net: "+ net);

    jniCheck testJni = new jniCheck();

    testJni.calcCheck(hours,rate,net);
    System.out.println("2 hours: "+ hours);
    System.out.println("2 rate: "+ rate);
    System.out.println("2 net: "+ net);
  }
  // native method declaration
  public native void c_calculate(double hours, double rate, double net );

  // load library
  static { System.loadLibrary("jniCheck");}
}
// end java source

my C source (jniCheck.cpp):

#include <jni.h>
#include <stdio.h>
#include <string.h>
#include "jniCheck.h"

extern "C" void f_calculate__(double* hours, double* rate, double* net);

JNIEXPORT void JNICALL Java_c_calculate(JNIEnv *env, jobject obj, jdouble
hours, jdouble rate, jdouble net)
{
  f_calculate__(&hours, &rate, &net);
};

int main(){
 //empty
}

//end c source

my fortran source (CALCULATE.FOR):
      SUBROUTINE F_CALCULATE(HOURS,RATE,NET)
      IMPLICIT REAL*8 (A-Z)

      NET= HOURS * RATE

      RETURN
      END
*end fortran source

build script:

echo Compiling jniCheck.java ...
javac jniCheck.java

echo Creating Header file from jniCheck.class ...
javah -classpath . -jni jniCheck

echo Compiling Fortan to produce object file...
g77 -c CALCULATE.FOR

echo Compiling jniCheck.cpp to create shared object...
gcc -o libjniCheck.so -I/usr/java/include -I/usr/java/include/solaris
jniCheck.cpp CALCULATE.o -lg2c -lm

echo

echo Done!

Everything works (at least i don't get any errors), then when i enter the
command:

java -classpath . jniCheck

I get the following error:

Exception in thread "main" java.lang.UnsatisfiedLinkError:
/home/astone/projects/DSI/web/libjniCheck.so: ld.so.1:
/usr/bin/../java/bin/../bin/sparc/native_threads/java: fatal:
/home/astone/projects/DSI/web/libjniCheck.so: required mapping 0x10000 size
0x12000, is already in use by file
/usr/bin/../java/bin/../bin/sparc/native_threads/java
        at java.lang.ClassLoader$NativeLibrary.load(Native Method)
        at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1382)
        at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1306)
        at java.lang.Runtime.loadLibrary0(Runtime.java:749)
        at java.lang.System.loadLibrary(System.java:820)
        at jniCheck.<clinit>(jniCheck.java:34

Any help would be great appreciated.

thank you for you time,
andy




More information about the Gcc-help mailing list