P990i CDC JNI

Basically a blog which is a mixture of anything that comes up in the author's mind. From the technologies that makes his life convenient to the everyday news in his country.

P990i CDC JNI

Friday, March 14, 2008

(0) Comments

Finally I was able to run my JNI sample on UIQ3 emulator thanks to this link

http://developer.uiq.com/devlib/uiq_31/SDKDocumentation/doc_source/doc_source/faqSDK/faq_0536.html(bug report)
http://developer.sonyericsson.com/thread.jspa?messageID=115603&tstart=0(JNI steps)

My CODES
[code]
class HelloWorld {
private native String print();
public static void main(String[] args) {
}
public String getString() {
return print();
}
static {
System.loadLibrary("Prompt");
}
}
//native implementation
#include
#include
#include "HelloWorld.h"
JNIEXPORT jstring JNICALL
Java_HelloWorld_print(JNIEnv *env, jobject obj) {
char *s;
s = "alf";
return (env)->NewStringUTF(s);
}
//lookup
#include
#include // Needed for strcmp

#include "HelloWorld.h"


#ifdef __cplusplus
extern "C" {
#endif

typedef void (*TFunc)();

typedef struct
{
char *token;
unsigned int procaddr;
} TABLE_ENTRY;

// lookup_table MUST be defined as 'const' or this file will cause a build error
// when comipling for the ARMI target
const TABLE_ENTRY lookup_table[]=
{
{ "Java_HelloWorld_print", (unsigned int) Java_HelloWorld_print },
};

// table_size MUST be defined as 'const' or this file will cause a build error
// when comipling for the ARMI target
const int table_size = sizeof(lookup_table) / sizeof(TABLE_ENTRY);


IMPORT_C TFunc jni_lookup(const char* aName);
EXPORT_C TFunc jni_lookup(const char* aName)
{
int res=0;
int mid=0;
int top=0;
int bottom=table_size-1;

// Loop while the number of the items left in the list is greater
// than 2. Each iteration will split the number of items left to search
// in half
while ((bottom - top) > 1) {
// This case handles the normal serach case where the number of
// items left to search is greater than 2
mid=(top+bottom)/2;
res=strcmp(aName,lookup_table[mid].token);
if (res==0) return((TFunc) lookup_table[mid].procaddr);
if (res>0) top=mid; else bottom=mid;
}

// If there are two items left in the list then the bottom item should be
// checked for a match
if (bottom != top) {
// Check the bottom item to see if it is a match
res=strcmp(aName,lookup_table[bottom].token);
if (res == 0) return ((TFunc) lookup_table[bottom].procaddr);
}

// Check the top item to see if it is a match
res=strcmp(aName,lookup_table[top].token);

if (res == 0) return ((TFunc) lookup_table[top].procaddr);

// Neither the top or bottom items were a match so the
// method must not exist in the file
return NULL;
}

#ifdef __cplusplus
}
#endif
//build
PRJ_PLATFORMS
gcce winscw

PRJ_EXPORTS
..\inc\Prompt.h

PRJ_MMPFILES
Prompt.mmp
//definition file
EXPORTS
jni_lookup @ 1 NONAME
//mmp
TARGET Prompt.dll
TARGETTYPE dll
UID 0x00000000 0x0A762D89

USERINCLUDE ..\inc
SYSTEMINCLUDE \epoc32\include \epoc32\include\libc

SOURCEPATH ..\src

SOURCE Prompt.cpp
SOURCE PromptDllMain.cpp HelloWorld.cpp lookup.cpp

//By default, the build tools look for the WINSCW def file in a BWINS directory
//(at the same level as the directory containing the mmp file),
//the GCC ARM def file in a BMARM directory, and the ARMV5 def file in a EABI directory.
//If def files are stored in these locations, the project files does not need to specify
//the location of the def files explicitly. If you want to store the def files in some other
//location, you will need to specify in the project file where the .def files are using
//the deffile keyword.

//The following commented out code shows how the build system uses the implicit
// location for defiles. To create the DEF files Choose Project > Freeze Exports from Carbide
// or run 'abld freeze' from the command-line
//#if defined (WINS)
// DEFFILE ..\bwins\Prompt.def
//#elif defined (GCC32)
// DEFFILE ..\bmarm\Prompt.def
//#else
// DEFFILE ..\eabi\Prompt.def
//#endif

nostrictdef

LIBRARY euser.lib estlib.lib

DEFFILE .\ exports.def
MACRO J9EPOC32
#if defined(WINS)
MACRO J9X86
#endif
EXPORTUNFROZEN
//pkg
#{"Prompt DLL"},(0x0A762D89),1,0,0


;Localised Vendor name
%{"Vendor-EN"}

;Unique Vendor name
:"Vendor"

"$(EPOCROOT)Epoc32\release\$(PLATFORM)\$(TARGET)\Prompt.dll" -"!:\system\libs\Prompt.dll"
[/code]

Taken from the site above:
It’s important that you use the CDC Java VM’s versions of these files,
that are distributed with the CDC Java SDK.extension.

1) Run "javah" on all your class-files with natives.

2) Run the "symexports.exe"-tool in a DOS-window.
This tool takes as input arguments all the javah-header
files and an output-file name and creates a .c-file which
has the dll lookup function.
note: symexports is bundled in the UIQ3 SDK

Using the sample-files from the zip-file, the syntax would be as follows:

symexports -h:file1.h -h:file2.h -out:lookup.c

This creates the lookup.c file.

3) The lookup.c file needs to be included as a source-file
with all your other native source-files in your *.mmp-file
that you use to build the Symbian dll. The line below shows this:
SOURCE lookup.c

4) You need to include the jni.h file in all source-files
with JNI natives. The jni.h includes the jniport.h file
which is also required.

5) You also need a def-file, e.g. exports.def.
Put it in your build directory.
It needs to have the following content:

EXPORTS
jni_lookup @ 1 NONAME

6) The .mmp file needs to have the following lines added to it:

DEFFILE .\ exports.def
MACRO J9EPOC32

NOSTRICTDEF

#if defined(WINS)
MACRO J9X86
#endif

EXPORTUNFROZEN

0 Responses to "P990i CDC JNI"

Post a Comment

Contributors