-=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- (c) WidthPadding Industries 1987 0|429|0 -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=-
SoCoder -> Link Home -> Help/Tutorials


 
JL235
Created : 07 September 2007
 

calling C from Java in 5 minutes

screencast explination of how it's done

https://www.netbeans.org/download/flash/netbeans_55/jni_demo/jni_demo.html
Mixing languages is becoming easier and easier. This short screen cast shows a German making a new Java and C project and then calls the C code from Java. Only takes about 5 minutes, but shows how easier it now is to mix these two powerful languages.

 

Comments


Tuesday, 19 August 2008, 11:30
mike_g
Well it worked for me. Took a little longer than 5 minute, but I had to make a few changes as I'm running Linux. Netbeans really is a kick ass compiler. Now I have my own custom CUPS interface
Tuesday, 19 August 2008, 17:01
steve_ancell
OOO-ER !...

I have NetBeans installed on my PC, but I thought that it only did Java.
Tuesday, 19 August 2008, 19:24
JL235
One thing to bear in mind (that I discovered a few days ago) is that if your calling the same native methods from classes loaded with different class loaders, then it will throw an error. This is because native libraries cannot be loaded with multiple class loaders (it's a limitation of the JVM which Sun will not change). Instead just load the native library with a 'shared classloader', i.e. the parent classloader (such as the system classloader) of all the classloaders that will be loading the classes that use the native library.

You can even achieve this from within a class loaded by a child of a classloader by just getting the system classloader and then loading a class that loads which has a static method that loads the native library. This method can be called through using reflection.

I also fully agree, NetBeans 6 does kick ass!
Tuesday, 19 August 2008, 23:51
mike_g
Steve: You can download the C/C++ plugin for it, or netbeans-6 comes with it by default plus a load of other nice things such as uml diagram generation. You would then have to link C/C++ compilers to the ide, but thats fairly straightforward.

JL: Yeah I noticed that I couldent access the functions from another class; ended up editing the headers by hand to change the class it worked with. For what I'm doing its fine connecting to 1 class as its only a printer interface, but I'll remember that in case I need it in future.
Wednesday, 20 August 2008, 02:06
JL235
The full installation of NetBeans does, but I don't know if you get the full installation of the Java installation if you got it via the JDK/NetBeans pack on the Sun Microsystems website (or if you just downloaded the Java installation when you got NetBeans).

The C/C++ compilers are more complicated on Windows then on a UNIX system. I personally don't understand why Sun don't port their Sun compilers to Windows and so also distribute them with NetBeans. So Steve you'll need the GCC compilers which are best to get through CygWin, which I believe the NetBeans site also recommends. They should also have a guide on their site somewhere.
Wednesday, 20 August 2008, 09:34
steve_ancell
Cheers mike_g.