Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JNI using. "symbol lookup error"

I trying to use md5 shared library with Java Native Interface. Here you can check sources. This library is too large to prepare it for using with JNI. So I want to use a simple accessory library for linking. After build this library while testing I got this output:

  java: symbol lookup error: ./libMd5bridge.so: undefined symbol: _Z3md5Ss

I tried to do LD_PRELOAD but this was no result. So here I posting every my actions step by step.

1) I can call my md5 library in very simple C++ code:

#include <iostream>
#include "md5.h"

int main(int argc, char* argv[]) {
    std::string passedValue;
    for(int i = 1; i < argc; i++)
     passedValue += argv[i];
    std::cout << "MD5 sum is: " << md5(passedValue) <<std::endl;
    return 0;
}

I named my accessory library as Md5bridge. So in next context I will call it so.

2) Md5Bridge.java

public class Md5bridge{
     native public void MD5SUM(String[] passedValue);
}

3) Getting Md5bridge header file:

javac Md5Bridge.java && javah Md5Bridge

Md5bridge.h

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class Md5bridge */

#ifndef _Included_Md5bridge
#define _Included_Md5bridge
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     Md5bridge
 * Method:    MD5SUM
 * Signature: ([Ljava/lang/String;)V
 */
JNIEXPORT void JNICALL Java_Md5bridge_MD5SUM
  (JNIEnv *, jobject, jobjectArray);

#ifdef __cplusplus
}
#endif
#endif

4) Preparing code from step one to using with jni:

#include <iostream>
#include "md5.h"
#include "Md5bridge.h"
JNIEXPORT void JNICALL Java_Md5bridge_MD5SUM
  (JNIEnv *, jobject, jobjectArray) {

    std::string passedValue;
    md5(passedValue);
}

5) Compiling as a shared library:

g++ -shared -fPIC -I /opt/jdk1.6.0_45/include/ -I /opt/jdk1.6.0_45/include/linux -o libMd5bridge.so Md5bridge.cpp

6) Preparing a simple java program for check this library:

   public class Main{
    static{
    System.loadLibrary("Md5bridge");
    }
    public static void main(String[] passedValue){
    new Md5bridge().MD5SUM(passedValue);
    }
    }

This program was compiled with javac and ran with error (I wrote one in head of this post):

java: symbol lookup error: ./libMd5bridge.so: undefined symbol: _Z3md5Ss

I'm not familiar with java and I believe that I did something wrong in one of my steps. Any idea?

UPDATED: After adding extern "C"{} md5 header file (thanks for Chris Stratton) I have this output:

java: symbol lookup error: /home/andrey/Desktop/md5LIB/libMd5bridge.so: undefined symbol: md5

It look like missed link to libmd5.so.

$ ldd libMd5bridge.so 
    linux-vdso.so.1 =>  (0x00007fff38666000)
    libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0x00007f2dfde3a000)
    libm.so.6 => /lib/libm.so.6 (0x00007f2dfdbb7000)
    libgcc_s.so.1 => /lib/libgcc_s.so.1 (0x00007f2dfd99f000)
    libc.so.6 => /lib/libc.so.6 (0x00007f2dfd619000)
    /lib64/ld-linux-x86-64.so.2 (0x00007f2dfe36b000)

How to link both libraries?

like image 384
Andrew Avatar asked Jan 23 '26 02:01

Andrew


1 Answers

Your C implementation must look like:

#include <iostream>
#include "md5.h"
#include "Md5bridge.h"
#ifdef __cplusplus
extern "C" {//pay attention to this!
#endif 
JNIEXPORT void JNICALL Java_Md5bridge_MD5SUM
  (JNIEnv *, jobject, jobjectArray) {

   std::string passedValue;
   md5(passedValue);
}
#ifdef __cplusplus
} //pay attention to this
#endif 

But BETTER! use origin md5 that is already implemented in JDK

Here is snapshot from my java code:

public class MD5Converter {

    private final static String ALGORITHM = "MD5";
    private final static String ENCODING = "UTF-8";

    public static String encryptToMd5(String normal) {
        MessageDigest digest;
        try {
            digest = java.security.MessageDigest.getInstance(ALGORITHM);
            digest.reset();
            digest.update(normal.getBytes(ENCODING));
            byte[] hash = digest.digest();
            return byteToHex(hash); //This is on you res to implement this 
        } catch (NoSuchAlgorithmException e) {
            final String err = "No such digest algorithm \"" + ALGORITHM + "\"";
            log.fatal(err, e);
            throw new IllegalStateException(err, e);
        } catch (UnsupportedEncodingException e) {
            final String err = "Unsupported encoding \"" + ENCODING + "\"";
            log.fatal(err, e);
            throw new IllegalStateException(err, e);
            }
        }
}
like image 142
Dewfy Avatar answered Jan 25 '26 14:01

Dewfy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!