Java compilation error : /bin/ld:cannot find -ljvm - java

I am trying to build a java project http://sourceforge.net/projects/fuse-j/?source=typ_redirect on fedora machine. The compilation fails while compiling JNI bindings.
MakeFile
include ../build.conf
include ../jvm_ldpath.def
SRCS := javafs.c javafs_bindings.c
HDRS := javafs.h javafs_bindings.h
LIB_SO := libjavafs.so
INCLUDES := -I${FUSE_HOME}/include -I${JDK_HOME}/include -I${JDK_HOME}/include/linux
LDPATH := ${LDPATH} -L${FUSE_HOME}/lib
all: ${LIB_SO}
${LIB_SO}: ${SRCS} ${HDRS}
gcc -fPIC -shared -D_FILE_OFFSET_BITS=64 -o ${LIB_SO} ${INCLUDES} ${LDPATH} -ljvm -lfuse -lpthread ${SRCS}
clean:
rm -f ${LIB_SO}
Error:
/bin/ld: cannot find -ljvm
collect2: error: ld returned 1 exit status
Makefile:17: recipe for target 'libjavafs.so' failed
make[1]: *** [libjavafs.so] Error
JDK_HOME ,FUSE_HOME are correctly set. Can you suggest what can be wrong here?

The jvm shared library will be found in a path under $JDK_HOME, however, you are not adding that path to your LDPATH make variable. You need to add -L${JDK_HOME}/lib to LDPATH (or wherever libjvm.so is found).

Related

sudo R CMD javareconf solution not working

I am trying to run rJava in RStudio but without success:
Error: package or namespace load failed for ‘rJava’:
.onLoad failed in loadNamespace() for 'rJava', details:
call: dyn.load(file, DLLpath = DLLpath, ...)
error: unable to load shared object '/Library/Frameworks/R.framework/Versions/3.6/Resources/library/rJava/libs/rJava.so':
dlopen(/Library/Frameworks/R.framework/Versions/3.6/Resources/library/rJava/libs/rJava.so, 6): Library not loaded: /Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk/Contents/Home/lib/server/libjvm.dylib
Referenced from: /Library/Frameworks/R.framework/Versions/3.6/Resources/library/rJava/libs/rJava.so
Reason: image not found
Using the common solution for rJava issues, 'sudo R CMD javareconf' doesn't work and gives me this ouput:
Java interpreter : /usr/bin/java
Java version : 11.0.6
Java home path : /Library/Java/JavaVirtualMachines/jdk-11.0.6.jdk/Contents/Home
Java compiler : /usr/bin/javac
Java headers gen.: /usr/bin/javah
Java archive tool: /usr/bin/jar
trying to compile and link a JNI program
detected JNI cpp flags : -I$(JAVA_HOME)/include -I$(JAVA_HOME)/include/darwin
detected JNI linker flags : -L$(JAVA_HOME)/lib/server -ljvm
clang -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I/Library/Java/JavaVirtualMachines/jdk-11.0.6.jdk/Contents/Home/include -I/Library/Java/JavaVirtualMachines/jdk-11.0.6.jdk/Contents/Home/include/darwin -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -I/usr/local/include -fPIC -Wall -g -O2 -c conftest.c -o conftest.o
clang-7: warning: no such sysroot directory: '/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk' [-Wmissing-sysroot]
In file included from conftest.c:1:
/Library/Java/JavaVirtualMachines/jdk-11.0.6.jdk/Contents/Home/include/jni.h:39:10: fatal error:
'stdio.h' file not found
#include <stdio.h>
^~~~~~~~~
1 error generated.
make: *** [conftest.o] Error 1
Unable to compile a JNI program
JAVA_HOME : /Library/Java/JavaVirtualMachines/jdk-11.0.6.jdk/Contents/Home
Java library path:
JNI cpp flags :
JNI linker flags :
Updating Java configuration in /Library/Frameworks/R.framework/Resources
Done.
Did anyone come across this error before and knows how to tackle it?
I had a problem like this with a coworker recently after they upgraded to Catalina. My solution was to install the offending version of JDK again. It looks like your system has confusion between 11.0.6 and 11.0.1.
Go to this page:
https://www.oracle.com/technetwork/java/javase/downloads/java-archive-javase11-5116896.html
Download and install 11.0.1
sudo R CMD javareconf
sudo Rstudio # elevate privs
install.packages("rJava")
If that doesn't work, try again from step 2, but with 11.0.6 from https://www.oracle.com/technetwork/java/javase/overview/index.html
What did the trick in the end was to show Rstudio where to look as identified by user Rmadillo here:
github.com/rstudio/rstudio/issues/2254
Just run this each time before using the rJava package.

Compiling a Mex file for Java

I found a java based TCP-IP system for matlab which might fix many problems I've been having with multiple and uninterruptible connections. However I'm stumped on how to get it working. I have compiled code under linux plenty of times but never on windows.
The code I'm trying to compile is located here and it states:
Build
Compile MEX files and Java helpers by the attached Makefile.
make
The question is how? I thought of using Visual studio command prompt but I end up getting an error:
makefile(8) : fatal error U1036: syntax error : too many names to left of '='
Which suggests either I have to edit the makefile or I'm using the wrong compiler. And I have no knowledge of Java so I don't even know where to begin when compiling it.
edit: Updating to show what code is in the makefile
MATLABDIR ?= /usr/local/matlab
MATLAB := $(MATLABDIR)/bin/matlab
MEX := $(MATLABDIR)/bin/mex
MEXEXT := $(shell $(MATLABDIR)/bin/mexext)
MEXSOURCES := $(wildcard private/*.cc)
MEXTARGETS := $(patsubst %.cc,%.$(MEXEXT),$(MEXSOURCES))
CLASSPATH := java
JAVASOURCES := $(wildcard java/matlab_tcpip/*.java)
JAVATARGETS = $(patsubst %.java,%.class,$(JAVASOURCES))
JARFILE = java/matlab_tcpip.jar
all: $(JARFILE) $(MEXTARGETS)
%.$(MEXEXT):%.cc
$(MEX) $< -output $#
$(JARFILE): $(JAVATARGETS)
jar cvf $# -C java matlab_tcpip/
jar i $#
%.class:%.java
javac -cp $(CLASSPATH) $<
test: $(JARFILE)
echo "run test/runServer.m" | $(MATLAB) -nodisplay & \
echo "run test/runClient.m" | $(MATLAB) -nodisplay
clean:
rm $(MEXTARGETS) java/matlab_tcpip/*.class $(JARFILE)

Issues in Installing RWeka in R (Ubuntu 16.04)

I want to install Fselector package in which RWeka is needed but on installing RWeka it throughs me following error:
Error : .onLoad failed in loadNamespace() for 'RWeka', details:
call: .jcall("java/lang/System", "V", "setOut", out)
error: method setOut with signature (Ljava/io/PrintStream;)V not found
Error: loading failed
Execution halted
ERROR: loading failed
* removing ‘/home/gaurav/R/x86_64-pc-linux-gnu-library/3.3/RWeka’
Warning in install.packages :
installation of package ‘RWeka’ had non-zero exit status
ERROR: dependency ‘RWeka’ is not available for package ‘FSelector’
* removing ‘/home/gaurav/R/x86_64-pc-linux-gnu-library/3.3/FSelector’
Warning in install.packages :
installation of package ‘FSelector’ had non-zero exit status
I searched about the same and did some modification with jdk, updated the path to jdk/bin in bashrc, but nothing worked.
Session Info
R version 3.3.3 (2017-03-06)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 16.04.2 LTS
locale:
[1] LC_CTYPE=en_IN.UTF-8 LC_NUMERIC=C LC_TIME=en_IN.UTF-8
[4] LC_COLLATE=en_IN.UTF-8 LC_MONETARY=en_IN.UTF-8 LC_MESSAGES=en_IN.UTF-8
[7] LC_PAPER=en_IN.UTF-8 LC_NAME=C LC_ADDRESS=C
[10] LC_TELEPHONE=C LC_MEASUREMENT=en_IN.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] Rcpp_0.12.12 digest_0.6.12 mime_0.5 grid_3.3.3 plyr_1.8.4
[6] R6_2.2.2 xtable_1.8-2 gtable_0.2.0 scales_0.4.1 ggplot2_2.2.1
[11] rlang_0.1.1 lazyeval_0.2.0 brew_1.0-6 tools_3.3.3 munsell_0.4.3
[16] Rook_1.1-1 shiny_1.0.4 httpuv_1.3.5 colorspace_1.3-2 htmltools_0.3.6
[21] tibble_1.3.3
Can anyone help me in this.
Thanks in advance
Update 1:#Nanov, I followed the steps but still i received same error, I have openjdk 8 and updated the path in bashrc, & when I used "sudo R CMD javareconf" I get below information:
gaurav#gaurav-ds:~$ sudo R CMD javareconf
Java interpreter : /usr/lib/jvm/java-8-oracle/jre/bin/java
Java version : 1.8.0_131
Java home path : /usr/lib/jvm/java-8-oracle/jre
Java compiler : not present
Java headers gen.:
Java archive tool:
trying to compile and link a JNI program
detected JNI cpp flags :
detected JNI linker flags : -L/usr/lib -ljvm
gcc -std=gnu99 -I/usr/share/R/include -DNDEBUG -fpic -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time-D_FORTIFY_SOURCE=2 -g -c conftest.c -o conftest.oconftest.c:1:17: fatal error: jni.h: No such file or directory
#include <jni.h>
^
compilation terminated.
/usr/lib/R/etc/Makeconf:132: recipe for target 'conftest.o' failed
make: *** [conftest.o] Error 1
Unable to compile a JNI program.
JAVA_HOME : /usr/lib/jvm/java-8-oracle/jre
Java library path:
JNI cpp flags :
JNI linker flags :
Updating Java configuration in /usr/lib/R
Done.
sudo apt-get install r-cran-rjava
sudo apt-get install openjdk-8-jdk
sudo rm -rf /usr/lib/jvm/default-java
sudo ln -s /usr/lib/jvm/java-8-openjdk-amd64/ /usr/lib/jvm/default-java
sudo R CMD javareconf
sudo R
install.packages(“RWeka”)
Better install openjdk-8-jdk, because in the newest version -9- is bug.

How to cross compile my C code containing JNI?

Previously I asked a question related to calling java function at
Calling Java functions from C language
Somehow I did with that.
Now I am planning to cross compile that.
I am facing an issue.
Before cross compiling ( means compiling in the same pc for the same pc)
I used the commands
gcc -I /usr/lib/jvm/java-6-openjdk-amd64/include -I /usr/lib/jvm/java-6-openjdk-amd64/include/linux -L /usr/bin/java -L /usr/lib/jvm/java-6-openjdk-amd64/jre/lib/amd64/server MyC.c -ljvm ;
That created well.
But now I am trying to cross compile the same for my target arm-none-eabi-
My makefile
CROSS_COMPILE =arm-none-eabi-
JAVA_HOME =/usr/lib/jvm/java-7-openjdk-amd64/
test:
javac -d ./ MyJava.java
$(CROSS_COMPILE)gcc -I $(JAVA_HOME)include \
-I $(JAVA_HOME)include/linux \
-L $(JAVA_HOME)jre/lib/amd64/server \
MyC.c -ljvm
This returning error
arm-none-eabi/bin/ld: warning: library search path "/usr/lib/jvm/java-7-openjdk-amd64/jre/lib/amd64/server" is unsafe for cross-compilation
/usr/lib/jvm/java-7-openjdk-amd64/jre/lib/amd64/server/libjvm.so: file not recognized: File format not recognized
collect2: ld returned 1 exit status
make: *** [test] Error 1

GCC option not found while constructing .dll

I'm trying to create a .dll while following this tutorial (http://www3.ntu.edu.sg/home/ehchua/programming/java/JavaNativeInterface.html) to use JNI in my eclipse project. There's an issue with my makefile however that may be unrelated to all the JNI stuff.
I'm getting "unknown option: --add-stdcall-alias" when I build (make all). I'm using the Mac GCC Compiler. Here's my console log and make file:
EDIT: So i removed the option that is giving me the error and my build worked. However, I feel unsafe just removing a line of code that I am clueless about. Any one want to tell me the implications of removing this code?
console output:
18:05:33 ** Build of configuration Default for project HPA* Testing *
make all
javah -classpath ../bin HPAProgram
gcc -Wl,--add-stdcall-alias -shared -o hpaprogram.dll HPAProgram.o
ld: unknown option: --add-stdcall-alias
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: ** [hpaprogram.dll] Error 1
18:05:34 Build Finished (took 823ms)
makefile:
# Define a variable for classpath
CLASS_PATH = ../bin
# Define a virtual path for .class in the bin directory
vpath %.class $(CLASS_PATH)
all : hpaprogram.dll
# $# matches the target, $< matches the first dependancy
hpaprogram.dll : HPAProgram.o
gcc -Wl,--add-stdcall-alias -shared -o $# $<
# $# matches the target, $< matches the first dependancy
HPAProgram.o : HPAProgram.c++ HPAProgram.h
gcc -I /System/Library/Frameworks/JavaVM.framework/Versions/A/Headers/ -c $< -o $#
# $* matches the target filename without the extension
HPAProgram.h : HPAProgram.class
javah -classpath $(CLASS_PATH) $*
clean :
rm HPAProgram.h HPAProgram.o hpaprogram.dll
I practiced by following the same tutorial, it turned out that it's OK to get rid of "-Wl,--add-stdcall-alias".
BTW, in Mac, you have to use ".dylib" instead of ".so", otherwise you will get error saying "java.lang.UnsatisfiedLinkError: no hello in java.library.path".

Categories