Difference between revisions of "Java package"
Line 31: | Line 31: | ||
==Coupling a java program with native libraries== | ==Coupling a java program with native libraries== | ||
− | The use of native libraries can be required when plateform-specific features are needed. In such a context, Java Native Interface (JNI) is helpful. It is included in the jamvm package but another tool called SWIG (Simplified Wrapper and Interface Generator) | + | The use of native libraries can be required when plateform-specific features are needed. In such a context, Java Native Interface (JNI) is helpful. It is included in the jamvm package but another tool called SWIG (Simplified Wrapper and Interface Generator) facilitates the design of this interface. |
SWIG can be installed in debian/ubuntu linux distributions as follows | SWIG can be installed in debian/ubuntu linux distributions as follows | ||
Revision as of 22:10, 8 September 2011
How-To install java on the target and run a java program.
Contents
Install java packages GNU Classpath and JamVM
Add the GNU Classpath and JamVM to your target:
$ make menuconfig
Goto the buildroot packages and enable the Java package and from the java sub menu select both GNU classpath and JamVM packages. You have to rebuild a new rootfs et update your target
Warning: With releases after 3.4 of armadeus you have to activate the Classpath library first (packages->Libraries->Java->classpath) then Jamvm itself (packages->Interpreter languages..->jamvm) |
Run a java program
You can run your java program compiled for your host. Upload your jar, zip or class files to the target then use the jamvm command to run your program.
jamvm -jar myprog.jar
If your program requires some other libraries, you will have to install these on the target first or build your program to provide your code and these libraries.
Coupling a java program with native libraries
The use of native libraries can be required when plateform-specific features are needed. In such a context, Java Native Interface (JNI) is helpful. It is included in the jamvm package but another tool called SWIG (Simplified Wrapper and Interface Generator) facilitates the design of this interface. SWIG can be installed in debian/ubuntu linux distributions as follows
sudo apt-get install swig
Then, an example can be easily tested. For instance, the one proposed in the tutorial submenu of the SWIG website is based on a single example.c file (and an associated interface file example.i similar to a header file .h). The key function of this library is char * get_time() because it is based on a system-based library (header file time.h). Use the following commands to produce the required files (native library libexample.so and static class example.class)
swig -java example.c example.i arm-linux-gcc -fpic -c example.c example_wrap.c arm-linux-gcc -shared example.o example_wrap.o -o libexample.so cat test.java public class test { public static void main(String argv[]) { System.loadLibrary("example"); System.out.println(example.getMy_variable()); System.out.println(example.fact(5)); System.out.println(example.get_time()); } } javac test.java
Warning: The name of the java test class is "main" in the SWIG tutorial but "test" is preferred here due to the ambiguity between the name of the class and the name of the main function. |
Then, three files (libexample.so, example.class, exampleJNI.class and test.class) must be downloaded into the APF board. They can be placed in the tftp server directory (e.g. /tftpboot) and then downloaded as follows (with the correct XXX.XXX.XXX.XXX IP address)
tftp -g -r libexample.so XXX.XXX.XXX.XXX tftp -g -r example.class XXX.XXX.XXX.XXX tftp -g -r exampleJNI.class XXX.XXX.XXX.XXX tftp -g -r test.class XXX.XXX.XXX.XXX
Verifiy (and change if necessary) privileges of these files. Then run the test class as follows
jamvm -Djava.library.path=$LD_LIBRARY_PATH test
where $LD_LIBRARY_PATH is the path of the directory where libexample.so is located in (e.g. /root).