How can I call C functions from Java code on Linux

I am writing a Java program on Suse Linux 11 using JavaSE-1.6 and I am having a problem using javac.

I follow the tutorial on

http://java.sun.com/developer/onlineTraining/Programming/JDCBook/jniexamp.html

and still wrote the following:

package com.ctest; class CTest { // Native method declaration native int testCall(); // Load the library static { System.loadLibrary("fpdpReaderLib"); } public static void main(String args[]) { int retVal; // Create class instance CTest cLangTest = new CTest(); // Call native method retVal = cLangTest.testCall(); System.out.println(retVal); } } 

When I run javac CTest.java, I get an error:

 /usr/lib/gcc/i586-suse-linux/4.3/../../../crt1.o: in function '_start': /usr/src/packages/BUILD/glibc-2.9/csu/../sysdeps/i386/elf/start.S:115: undefined reference to 'main' /tmp/cc97kcJu.o:(.data+0x28) undefined reference to 'hidden alias for int com::ctest::CTest::testCall()' /tmp/cc97kcJu.o:(.data+0x74) undefined reference to 'hidden alias for int com::ctest::CTest::testCall()' collect2: ld returned 1 exit status 

I suspect it uses gcc, not the java version of javac, but I'm not sure.

What ideas might come up?

I tried using the "--main =" option mentioned here:

http://gcc.gnu.org/java/faq.html#4_1

but instead of an error, before I just get:

 /tmp/ccwfugWq.o:(.data+0x28) undefined reference to 'hidden alias for int com::ctest::CTest::testCall()' /tmp/ccwfugWq.o:(.data+0x74) undefined reference to 'hidden alias for int com::ctest::CTest::testCall()' collect2: ld returned 1 exit status 
+4
source share
3 answers

I think you should install and use the Sun Java SDK, and not use the gcc javac compiler.

Google for suse javac gcc causes a lot of similar problems, and the solution is always similar to using Sun JDK.

0
source

On the page you indicated:

The library containing the implementation of the native code is loaded, call System.loadLibrary (). Placing this call in a static Initializer ensures that this library is loaded only once per class. the library can be loaded outside the static block if your application requires it. You may need to set up your environment so that the loadLibrary method can find your own code library.

My emphasis. Have you installed LD_LIBRARY_PATH (or something suitable) for your system?

+2
source

I suggest you run which javac to determine which compiler you are using. If you want Java 6, you cannot use gcj. You need to set PATH so that you use javac from JDK 6.

+1
source

Source: https://habr.com/ru/post/1381282/


All Articles