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
......@@ -4,10 +4,10 @@ At times, it is necessary to use native codes (C/C++) to overcome the memory man
# Prerequisites
1. **Java Development Kit 8**
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)
* **[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)
* 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)).
# Clone project
......@@ -24,22 +24,22 @@ At times, it is necessary to use native codes (C/C++) to overcome the memory man
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*
1. Building the Java code and the native code using the make targets:
* Command line:```make```
# Run project
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*.
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*.
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*.
You should see the output "Hello World!" displayed on the console.
## Command line
### Example 1
```java -Djava.library.path=jni -cp bin java_exercises7_1_1.HelloJNI```
### Example 2
```java -Djava.library.path=jni -cp bin java_exercises7_2_1.HelloJNI```
......
# 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