README.md 2.25 KB
Newer Older
Martin Deinhofer's avatar
Martin Deinhofer committed
1
2
# Exercise repository for Java Native Interface (JNI)

Martin Deinhofer's avatar
Martin Deinhofer committed
3
4
At times, it is necessary to use native codes (C/C++) to overcome the memory management and performance constraints in Java. Java supports native codes via the Java Native Interface (JNI). This repository contains a "Hello World" example for using the JNI which is based on this [tutorial](https://www3.ntu.edu.sg/home/ehchua/programming/java/JavaNativeInterface.html).

5
6
7
8
9
10
11
12
13

# Prerequisites
1. **Java Development Kit 8**
2. **C/C++ Compiler**
    * GCC compiler and Makefile ([Read "GCC and Make"](https://www3.ntu.edu.sg/home/ehchua/programming/cpp/gcc_make.html)) **or**
    * Cygwin or MinGW on Windows ([Read "How to Setup Cygwin and MinGW](https://www3.ntu.edu.sg/home/ehchua/programming/howto/Cygwin_HowTo.html)
3. **Eclipse for Java Developers** with **C/C++ Development Tool (CDT) extension** installed ([Read "Eclipse CDT"](https://www3.ntu.edu.sg/home/ehchua/programming/howto/EclipseCpp_HowTo.html)).

# Clone project
Martin Deinhofer's avatar
Martin Deinhofer committed
14
```git clone https://es.technikum-wien.at/embedded_systems_public/Java-Exercises-7-JNI-HW-Interfacing.git```
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

# Create Eclipse project

1. *File/New/Project*
2. Select *General/Project* and click *Next*
3. Unselect *Use default location* and browse to cloned diretory, click *OK*
4. Enter *Project name* and click *Finish*

# Build project

Building the project involves two steps

1. Building Java code
    * Click on *Project/Build All*. This is not necessary if *Build Automatically* is selected.
2. Building native code either on command line or using the configured Eclipse build targets
    * Command line:```cd jni && make``` or
    * Double click on *jni/Build Targets/all*

33
34
# Run project

Martin Deinhofer's avatar
Martin Deinhofer committed
35
36
37
Before the application can be run, you must build the native code and provide the library path to the "hello.dll" file, if it is not located in the *current working directory*.
This can be done via the VM argument ```-Djava.library.path```. Right-click on the project and select *Run As/Run Configurations*, then select *Java Application*. In the *Main tab*, enter the main class "HelloJNI" and in *Arguments/VM Arguments*, enter ```-Djava.library.path=jni```. Finally click on *Run*.

38
39
You should see the output "Hello World!" displayed on the console.

Martin Deinhofer's avatar
Martin Deinhofer committed
40
## Command line
41

Martin Deinhofer's avatar
Martin Deinhofer committed
42
```java -Djava.library.path=jni -cp bin HelloJNI```
43

44
45


Martin Deinhofer's avatar
Martin Deinhofer committed
46