Couldn't find "Hello.so" - java

I need to integrate C++ code with Java code using JNI. I do have the NativeCallHandler.java which declares the interface i need to call. I also got the Android.mk, and LOCAL_MODULE is set to Hello.
But I got error saying that it couldn't find 'Hello.so'. I don't know what I did is not correct.
Could somebody help me out? Thanks in advance!

set "LD_LIBRARY_PATH" before run program,
export LD_LIBRARY_PATH=/path/to/so
follow my java-native-interface-jni-tutorial if necessary
NOTE:HelloWorld.java, HelloWorld.h, HelloWord.c,libHelloWorld.so are in same directory
javac HelloWorld.java -> HelloWorld.class
javah -jni HelloWord -> HelloWorld.h
implement HelloWord.c
gcc -fpic -shared HelloWorld.c -o libHelloWorld.so
export LD_LIBRARY_PATH=`pwd`
java HelloWorld

Related

How to Create a Makefile to comile a C file and Java File Separately

I am new to creating makefiles with java files and c files together. I am trying to just have the two files compiled separately by using one makefile. The clean commands don't seem to be an issue, but the below code only compiles the C file.
What am I missing?
#target: dependencies
helloWorld: helloWord.c
g++ -std=c++0x helloWorld.c -o helloWorld
hi.class: hi.java
javac hi.java
clean:
rm helloWorld
rm hi.class
By default, it will only build the first target. Option 1, add an "all" before helloWorld like (also, there's a typo on your version of helloWorld).
all: helloWorld hi.class
helloWorld: helloWorld.c
g++ -std=c++0x helloWorld.c -o helloWorld
Or, you can explicitly run your existing target
make hi.class

Generating Java header file error

Sorry for asking but I am having trouble generating a Java header file from my code - in command prompt I typed:
javah -o JNIDemoJava.h -classpath JNIDemoJava/build/classes jnidemojava.Main
From what I've read that should have worked but I get this error:
Error: Could not find class file for 'jnidemojava.Main'.
Before you say I forgot to clean and build to compile it I did, but I still have this error if you need more info just ask .
Update: Problem was solved
It seems your command is right. In which platform you run your command? Be cautious to the file separator, it's \ in Windows and / in *NIX.
So in Windows you should
javah -o JNIDemoJava.h -classpath JNIDemoJava\build\classes jnidemojava.Main
In *NIX, you should
javah -o JNIDemoJava.h -classpath JNIDemoJava/build/classes jnidemojava.Main

Calling Haskell from Java, dynamic linking error Relocation

I'm having troubles with compiling a standalone library for use by Java (with C++ inbetween).
There is a program in Haskell exporting one function that processes some text and returns it.
The program in Haskell needs some external data (binary file). I'm 'compiling it in' with the help of file-embed package.
When I started the compilation with:
$ ghc -fPIC -dynamic -c -O --make MyModule.hs
It throws the error:
MyModule.hs:239:15:
Dynamic linking required, but this is a non-standard build (eg. prof).
You need to build the program twice: once the normal way, and then
in the desired way using -osuf to set the object file suffix.
This is the place where I use file-embed.
So I tried the proposed approach (compiling twice, changing suffixes):
$ ghc -fPIC -c -O --make MyModule.hs
$ ghc -osuf d.o -fPIC -dynamic -c -O --make MyModule.hs
$ javac -cp javacpp.jar MyModule.java
$java -jar javacpp.jar -Dcompiler.path=ghc -Dcompiler.output.prefix="-optc-O3 -Wall MyModule.d.o -dynamic -fPIC -shared -lstdc++ -lHSrts-ghc7.6.3 -o " -Dcompiler.linkpath.prefix2="-optl -Wl,-rpath," MyModule
And now I'm getting an error that I don't understand:
/usr/bin/ld: MyModule.d.o: relocation R_X86_64_PC32 against undefined symbol `{Directory_with_code}zi{Some_module}_{Some_module}_con_info' can not be used when making a shared object; recompile with -fPIC
Can anyone explain it to me and give some tips on how to solve it?
Just a guess, but maybe you need to different version of HSrts-ghc-7.6.3 since this is a "non-standard" build.

Compiling SWIG java library with mingw

I've got a Java project which depends on a native library. I'm using SWIG to build the native library, which works fine on *nix systems. I'm trying to get it to compile now on Windows using the mingw 64 bit toolchain and SWIG. It will compile and I can get the jnilib packaged appropriately (i.e. the same way as I'm doing it on other platforms) but when I try to load the library in the Java code I get:
Exception in thread "main" java.lang.UnsatisfiedLinkError: ... .jnilib: Can't find dependent libraries
and then it has the trace on the java side. Clearly I'm not linking something properly but I'm not sure what. Here's the relevant part of my compile commands:
swig -java -package cStopPow -c++ StopPow.i
x86_64-w64-mingw32-g++ -I"C:/Progra~1/Java/jdk1.7.0_25/include/" -I"C:/Progra~1/Java/jdk1.7.0_25/include/win32/" -std=c+
+11 -O3 -I"C:/MinGW/include/boost/math/" -c ../src/StopPow.cpp ../src/StopPow_SRIM.cpp ../src/StopPow_LP.cpp ../src/Stop
Pow_BetheBloch.cpp ../src/StopPow_AZ.cpp ../src/PlotGen.cpp ../src/AtomicData.cpp StopPow_wrap.cxx
x86_64-w64-mingw32-g++ -shared -lmingw32 -mconsole StopPow_wrap.o StopPow.o StopPow_SRIM.o StopPow_LP.o StopPow_BetheBlo
ch.o StopPow_AZ.o PlotGen.o AtomicData.o -o libcStopPow.jnilib
Why the output is libcStopPow.jnilib? I think it must a dll file
I use SWIG 2.0.10, Oracle JDK 1.7.0_40 and mingw 64 from http://nuwen.net/ with the Examples\java\simple. I do the following in cmd (I set JAVA_HOME correctly)
swig -java -c++ example.i
g++ -Wall -Wl,--kill-at -I "%JAVA_HOME%/include" -I "%JAVA_HOME%/include/win32" -shared -s -o example.dll *.c *.cxx
java -cp . runme
It works as expected

SWIG CYGWIN DLL linking

I'm trying to follow the SWIG Java example located here. This example uses cygwin for compilation. I would like to pass the compiler an alredy compiled dll, test.dll, instead of a C source file. The SWIG.org example uses C source. I attempted to pass the test.dll(compiled C source) and then example.dll to the 3rd command but I get an error "test.dll: no such file or directory". The SWIG.org example's 3rd command creates the example.dll based on example.c. How can I create example.dll so that it uses test.dll instead of example.c?
Is there a way to accomplish this within the context of this example?
My Attempt without C Source, just dll:
$ swig -java example.i
$ gcc example_wrap.c -I/c/jdk1.6.0_30/include -I/c/jdk1.6.0_30/include/win32
$ gcc -shared example_wrap.o -mno-cygwin -Wl,--add-stdcall-alias -o test.dll example.dll
SWIG.org Example Code:
$ swig -java example.i
$ gcc **-c example.c** example_wrap.c -I/c/jdk1.3.1/include -I/c/jdk1.3.1/include/win32
$ gcc -shared example.o example_wrap.o -mno-cygwin -Wl,--add-stdcall-alias -o example.dll
You need to pass test.dll to the last command, which links your program, rather than the second, which just compiles the example_wrap.c source file. (The -c option tells gcc to compile only.)
Note that the ordering of objects and DLLs on the link line is important. DLLs should come after anything that uses them.

Categories