0
votes

I'm using this tutorial for JNI in Eclipse:

https://www3.ntu.edu.sg/home/ehchua/programming/java/JavaNativeInterface.html#zz-2.6

(I'm using only the part "2.6 JNI in Eclipse").

And until this part of the tutorial:

Run the makefile for the target "all", by right-click on the makefile ⇒ Make >Targets ⇒ Build ⇒ Select the target "all" ⇒ Build

It seemed like everything worked well (meaning- the results were the same as in the tutorial and there were no warning or errors in the "Problems" tab).

But when I did this part, I noticed that the line :

javah -classpath ../bin HelloJNI

was missing from the printing in the console.

Then I continued to the next step of the tutorial- "Step 5: Run the Java JNI Program".

But even though it did print to the console "Hello World!", I noticed that there is an error in the "Problems" tab:

"make: *** No rule to make target `all'. Stop."

Development Environment

+Eclipse IDE for Java Developers (32 bit) Version: Kepler Service Release 2.

+CDT plugin for Eclipse

+Windows 10 64-bit (I use eclipse 32-bit because at some point, the 64-bit eclipse couldn't open and the solution was to use 32-bit eclipse)

makefile

# 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 dependency
hello.dll : HelloJNI.o
    gcc -Wl,--add-stdcall-alias -shared -o $@ $<

# $@ matches the target, $< matches the first dependency
HelloJNI.o : HelloJNI.c HelloJNI.h
    gcc -I"C:\Program Files (x86)\Java\jdk1.8.0_212\include" -I"C:\Program Files (x86)\Java\jdk1.8.0_212\include\win32" -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

HelloJNI.c

#include <jni.h>
#include <stdio.h>
#include "HelloJNI.h"

JNIEXPORT void JNICALL Java_HelloJNI_sayHello(JNIEnv *env, jobject thisObj) {
   printf("Hello World!\n");
   return;
}

HelloJNI.h

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class HelloJNI */

#ifndef _Included_HelloJNI
#define _Included_HelloJNI
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     HelloJNI
 * Method:    sayHello
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_HelloJNI_sayHello
  (JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif

HelloJNI.java

public class HelloJNI {
   static {
      System.loadLibrary("hello"); // hello.dll (Windows) or libhello.so (Unixes)
   }

   // Declare native method
   private native void sayHello();

   // Test Driver
   public static void main(String[] args) {
      new HelloJNI().sayHello();  // Allocate an instance and invoke the native method
   }
}

1
I think that maybe I get that error because maybe Eclipse is searching for my makefile in the folder of the project instead of the "jni" folder that inside the folder of the project. But even if that's the reason, I don't know how to solve it.Moshe

1 Answers

1
votes

suggest:

Before the makefile target all, insert the statement:

.PHONY: all

so the make utility is not trying to create a file named 'all'

Probably should add a similar statement before the target clean