BogoToBogo
  • Home
  • About
  • Big Data
  • Machine Learning
  • AngularJS
  • Python
  • C++
  • go
  • DevOps
  • Kubernetes
  • Algorithms
  • More...
    • Qt 5
    • Linux
    • FFmpeg
    • Matlab
    • Django 1.8
    • Ruby On Rails
    • HTML5 & CSS

Eclipse CDT & JNI (Java Native Interface) with 64 bit MinGW - 2020

cplusplus_icon.png




Bookmark and Share





bogotobogo.com site search:





Knock, knock.
"Who's there?"
Very long pause.
...
...
"Java."


Eclipse CDT with MingW on Windows
Install

Following items are needed:

  1. Eclipse - Kepler 4.3
  2. We need compiler since CDT (C/C++ Development Tool) does not come with it.
    Eclipse C/C++ IDE (CDT) for Kepler.
  3. To make it work (64 dll on 64 machine), we need 64-bit MinGW.
    In my case, I put it here: C:\eclipse\MinGW
    Also, we need to add the path (C:\eclipse\MinGW\bin) to environment variable

Eclipse Setup
  1. Put the source code:

    eclipse_hello_source.png

  2. Window->Open Perspective->Others...->C/C++->OK.
  3. Project->Properties...
    Check items under C/C++ Build.
    For example, the PATH under Environment should lead us to the compiler and it should look like this:
    ${MINGW_HOME}\bin;
  4. Build project and run:

    EclipseOutput_windows.png



64-bit JNI (Java Native Interface) with Eclipse CDT
Creating Java Project
  1. Create a new Java project (HelloJNI)
  2. Make the HelloJNI.java Java class:
    public class HelloJNI 
    {
       static {
          // hello.dll on Windows or libhello.so on Linux
          System.loadLibrary("hello"); 
       }
       private native void sayHello();
     
       public static void main(String[] args) {
          // invoke the native method
          new HelloJNI().sayHello();  
       }
    }
    

    Converting the Java Project to a C/C++ Project

    1. Right-click on the HelloJNI Java project->New->Other...
      Under C/C++, Convert to a C/C++ Project (Adds C/C++ Nature)->Next.
    2. Then, the "Convert to a C/C++ Project" dialog will pop up. In "Project type", select "Makefile Project" ->In "Toolchains", select "MinGW GCC", and hit Finish.

      ConvertToACCppProject.png

    3. Now, we can run this project as a Java as well as a C/C++ project.


    Generating C/C++ Header File
    1. Create a directroy called "jni" under the project to keep all the C/C++ codes:
      right-click on the project->New->Folder. In "Folder name", enter "jni".
    2. Create a "makefile" under the "jni" directory:
      Right-click on the "jni" folder->new->File. In "File name", enter "makefile", and hit Finish.

      jni_folder.png

    3. Then, enter the following codes. Because it is makefile, we should use tab for the indent instead of spaces.
      # Define a variable for classpath
      CLASS_PATH = ../bin
      
      # Define a virtual path for .class in the bin directory
      vpath %.class $(CLASS_PATH)
      
      # $* matches the target filename without the extension
      HelloJNI.h : HelloJNI.class
      	javah -classpath $(CLASS_PATH) $*
      
      This makefile creates a target "HelloJNI.h", which has a dependency of "HelloJNI.class", and invokes the javah utiltiy on "HelloJNI.class" (under -classpath) to build the target header file: Right-click on the makefile->Make Targets->Create...
      In "Target Name", enter "HelloJNI.h". Then, hit OK.

      CreateMakeTarget_hello_jni_h.png

    4. Run the makefile for the target "HelloJNI.h":
      Right-click on the makefile->Make Targets->Build...
      Make sure all the files (HelloJNI.java and makefile) are saved.
      Select the target "HelloJNI.h"->Build...
      The header file "HelloJNI.h" shall be generated in the "jni" directory.

      HelloJNI_h_Console.png

      HelloJNI_h.png


    C File - HelloJNI.c

    1. Create a C program called "HelloJNI.c":
      Right-click on the "jni" folder->New->Source file.
      In "Source file", enter "HelloJNI.c".
      Enter the following code:
      #include <jni.h>
      #include <stdio.h>
      #include "HelloJNI.h"
       
      JNIEXPORT void JNICALL Java_HelloJNI_sayHello(JNIEnv *env, jobject thisObj) 
      {
         printf("Hello World!\n");
         return;
      }
      
    2. Modify the "makefile" as shown below to generate the shared library "hello.dll":
      # Define a variable for classpath
      CLASS_PATH = ../bin
      
      # Define a virtual path for .class in the bin directory
      vpath %.class $(CLASS_PATH)
      
      all : hello.dll
      
      # $@ matches the target, $< matches the first dependancy
      hello.dll : HelloJNI.o
      	gcc -m64 -Wl,--add-stdcall-alias -shared -o $@ $<
      
      # $@ matches the target, $< matches the first dependancy
      HelloJNI.o : HelloJNI.c HelloJNI.h
      	gcc -m64 -I"C:\Program Files\Java\jdk1.7.0_21\include" -c $< -o $@
      
      # $* matches the target filename without the extension
      HelloJNI.h : HelloJNI.class
      	javah -classpath $(CLASS_PATH) $*
      
      clean :
      	rm HelloJNI.h HelloJNI.o hello.dll
      
    3. Right-click on the "makefile"->Make Targets->Create...
      In "Target Name", enter "all".
      Repeat to create a target "clean".
    4. Run the makefile for the target "all": Right-click on the makefile->Make Targets->Build...
      Select the target "all"->Build...

      build_all.png

      BuildingTarget_all.png

    5. The shared library "hello.dll" has been created in "jni" directory.

      hello.dll_created.png


    Finally, Running the Java JNI Program

    1. We need to provide the library path to the "hello.dll".
      This can be done via VM argument -Djava.library.path:
      Right-click on the project->Run As->Run Configurations...
      Select (double-click) "Java Application"
      In "Main" tab, enter the Main class "HelloJNI".
      In "Arguments", "VM Arguments", enter "-Djava.library.path=jni"


      JNI_Run_Configuration.png

    2. Run.
      We should see the output "Hello World!" displayed on the console.

      HelloWorldOutput.png



    Note: 32-bit dll on 64-bit JVM?

    In my case (AMD 64-bit, Windows8), initially, I had an error related to the 64bit JVM (or 32bit dll):
    UnsatisfiedLinkError: C:\Users\KHyuck\workspaceJNI\HelloJNI\jni\hello.dll:
    Can't load IA 32-bit .dll on a AMD 64-bit platform.
    So, I checked my machine using java -d64 -version command:
    C:\>java -d64 -version
    java version "1.7.0_21"
    Java(TM) SE Runtime Environment (build 1.7.0_21-b11)
    Java HotSpot(TM) 64-Bit Server VM (build 23.21-b01, mixed mode)
    
    C:\>java -d32 -version
    Error: This Java instance does not support a 32-bit JVM.
    Please install the desired version.
    

    As expected, my JVM is 64-bit.

    Regarding the complain: Can't load IA 32-bit .dll (hello.dll), I checked it with Dependency Walker.


    dependency_walker.png

    Indeed, the CPU columns are not x64, x86_64 or amd64 (64-bit) but it is x86 (32-bit)!

    So, I needed to get the 64-bit MinGW to make 64-bit dll. It has msys as well.

    Here is the Environment variables for Project Properties that works with the MinGW 64-bit.


    ProjectPropertiesEnvironment.png




    C++ with Eclipse on Linux

    The steps are similar to the Windows case except that we do not have to install additional the compiler.

    This example is running on Fedora 18.



    Ecluipse_Juno.png

    Fedora.png

    Install Eclipse and Eclipse C/C++ Development Tools (CDT) plugin.

    1. yum install eclipse
    2. yum install eclipse-cdt

    eclipse-cdt.png hello.png




Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization

YouTubeMy YouTube channel

Sponsor Open Source development activities and free contents for everyone.

Thank you.

- K Hong






Sponsor Open Source development activities and free contents for everyone.

Thank you.

- K Hong






C++ Tutorials

C++ Home

Algorithms & Data Structures in C++ ...

Application (UI) - using Windows Forms (Visual Studio 2013/2012)

auto_ptr

Binary Tree Example Code

Blackjack with Qt

Boost - shared_ptr, weak_ptr, mpl, lambda, etc.

Boost.Asio (Socket Programming - Asynchronous TCP/IP)...

Classes and Structs

Constructor

C++11(C++0x): rvalue references, move constructor, and lambda, etc.

C++ API Testing

C++ Keywords - const, volatile, etc.

Debugging Crash & Memory Leak

Design Patterns in C++ ...

Dynamic Cast Operator

Eclipse CDT / JNI (Java Native Interface) / MinGW

Embedded Systems Programming I - Introduction

Embedded Systems Programming II - gcc ARM Toolchain and Simple Code on Ubuntu and Fedora

Embedded Systems Programming III - Eclipse CDT Plugin for gcc ARM Toolchain

Exceptions

Friend Functions and Friend Classes

fstream: input & output

Function Overloading

Functors (Function Objects) I - Introduction

Functors (Function Objects) II - Converting function to functor

Functors (Function Objects) - General



Git and GitHub Express...

GTest (Google Unit Test) with Visual Studio 2012

Inheritance & Virtual Inheritance (multiple inheritance)

Libraries - Static, Shared (Dynamic)

Linked List Basics

Linked List Examples

make & CMake

make (gnu)

Memory Allocation

Multi-Threaded Programming - Terminology - Semaphore, Mutex, Priority Inversion etc.

Multi-Threaded Programming II - Native Thread for Win32 (A)

Multi-Threaded Programming II - Native Thread for Win32 (B)

Multi-Threaded Programming II - Native Thread for Win32 (C)

Multi-Threaded Programming II - C++ Thread for Win32

Multi-Threaded Programming III - C/C++ Class Thread for Pthreads

MultiThreading/Parallel Programming - IPC

Multi-Threaded Programming with C++11 Part A (start, join(), detach(), and ownership)

Multi-Threaded Programming with C++11 Part B (Sharing Data - mutex, and race conditions, and deadlock)

Multithread Debugging

Object Returning

Object Slicing and Virtual Table

OpenCV with C++

Operator Overloading I

Operator Overloading II - self assignment

Pass by Value vs. Pass by Reference

Pointers

Pointers II - void pointers & arrays

Pointers III - pointer to function & multi-dimensional arrays

Preprocessor - Macro

Private Inheritance

Python & C++ with SIP

(Pseudo)-random numbers in C++

References for Built-in Types

Socket - Server & Client

Socket - Server & Client 2

Socket - Server & Client 3

Socket - Server & Client with Qt (Asynchronous / Multithreading / ThreadPool etc.)

Stack Unwinding

Standard Template Library (STL) I - Vector & List

Standard Template Library (STL) II - Maps

Standard Template Library (STL) II - unordered_map

Standard Template Library (STL) II - Sets

Standard Template Library (STL) III - Iterators

Standard Template Library (STL) IV - Algorithms

Standard Template Library (STL) V - Function Objects

Static Variables and Static Class Members

String

String II - sstream etc.

Taste of Assembly

Templates

Template Specialization

Template Specialization - Traits

Template Implementation & Compiler (.h or .cpp?)

The this Pointer

Type Cast Operators

Upcasting and Downcasting

Virtual Destructor & boost::shared_ptr

Virtual Functions



Programming Questions and Solutions ↓

Strings and Arrays

Linked List

Recursion

Bit Manipulation

Small Programs (string, memory functions etc.)

Math & Probability

Multithreading

140 Questions by Google



Qt 5 EXPRESS...

Win32 DLL ...

Articles On C++

What's new in C++11...

C++11 Threads EXPRESS...

Go Tutorial

OpenCV...








Contact

BogoToBogo
contactus@bogotobogo.com

Follow Bogotobogo

About Us

contactus@bogotobogo.com

YouTubeMy YouTube channel
Pacific Ave, San Francisco, CA 94115

Pacific Ave, San Francisco, CA 94115

Copyright © 2024, bogotobogo
Design: Web Master