Wednesday 31 March 2010

HTML5 - demo1

Test elementu canvas.


Sunday 28 March 2010

JNI Example - Eclipse, Dev-Cpp

I wanted to test JNI by preparing simple example. I did had a lot of problems with it. Especially with creating proper DLL :( That is why I wanted to live something behind - maybe someone will have less problems. This is a simple example not tutorial about JNI.


Software:


  • Eclipse GALILEO - Java IDE - here

  • Dev-Cpp - C++ IDE - here



Plan:


  • Java part.

    • Create java project.

    • Create java class with native methods.

    • Generate cpp header.

    • Create java class which will load library and use our native class



  • C++ part.

    • Create c++ project.

    • Create c++ class and implement it.






Java part


After opening eclipse IDE create new Java project (File->New->Other->Java Project). Name: jni_hello_world. Create new class (File->New->Class) in default package (empty package) named MyFirstWrapper.



public class MyFirstWrapper {
native public String first(String parameter);
}

Create cpp directory in main project. Using eclipse External Tools Configuration (on the main toolbar there is play icon with red toolbox - running external tools) we add new configuration. Name: generate JNI header, location: c:\Program Files\Java\jdk1.7.0\bin\javah.exe, working directory: ${workspace_loc:/jni_hello_world/cpp} (use browse workspace directory), arguments: -jni MyFirstWrapper. After running it refresh the project (F5 on project). There should be created header file MyFirstWrapper.h in cpp directory.



/* DO NOT EDIT THIS FILE - it is machine generated */
#include
/* Header for class MyFirstWrapper */

#ifndef _Included_MyFirstWrapper
#define _Included_MyFirstWrapper
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: MyFirstWrapper
* Method: first
* Signature: (Ljava/lang/String;)Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_MyFirstWrapper_first
(JNIEnv *, jobject, jstring);

#ifdef __cplusplus
}
#endif
#endif

Now lets create new class named Runner in default package.



public class Runner {
static public void main(String[] args) throws Exception {
System.loadLibrary("MyFirst");
MyFirstWrapper wrapper = new MyFirstWrapper();
System.out.println(wrapper.first("world!"));
}
}

Using eclipse Run Configuration (on the main toolbar there is play icon) we add new configuration. On the main tab: name: Runner, project: jni_hello_world, main class: Runner. On the arguments tab: vm arguments: -Djava.library.path=cpp. Apply.


Now we are missing the dll so we go to the second part in dev-cpp.



C++ part


Open dev-cpp and create new project (File->New->Project). Choose on tab basic: Dynamic DLL, name: MyFirst, language C++, then OK and save it in your workspace in cpp directory (e.g. c:\workspaces\playground\jni_hello_world\cpp\MyFirst.dev). Now lets close without saving all generated tabs (right click on tab and close all). Add header file (right click on project and add to project - choose MyFirstWrapper.h). Create new file and save it as MyFirstWrapper.cpp (right click on project and new file then CTRL+S).



#include
#include
#include
#include
#include
#include "MyFirstWrapper.h"

JNIEXPORT jstring JNICALL Java_MyFirstWrapper_first
(JNIEnv *env, jobject obj, jstring parameter) {

jboolean isCopy;
const char *param = env->GetStringUTFChars(parameter, JNI_FALSE);
char temp[127];

sprintf(temp, "Hello %s!", param);
jstring result = env->NewStringUTF(temp);

env->ReleaseStringUTFChars(parameter, param);

return result;
}

Now we have to setup the project options. Open project properties (right click on project and project options). Tab Directories, subtab Include Directories add C:\Program Files\Java\jdk1.7.0\include and C:\Program Files\Java\jdk1.7.0\include\win32 (click the folder icon, choose folder and add button).


Hit in menu Execute->Rebuild All and there should be MyFirst.dll created.


Finally refresh the project in Eclipse and hit the earlier prepared runner configuration. The End. If you found it usefull live me a note ;) Maybe I will write something more when I will play around.