Commit ac45bfc7 authored by Martin Deinhofer's avatar Martin Deinhofer
Browse files

* moved makefile from jni to root directory

* copied orig makefile to makefile.windows
* since jdk 8 javah is outdated, but javac -h should be used instead.
* migrated libs, targets to linux (.dll --> .so,...)
parent 6c4eb0e8
# Exercise repository for Java Native Interface (JNI) # Exercise repository for Java Native Interface (JNI)
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). 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).
# Prerequisites # Prerequisites
1. **Java Development Kit 8** 1. **Java Development Kit >= 8**
2. **C/C++ Compiler** 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** * **[windows-only]**: Cygwin or MinGW on Windows ([Read "How to Setup Cygwin and MinGW](https://www3.ntu.edu.sg/home/ehchua/programming/howto/Cygwin_HowTo.html)
* Cygwin or MinGW on Windows ([Read "How to Setup Cygwin and MinGW](https://www3.ntu.edu.sg/home/ehchua/programming/howto/Cygwin_HowTo.html) * GCC compiler and Makefile ([Read "GCC and Make"](https://www3.ntu.edu.sg/home/ehchua/programming/cpp/gcc_make.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)). 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 # Clone project
```git clone https://es.technikum-wien.at/embedded_systems_public/Java-Exercises-7-JNI-HW-Interfacing.git``` ```git clone https://es.technikum-wien.at/embedded_systems_public/Java-Exercises-7-JNI-HW-Interfacing.git```
# Create Eclipse project # Create Eclipse project
1. *File/New/Project* 1. *File/New/Project*
2. Select *General/Project* and click *Next* 2. Select *General/Project* and click *Next*
3. Unselect *Use default location* and browse to cloned diretory, click *OK* 3. Unselect *Use default location* and browse to cloned diretory, click *OK*
4. Enter *Project name* and click *Finish* 4. Enter *Project name* and click *Finish*
# Build project # Build project
Building the project involves two steps Building the project involves two steps
1. Building Java code 1. Building the Java code and the native code using the make targets:
* Click on *Project/Build All*. This is not necessary if *Build Automatically* is selected. * Command line:```make```
2. Building native code either on command line or using the configured Eclipse build targets
* Command line:```cd jni && make``` or # Run project
* Double click on *jni/Build Targets/all*
Before the application can be run, you must provide the library path to the native library (Linux: ```libhello.so```, Windows: ```hello.dll```) file, if it is not located in the *current working directory*.
# Run project 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*.
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*. You should see the output "Hello World!" displayed on the console.
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*.
## Command line
You should see the output "Hello World!" displayed on the console.
### Example 1
## Command line ```java -Djava.library.path=jni -cp bin java_exercises7_1_1.HelloJNI```
### Example 2
```java -Djava.library.path=jni -cp bin java_exercises7_1_1.HelloJNI``` ```java -Djava.library.path=jni -cp bin java_exercises7_2_1.HelloJNI```
# Define a variable for classpath # Define a variable for classpath
CLASS_PATH = ../bin CLASS_PATH = ../bin
JAVA_HOME=C:\Program Files (x86)\Java\jdk1.8.0_131 JAVA_HOME=C:\Program Files (x86)\Java\jdk1.8.0_131
# Define a virtual path for .class in the bin directory # Define a virtual path for .class in the bin directory
vpath %.class $(CLASS_PATH) vpath %.class $(CLASS_PATH)
all : hello.dll java_exercises7_2_1_HelloJNI.dll all : hello.dll java_exercises7_2_1_HelloJNI.dll
# Targets for java_exercises_7_1_1 # Targets for java_exercises_7_1_1
# $@ matches the target, $< matches the first dependancy # $@ matches the target, $< matches the first dependancy
hello.dll : HelloJNI.o hello.dll : HelloJNI.o
gcc -Wl,--add-stdcall-alias -shared -o $@ $< gcc -Wl,--add-stdcall-alias -shared -o $@ $<
# $@ matches the target, $< matches the first dependancy # $@ matches the target, $< matches the first dependancy
HelloJNI.o : HelloJNI.c HelloJNI.h HelloJNI.o : HelloJNI.c HelloJNI.h
gcc -I"$(JAVA_HOME)\include" -I"$(JAVA_HOME)\include\win32" -c $< -o $@ gcc -I"$(JAVA_HOME)\include" -I"$(JAVA_HOME)\include\win32" -c $< -o $@
# $* matches the target filename without the extension # $* matches the target filename without the extension
HelloJNI.h : java_exercises7_1_1/HelloJNI.class HelloJNI.h : java_exercises7_1_1/HelloJNI.class
javah -o HelloJNI.h -classpath $(CLASS_PATH) java_exercises7_1_1.$* javah -o HelloJNI.h -classpath $(CLASS_PATH) java_exercises7_1_1.$*
# Targets for java_exercises_7_2_1 # Targets for java_exercises_7_2_1
# $@ matches the target, $< matches the first dependancy # $@ matches the target, $< matches the first dependancy
java_exercises7_2_1_HelloJNI.dll : java_exercises7_2_1_HelloJNI.o java_exercises7_2_1_HelloJNI.dll : java_exercises7_2_1_HelloJNI.o
gcc -Wl,--add-stdcall-alias -shared -o $@ $< gcc -Wl,--add-stdcall-alias -shared -o $@ $<
# $@ matches the target, $< matches the first dependancy # $@ matches the target, $< matches the first dependancy
java_exercises7_2_1_HelloJNI.o : java_exercises7_2_1_HelloJNI.c java_exercises7_2_1_HelloJNI.h java_exercises7_2_1_HelloJNI.o : java_exercises7_2_1_HelloJNI.c java_exercises7_2_1_HelloJNI.h
gcc -I"$(JAVA_HOME)\include" -I"$(JAVA_HOME)\include\win32" -c $< -o $@ gcc -I"$(JAVA_HOME)\include" -I"$(JAVA_HOME)\include\win32" -c $< -o $@
java_exercises7_2_1_HelloJNI.h : java_exercises7_2_1/HelloJNI.class java_exercises7_2_1_HelloJNI.h : java_exercises7_2_1/HelloJNI.class
javah -classpath $(CLASS_PATH) java_exercises7_2_1.HelloJNI javah -classpath $(CLASS_PATH) java_exercises7_2_1.HelloJNI
clean : clean :
rm HelloJNI.h HelloJNI.o hello.dll java_exercises7_2_1_HelloJNI.dll java_exercises7_2_1_HelloJNI.h java_exercises7_2_1_HelloJNI.o rm HelloJNI.h HelloJNI.o hello.dll java_exercises7_2_1_HelloJNI.dll java_exercises7_2_1_HelloJNI.h java_exercises7_2_1_HelloJNI.o
\ No newline at end of file
# Define a variable for classpath
CLASS_PATH = ./bin
JAVA_HOME=/usr/lib/jvm/default-java/
JNI_DIR=./jni
SRC_DIR=./src
# Define a virtual path for .class in the bin directory
vpath %.class $(CLASS_PATH)
all : $(JNI_DIR)/libhello.so $(JNI_DIR)/libjava_exercises7_2_1_HelloJNI.so
# Targets for java_exercises_7_1_1
# $@ matches the target, $< matches the first dependancy
$(JNI_DIR)/libhello.so : $(JNI_DIR)/HelloJNI.o
gcc -W -shared -o $@ $<
# $@ matches the target, $< matches the first dependancy
$(JNI_DIR)/HelloJNI.o : $(JNI_DIR)/HelloJNI.c $(JNI_DIR)/HelloJNI.h
gcc -I"$(JAVA_HOME)/include" -I"$(JAVA_HOME)/include/linux" -I"$(JNI_DIR)" -c $< -o $@
# $* matches the target filename without the extension
$(JNI_DIR)/HelloJNI.h :
javac -h $(JNI_DIR) -d $(CLASS_PATH) $(SRC_DIR)/java_exercises7_1_1/HelloJNI.java
# Targets for java_exercises_7_2_1
# $@ matches the target, $< matches the first dependancy
$(JNI_DIR)/libjava_exercises7_2_1_HelloJNI.so : $(JNI_DIR)/java_exercises7_2_1_HelloJNI.o
gcc -W -shared -o $@ $<
# $@ matches the target, $< matches the first dependancy
$(JNI_DIR)/java_exercises7_2_1_HelloJNI.o : $(JNI_DIR)/java_exercises7_2_1_HelloJNI.c $(JNI_DIR)/java_exercises7_2_1_HelloJNI.h
gcc -I"$(JAVA_HOME)/include" -I"$(JAVA_HOME)/include/linux" -I"$(JNI_DIR)" -c $< -o $@
$(JNI_DIR)/java_exercises7_2_1_HelloJNI.h :
javac -h $(JNI_DIR) -d $(CLASS_PATH) $(SRC_DIR)/java_exercises7_2_1/HelloJNI.java
clean :
rm -Rf $(JNI_DIR)/*.h $(JNI_DIR)/*.o $(JNI_DIR)/*.so $(JNI_DIR)/*.dll $(CLASS_PATH)/*
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment