Can I run a system native method?

(Sorry for the cross-posting. I put this in advanced-topics before I discovered this forum).

This method definition exists in ObjectInputStream.java:

private static native Object allocateNewObject(Class aclass, Class initclass)

throws InstantiationException, IllegalAccessException;

I want to use this same method. I can compile with the above definition in my source with no problem. When I try to run it I get a dll missing error.

I checked every occurance of loadLibrary in jdk1.3.1 source and found nothing that looks useful.

How do I call this native system method as ObjectInputStream.java does?

[658 byte] By [mchahn] at [2007-9-26 1:35:14]
# 1

Ok, I think I have a solution for you using jni. You'll need a C/C++ compiler (I've used Visual C++).

I've tested it on Windows 2000 server, but should be more or less portable. Forgive me if I'm making an utmost fool of myself it there are much better/easier solutions, but I can't come up with a better one. At least my code works for me.

Also note that there might be extra code required (exception handling, ...).

Here it comes:

1) create a file named New.java:import java.lang.reflect.*;

public class New {

static {

System.loadLibrary("ObjectHandler");

}

public static void main (String[] args) {

try {

Object obj;

obj = allocNewObject (Class.forName ("java.awt.List"));

printMethods (obj);

}

catch (Exception ex) {

System.out.println ("Exception occurred: " + ex);

ex.printStackTrace();

}

}

// simple function to test reflection

static void printMethods(Object obj) {

Method[] methods = obj.getClass().getMethods();

for (int j=0; j < methods.length; j++) {

System.out.println("Method " + methods[j].getName() +

" returns " + methods[j].getReturnType().getName());

}

}

public static native Object allocNewObject (Class aClass);

}

2) create a file named New.h:#include <jni.h>

#ifndef _Included_New

#define _Included_New

#ifdef __cplusplus

extern "C" {

#endif

/*

* Class:New

* Method:allocNewObject

* Signature: (Ljava/lang/Class;Ljava/lang/Class;)Ljava/lang/Object;

*/

JNIEXPORT jobject JNICALL Java_New_allocNewObject

(JNIEnv *, jclass, jclass);

#ifdef __cplusplus }

#endif

#endif

3) create a file named ObjectHandlerFuncs.cpp:#include <stdio.h>

#include "New.h"

JNIEXPORT jobject JNICALL Java_New_allocNewObject

(JNIEnv *env, jclass cl, jclass aClass) {

jobject obj = env->AllocObject (aClass);

return obj;

}

4) Start Visual C++, and create a new project. Name it "ObjectHandler". Follow the instructions from the jdk or website to link your project to the right jni library and imports. Then, add the files New.h and ObjectHandlerFuncs.cpp to this project. Compile the project, resulting in a file named ObjectHandler.dll. Copy this file into the same directory as the previously created classfile.

5) Run the program. This is a part from the output for "java.awt.List":

Method add returns void

Method add returns void

Method remove returns void

Method remove returns void

Method clear returns void

Method removeAll returns void

Method addNotify returns void

Method removeNotify returns void

Method getSelectedIndexes returns [I

Method getItemCount returns int

Method countItems returns int

Method getItem returns java.lang.String

Method getItems returns [Ljava.lang.String;

Method addItem returns void

Method addItem returns void

Method replaceItem returns void

Method delItem returns void

Method delItems returns void

Method getSelectedIndex returns int

Method getSelectedItem returns java.lang.String

Method getSelectedItems returns [Ljava.lang.String;

Method getSelectedObjects returns [Ljava.lang.Object;

Good luck!

If you have any questions/compiler errors, please post them and I'll look for a solution.

Kurt.

leukbr at 2007-6-29 2:19:21 > top of Java-index,Java HotSpot Virtual Machine,Specifications...