0
votes

I m trying to call java class methods from c with JNI.I have made a simple hello_world.java and hello_world.c and a Makefile while executing make command i am getting following errors . please help me out

make gcc hello_world.c -o hello_world \ -L /usr/java/latest/jre/lib/i386/server/ \ -ljvm \ -I /usr/java/latest/include/ \ -I /usr/java/latest/include/linux/ \ hello_world.c /usr/bin/ld: skipping incompatible /usr/java/latest/jre/lib/i386/server//libjvm.so when searching for -ljvm /usr/bin/ld: cannot find -ljvm collect2: ld returned 1 exit status make: *** [hello_world] Error 1

public class hello_world{
  public static void main(String[] args){
    System.out.println("Hello, World");
  }
  public static int square(int input){
    int output = input * input;
    return output;
  }
  public static int power(int input, int exponent){
    int output,i;
    output=1;
    for(i=0;i<exponent;i++){
      output *= input;
    }
    return output;
  }
}
#include <stdio.h>
#include <jni.h>

JNIEnv* create_vm(JavaVM **jvm)
{
    JNIEnv* env;
    JavaVMInitArgs args;
    JavaVMOption options;
    args.version = JNI_VERSION_1_6;
    args.nOptions = 1;
    options.optionString = "-Djava.class.path=./";
    args.options = &options;
    args.ignoreUnrecognized = 0;
    int rv;
    rv = JNI_CreateJavaVM(jvm, (void**)&env, &args);
    if (rv < 0 || !env)
        printf("Unable to Launch JVM %d\n",rv);
    else
        printf("Launched JVM! :)\n");
    return env;
}

void invoke_class(JNIEnv* env)
{
    jclass hello_world_class;
    jmethodID main_method;
    jmethodID square_method;
    jmethodID power_method;
    jint number=20;
    jint exponent=3;
    hello_world_class = (*env)->FindClass(env, "helloWorld");
    main_method = (*env)->GetStaticMethodID(env, hello_world_class, "main", "([Ljava/lang/String;)V");
    square_method = (*env)->GetStaticMethodID(env, hello_world_class, "square", "(I)I");
    power_method = (*env)->GetStaticMethodID(env, hello_world_class, "power", "(II)I");
    (*env)->CallStaticVoidMethod(env, hello_world_class, main_method, NULL);
    printf("%d squared is %d\n", number,
        (*env)->CallStaticIntMethod(env, hello_world_class, square_method, number));
    printf("%d raised to the %d power is %d\n", number, exponent,
        (*env)->CallStaticIntMethod(env, hello_world_class, power_method, number, exponent));
}

int main(int argc, char **argv)
{
    JavaVM *jvm;
    JNIEnv *env;
    env = create_vm(&jvm);
    if(env == NULL)
        return 1;
    invoke_class(env);
    return 0;
}
all:run

hello_world.class:hello_world.java
        /usr/java/latest/bin/javac hello_world.java

hello_world: hello_world.c
        gcc -o hello_world \
        -L /usr/java/latest/jre/lib/i386/server/ \
        -ljvm \
        -I /usr/java/latest/include/ \
        -I /usr/java/latest/include/linux/ \
        hello_world.c

run: hello_world.class hello_world
        export LD_LIBRARY_PATH="/usr/java/latest/jre/lib/i386/server/"
        ./hello_world

clean:
        rm -f hello_world.class hello_world
1

1 Answers

2
votes

-ljvm will be transform by GCC to libjvm.so and it try find it into the -L paths. try to look into you folder if you find the good .so