Exception while calling native method

Hi All,

My problem is that i am able to load the dll library but when i am calling native methos in my java code its throwing exception saying--

Exception in thread "main" java.lang.UnsatisfiedLinkError: Initialize

at testmain.Main.Initialize(Native Method)

at testmain.Main.main(Main.java:120)

where initialize is the native method i am calling.

Please help.

Cheers:

Akash

[429 byte] By [Akash5495a] at [2007-11-27 0:35:11]
# 1

> Hi All,

> My problem is that i am able to load the

> dll library but when i am calling native methos in

> my java code its throwing exception saying--

>

> Exception in thread "main"

> java.lang.UnsatisfiedLinkError: Initialize

> at testmain.Main.Initialize(Native Method)

> at testmain.Main.main(Main.java:120)

>

> where initialize is the native method i am calling.

Impossible to diagnose without seeing you Java native method declaration, your header file and your native implementation.

My first guess would be that you created your header file using the 'javah' tool but did not pass the fully qualified name of the class (testmain.Main) as the argument. The 'javah' tool will not complain and will produce an incorrect header file.

Jim S.

Niceguy1a at 2007-7-11 22:43:24 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 2

Hi Below is my all files

//Main Class

package javaapplication8;

class Main

{

public static void main(String args[]) throws java.lang.UnsatisfiedLinkError

{

NTProcess ps=new NTProcess();

if (ps.Initialize())

{

int[] pids=ps.EnumProcesses();

if (pids!=null){

System.out.println("List of Processes ....");

for (int i=0;i<pids.length;i++){

int hProcess=ps.OpenProcess(pids);

if (hProcess!=0){

int[] hModule=ps.EnumProcessModules(hProcess);

if (hModule!=null){

System.out.println("PID: " + pids + " Image Name: " +ps.GetModuleBaseName(hProcess,hModule[0]));

}

};

ps.CloseHandle(hProcess);

}

}

}

}

}

//End Main Class

//NTProcess.java

package javaapplication8;

import java.lang.reflect.Field;

import java.lang.SecurityManager;

import java.lang.SecurityException;

import java.util.PropertyPermission;

public class NTProcess{

public static native boolean Initialize();

public static native int[] EnumProcesses();

public static native int OpenProcess(int Pid);

public static native int[] EnumProcessModules(int hProcess);

public static native String GetModuleFileName(int hProcess,int hModule);

public static native String GetModuleBaseName(int hProcess,int hModule);

public static native MemoryInfo GetProcessMemoryInfo(int hProcess);

public static native boolean CloseHandle(int handle);

static{

Field field=null;

Object original=null;

Class clazz=null;

boolean accessible=false;

try {

clazz = ClassLoader.class;

field = clazz.getDeclaredField("sys_paths");

accessible = field.isAccessible();

if (!accessible)

field.setAccessible(true);

original = field.get(clazz);

field.set(clazz, null);

System.setProperty("java.library.path", "C:\\Projects\\DynamicLibrary8\\dist");

System.loadLibrary("DynamicLibrary8");

System.out.println("java.library.path-->"+System.getProperty("java.library.path"));

}catch(Exception e)

{

System.out.println("Exception from>"+e);

}

finally

{

try{

field.set(clazz, original);

field.setAccessible(accessible);

}catch(Exception e){

System.out.println("Hi-->");

}

}

}

NTProcess(){};

}

//MemoryInfo.java

package javaapplication8;

/**

* Class MemoryInfo

* Author: Iulian Tarhon

* Date : May, 1999

*/

public class MemoryInfo{

int cb;

int PageFaultCount;

int PeakWorkingSetSize;

int WorkingSetSize;

int QuotaPeakPagedPoolUsage;

int QuotaPagedPoolUsage;

int QuotaPeakNonPagedPoolUsage;

int QuotaNonPagedPoolUsage;

int PagefileUsage;

int PeakPagefileUsage;

};

//Process.c

#include <windows.h>

#include <string.h>

#include <jni.h>

#include "NTProcess.h"

#define MaxProcessNumber 1024

typedef BOOL (WINAPI *ENUMPROCESSES)(

DWORD * lpidProcess,

DWORD cb,

DWORD * cbNeeded

);

typedef BOOL (WINAPI *ENUMPROCESSMODULES)(

HANDLE hProcess,

HMODULE * lphModule,

DWORD cb,

LPDWORD lpcbNeeded

);

typedef DWORD (WINAPI *GETMODULEFILENAMEEXA)(

HANDLE hProcess,

HMODULE hModule,

LPTSTR lpstrFileName,

DWORD nSize

);

typedef DWORD (WINAPI *GETMODULEBASENAME)(

HANDLE hProcess,

HMODULE hModule,

LPTSTR lpstrFileName,

DWORD nSize

);

typedef struct _PROCESS_MEMORY_COUNTERS {

DWORD cb;

DWORD PageFaultCount;

DWORD PeakWorkingSetSize;

DWORD WorkingSetSize;

DWORD QuotaPeakPagedPoolUsage;

DWORD QuotaPagedPoolUsage;

DWORD QuotaPeakNonPagedPoolUsage;

DWORD QuotaNonPagedPoolUsage;

DWORD PagefileUsage;

DWORD PeakPagefileUsage;

} PROCESS_MEMORY_COUNTERS;

typedef PROCESS_MEMORY_COUNTERS *PPROCESS_MEMORY_COUNTERS;

typedef BOOL (WINAPI *GETPROCESSMEMORYINFO)(

HANDLE hProcess,

PPROCESS_MEMORY_COUNTERS ppsmenCounters,

DWORD cb

);

ENUMPROCESSES EnumProcesses;

ENUMPROCESSMODULES EnumProcessModules;

GETMODULEFILENAMEEXA GetModuleFileNameExA;

GETMODULEBASENAME GetModuleBaseName;

GETPROCESSMEMORYINFO GetProcessMemoryInfo;

BOOL APIENTRY DllMain(HANDLE hInst, DWORD ul_reason_being_called, LPVOID lpReserved){

return TRUE;

};

/*

* Class:NTProcess

* Method:Initialize

* Signature: ()Z

*/

JNIEXPORT jboolean JNICALL Java_NTProcess_Initialize(JNIEnv * env,jclass clazz){

HANDLE hpsapi=LoadLibrary("PSAPI.DLL");

if (hpsapi==NULL) return FALSE;

EnumProcesses=(ENUMPROCESSES)GetProcAddress((HINSTANCE)hpsapi,"EnumProcesses");

GetModuleFileNameExA = (GETMODULEFILENAMEEXA)GetProcAddress((HINSTANCE)hpsapi, "GetModuleFileNameExA");

GetModuleBaseName = (GETMODULEBASENAME)GetProcAddress((HINSTANCE)hpsapi, "GetModuleBaseNameA");

EnumProcessModules = (ENUMPROCESSMODULES)GetProcAddress((HINSTANCE)hpsapi, "EnumProcessModules");

GetProcessMemoryInfo = (GETPROCESSMEMORYINFO)GetProcAddress((HINSTANCE)hpsapi, "GetProcessMemoryInfo");

if (

NULL == EnumProcesses||

NULL == GetModuleFileName||

NULL == GetModuleBaseName||

NULL == EnumProcessModules )

return FALSE;

return TRUE;

};

/*

* Class:NTProcess

* Method:EnumProcesses

* Signature: ()[I

*/

JNIEXPORT jintArray JNICALL Java_NTProcess_EnumProcesses(JNIEnv * env, jclass clazz){

DWORD aPids[MaxProcessNumber];

DWORD cGot;

jintArray Pids=0;

if(EnumProcesses(aPids,sizeof(aPids),&cGot)){

cGot /= sizeof(aPids[0]);

Pids= (*env)->NewIntArray(env,cGot);

(*env)->SetIntArrayRegion(env,Pids,0,cGot,(jint*) aPids);

};

return Pids;

};

/*

* Class:NTProcess

* Method:OpenProcess

* Signature: (IZI)I

*/

JNIEXPORT jint JNICALL Java_NTProcess_OpenProcess (JNIEnv * env, jclass clazz,jint Pid){

return (jint) OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,FALSE,Pid);

};

/*

* Class:NTProcess

* Method:EnumProcessModules

* Signature: (I[I)[I

*/

JNIEXPORT jintArray JNICALL Java_NTProcess_EnumProcessModules (JNIEnv * env, jclass clazz, jint hProcess){

HMODULE hModule[MaxProcessNumber];

jintArray jModule=0;

DWORD cGot;

if (EnumProcessModules((HANDLE)hProcess,hModule,sizeof(hModule),&cGot)){

cGot/= sizeof(hModule[0]);

jModule= (*env)->NewIntArray(env,cGot);

(*env)->SetIntArrayRegion(env,jModule,0,cGot,(jint*)hModule);

};

return jModule;

};

/*

* Class:NTProcess

* Method:GetModuleFileName

* Signature: (II)Ljava/lang/String;

*/

JNIEXPORT jstring JNICALL Java_NTProcess_GetModuleFileName(JNIEnv * env, jclass clazz, jint hProcess, jint hModule){

jstring jName=0;

char FileName[MAX_PATH];

if(GetModuleFileNameExA((HANDLE)hProcess,(HMODULE)hModule,FileName,sizeof(FileName))!=0){

jName=(*env)->NewStringUTF(env,FileName);

};

return jName;

};

/*

* Class:NTProcess

* Method:GetModuleBaseName

* Signature: (II)Ljava/lang/String;

*/

JNIEXPORT jstring JNICALL Java_NTProcess_GetModuleBaseName(JNIEnv * env, jclass clazz, jint hProcess, jint hModule){

jstring jName=0;

char FileName[MAX_PATH];

if(GetModuleBaseName((HANDLE)hProcess,(HMODULE)hModule,FileName,sizeof(FileName))!=0){

jName=(*env)->NewStringUTF(env,FileName);

};

return jName;

};

JNIEXPORT jboolean JNICALL Java_NTProcess_CloseHandle(JNIEnv * env, jclass clazz, jint handle)

{

return CloseHandle((HANDLE) handle);

};

/*

* Class:NTProcess

* Method:GetProcessMemoryInfo

* Signature: (I)LMemoryInfo;

*/

JNIEXPORT jobject JNICALL Java_NTProcess_GetProcessMemoryInfo(JNIEnv * env, jclass clazz, jint hProcess){

jfieldID jfield;

jobject jobj=0;

PROCESS_MEMORY_COUNTERS pmc;

if(hProcess ==0) return 0;

if ( GetProcessMemoryInfo((HANDLE) hProcess, &pmc, sizeof(pmc)) ){

clazz=(*env)->FindClass(env,"MemoryInfo");

if (clazz==0) return 0;

jobj = (*env)->AllocObject (env,clazz);

//set MemoryInfo object field

jfield=(*env)->GetFieldID(env,clazz,"cb","I");

(*env)->SetIntField (env,jobj, jfield,pmc.cb);

jfield=(*env)->GetFieldID(env,clazz,"PageFaultCount","I");

(*env)->SetIntField (env,jobj, jfield,pmc.PageFaultCount);

jfield=(*env)->GetFieldID(env,clazz,"PeakWorkingSetSize","I");

(*env)->SetIntField (env,jobj, jfield,pmc.PeakWorkingSetSize);

jfield=(*env)->GetFieldID(env,clazz,"WorkingSetSize","I");

(*env)->SetIntField (env,jobj, jfield,pmc.WorkingSetSize);

jfield=(*env)->GetFieldID(env,clazz,"QuotaPeakPagedPoolUsage","I");

(*env)->SetIntField (env,jobj, jfield,pmc.QuotaPeakPagedPoolUsage);

jfield=(*env)->GetFieldID(env,clazz,"QuotaPagedPoolUsage","I");

(*env)->SetIntField (env,jobj, jfield,pmc.QuotaPagedPoolUsage);

jfield=(*env)->GetFieldID(env,clazz,"QuotaPeakNonPagedPoolUsage","I");

(*env)->SetIntField (env,jobj, jfield,pmc.QuotaPeakNonPagedPoolUsage);

jfield=(*env)->GetFieldID(env,clazz,"QuotaNonPagedPoolUsage","I");

(*env)->SetIntField (env,jobj, jfield,pmc.QuotaNonPagedPoolUsage);

jfield=(*env)->GetFieldID(env,clazz,"PagefileUsage","I");

(*env)->SetIntField (env,jobj, jfield,pmc.PagefileUsage);

jfield=(*env)->GetFieldID(env,clazz,"PeakPagefileUsage","I");

(*env)->SetIntField (env,jobj, jfield,pmc.PeakPagefileUsage);

};

return jobj;

};

Please tel me why the exception in coming when calling first method itself.

Cheers:

Akash.

Akash5495a at 2007-7-11 22:43:24 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 3

Two things:

1) The Native method names donot match the java method names, it's missing the package name.

For Ex:

JNIEXPORT jboolean JNICALL Java_NTProcess_Initialize

should be

JNIEXPORT jboolean JNICALL Java_javaapplication8_NTProcess_Initialize

2) The static native methods should be accessed in a static way.

you don't need an NTProcess object to call native methods. You can remove this line

NTProcess ps=new NTProcess();

and call native methods directly by class name.

Ex:

nt[] pids=ps.EnumProcesses();

should be

nt[] pids=NTProcess.EnumProcesses();

Hope this helps.

kteegalaa at 2007-7-11 22:43:24 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 4
Thank you Very Much...its Running.After One week!!!!!!!!!!!!!!!!!!!!!Thx Again..
Akash5495a at 2007-7-11 22:43:24 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 5
Good to know that. When you run the 'javah' , prepend the package name to the class name and that should produce correct header files.javah.exe javaapplication8.NTProcess
kteegalaa at 2007-7-11 22:43:24 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 6

HI,

Now i am calling the below mwthod but showing Class not fount Exception

System.out.println(ps.GetProcessMemoryInfo( hProcess));//Exception

System.out.println("Page Fault Count->"+mi.PageFaultCount);

Exceptuion is--

Exception in thread "main" java.lang.NoClassDefFoundError: MemoryInfo

at javaapplication8.NTProcess.GetProcessMemoryInfo(Native Method)

at javaapplication8.Main.main(Main.java:26)

Please help me.

Akash5495a at 2007-7-11 22:43:24 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 7

> HI,

> Now i am calling the below mwthod but showing

> Class not fount Exception

> System.out.println(ps.GetProcessMemoryInfo(

> hProcess));//Exception

>

> ystem.out.println("Page Fault

> Count->"+mi.PageFaultCount);

>

> Exceptuion is--

> Exception in thread "main"

> java.lang.NoClassDefFoundError: MemoryInfo

> at

> javaapplication8.NTProcess.GetProcessMemoryInfo(Native

> Method)

> at javaapplication8.Main.main(Main.java:26)

> se help me.

Post the native code for the 'GetProcessMemoryInfo' method.

Niceguy1a at 2007-7-11 22:43:24 > top of Java-index,Java HotSpot Virtual Machine,Specifications...