Skip to Main Content

Java HotSpot Virtual Machine

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JNI DLL Problem

843829Dec 8 2009 — edited Dec 16 2009
Hi Friends,
Here is the setup and my problem

My Java File
public class GET {
		//This static block loads all the native methods in to the lib
		static{
				System.loadLibrary("gettime");
		}

		//Declaring all native methods
		native int[] 	cs_gettime(int devIndex,int timeType);

		public static void main(String args[]){
			int [] doytime = new GET().cs_gettime(0,0);
		}
}// End GET
MY Native method Implementation:
//<--Includes-->
#include <jni.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "tsync.h"
#include "tsync_example.h"

//Implement the Native method defined in header file GET.h
JNIEXPORT jintArray JNICALL Java_GET_cs_1gettime(JNIEnv *env, jobject obj, 
	jint devIndex, jint timeType1){

	//Board Variables
		//JNI array:
	//1. Returns time if sucess in reading it from the board
	//2. Else returns error on it last field.
    jintArray time	= (*env)->NewIntArray(env,8);
	TSYNC_BoardHandle      hnd;
    int                    rc;
    int                    devIdx;
    char                  *devName = DEFAULT_DEV;
    char                   fullDevName[32];
    TSYNC_ERROR            err     = TSYNC_SUCCESS;
    ML_TIME_TYPE           timeType;
    TSYNC_TimeObj          doyTime;
    TSYNC_TimeBCDObj       bcdTime;
    unsigned int           seconds;
    unsigned int           ns;
    TSYNC_TimeDSTStateObj  ds;
	//Temporary Buffer to store time
    int			   temp_buffer[8];
	//Set Error bit to false
	temp_buffer[1] = 0;
	
 
    //Construct the TSYNC/TSAT device name 
    devIdx = devIndex;
    (void)sprintf( fullDevName, "%s%s%d", DEVICE_ROOT, devName, devIdx );
  
    
    //Open the device, If error return the array with the error
    //bit set to 1(ON)
    if ( (rc = TSYNC_open(&hnd, fullDevName)) != TSYNC_SUCCESS )
    {
        printf(": !Could not open <%s> : rc <%d>\n",fullDevName, rc);
		temp_buffer[7] = 1;
		(*env)->SetIntArrayRegion( env, time ,0, 8, (jint *)temp_buffer);
		return time;    
    }
	 timeType    = (ML_TIME_TYPE)timeType1;

	 //Check which time Type(DOY, BCD or NS)
	 switch (timeType){
        case ML_TIME_DOYTIME:
            err = TSYNC_CS_getTime(hnd, &doyTime);    
            break;

        case ML_TIME_BCDTIME:
            err = TSYNC_CS_getTimeBcd(hnd, &bcdTime);
            break;

        case ML_TIME_SECONDS:
            err = TSYNC_CS_getTimeSec(hnd, &seconds, &ns);
            break;

        default:
            printf("  Error: Bad time type\n");     
    }

	 // If error in reading time return the array with the error
	 //bit set to 1(ON)
	if (err != TSYNC_SUCCESS){
        printf("  Error: %s.\n", tsync_strerror(err));
		temp_buffer[7] = 1;
		(*env)->SetIntArrayRegion( env, time ,0, 8, (jint *)temp_buffer);
		return time;          
    }

	//Get DST State
    err = TSYNC_CS_getDstState(hnd, &ds);
    if (err != TSYNC_SUCCESS){
        printf("  Error: %s.\n", tsync_strerror(err));
    }


	// Get time and assign it to a native array
	//depending on the timeType selection
    switch (timeType){
        case ML_TIME_DOYTIME:
			temp_buffer[0] = doyTime.years;
			temp_buffer[1] = doyTime.doy;
			temp_buffer[2] = doyTime.hours;
			temp_buffer[3] = doyTime.minutes;
			temp_buffer[4] = doyTime.seconds;
			temp_buffer[5] = doyTime.ns;
			temp_buffer[6] = ds.state;
			//Assign temp_buffer to a native array
			(*env)->SetIntArrayRegion( env, time ,0, 8, (jint *)temp_buffer);
			break;

        case ML_TIME_SECONDS:
			temp_buffer[0] = seconds;
			temp_buffer[1] = ns;
			//Assign temp_buffer to a native array
			(*env)->SetIntArrayRegion(env, time ,0, 1, (jint *)temp_buffer);
            break;

        default:
            break;
    }
	//Close the device, If error return the array with the error bit
	//set to 1(ON)
    if ( (rc = TSYNC_close(hnd)) != TSYNC_SUCCESS ){
        (void) printf(": error closing <%s> rc: <%d>\n",fullDevName, rc );
		temp_buffer[7] = 1;
		(*env)->SetIntArrayRegion( env, time ,0, 8, (jint *)temp_buffer);
		return time;    
    }
	//Return time
    return time;
}
And Finally My make file which I run im Microsoft Visual Studio 2005
CFLAGS=/I . /I ..\..\..\dev /I"c:\java\include" /I"c:\java\include\win32" /MD


all:
	cl -c $(CFLAGS) gettime.c
	link /LIBPATH:..\..\..\dev /MANIFEST /SUBSYSTEM:CONSOLE /MACHINE:X86 tsync.lib kernel32.lib user32.lib gettime.obj /dll
	

clean:
	erase /f *.dll
	erase /f *.exp
	erase /f *.lib
	erase /f *.obj
Now after researching I found out that /MD option if removed might solve many unsatisfied link error. But in my case i need that otherwise it was giving lots of errors

I also Put the msvcr80.dll in C:\windows\system32 as the dependency walker showed me that i was missing that dll

Now I am able to create a dll file with it. the files generated are
getime.dll
gettime.dll.manifest
gettime.lib
gettime.obj
gettime.exp


now when I try to run my program it gives me errors

if I do a java GET or java -Djava.library.path=. GET I get the following error
Exception in thread "main" java.lang.UnsatisfiedLinkError: C:\Program Files\Spectracom\TSYNC PCI\Examples\TSync API\jexp\gettime.dll: A dynamic
 link library (DLL) initialization routine failed
        at java.lang.ClassLoader$NativeLibrary.load(Native Method)
        at java.lang.ClassLoader.loadLibrary0(Unknown Source)
        at java.lang.ClassLoader.loadLibrary(Unknown Source)
        at java.lang.Runtime.loadLibrary0(Unknown Source)
        at java.lang.System.loadLibrary(Unknown Source)
        at GET.<clinit>(GET.java:29)
Could not find the main class: GET.  Program will exit.
Can I get some guidance of where I am going wrong.
Is it that the dll created has more dependent dlls or am I running the java code not properly

Also to note that the same code I was able to run on Ubuntu using gcc and it created a .so file and things worked fine

I dnt knw where am I going wrong here
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 13 2010
Added on Dec 8 2009
24 comments
1,567 views