Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

249
Visualizações
Loading a dynamic C shared library using JNI which also loads another shared library

Using JNI on Java eclipse (Linux), I am loading a dynamic shared library called first.so. So far all goes well. The problems being, that first.so also loads a dynamic library called second.so.

When running the program I am getting many "undefined symbol" errors regarding symbols located in second.so.

It seems that a library loaded using JNI cannot load other C libraries on run-time because we are in a Java environment. Is my assumption correct? Do I need special compile flags to compile the first.so library or is it special arguments to tell eclipse that it will be trying to load an .so during runtime?

Thanks in advance!

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

It seems that a library loaded using JNI cannot load other C libraries on run-time because we are in a Java environment. Is my assumption correct?

No.

How is libsecond.so used by libfirst.so? Is it a linked dependency, or is it loaded by dlopen?

I've found that something like:

static {
    System.loadLibrary("second");
    System.loadLibrary("first");
}

in the class that uses the JNI usually works.

EDIT: Now that I know how you are loading libsecond.so this worked for me:

Test.java

public class Test {

    public static void main (String args[]) {
        test();
    }

    private native static void test();

    static {
        System.loadLibrary("first");
    }
} 

first.c -- The sole translation unit of libfirst.so

#include <jni.h>
#include "Test.h"
#include <dlfcn.h>
#define LIBNAME "libsecond.so"

#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     Test
 * Method:    test
 * Signature: ()V
 */
JNIEXPORT void JNICALL
Java_Test_test(JNIEnv *env , jclass cls)
{
  void* h;
  void (*sym)(void);

  h = dlopen(LIBNAME, RTLD_LAZY|RTLD_GLOBAL);
  if (h) {
    printf("dlopen " LIBNAME " worked\n");
    sym = (void (*)(void))dlsym(h,"second");
    sym();
  } else {
    printf("dlopen " LIBNAME " failed\n");
  }
}

#ifdef __cplusplus
}
#endif

second.c -- The sole translation unit of libsecond.so

#include <stdio.h>

void
second(void)
{
  printf("hello from second\n");
}

Makefile

CFLAGS=-fPIC

all : libfirst.so libsecond.so 

libsecond.so : second.o
        $(CC) -shared -Wl,-soname,libsecond.so.0 -o $@ $^ -lc

libfirst.so : first.o
        $(CC) -shared -Wl,-soname,libfirst.so.0 -o $@ $^ -ldl -lc

clean:
        rm -f *.o *.so

Test.h can be produced by javah Test. Note that libfirst.so and libsecond.so are not linked together.

over 4 years ago · Santiago Trujillo Relatório

0

All usual rules apply to dopen() from a library loaded from Java. Particularly, you check your LD_LIBRARY_PATH, rpath, runpath, etc. See also dlopen failed:cannot open shared object file: No such file or directory

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda