We help IT Professionals succeed at work.


rajeevmohan asked
Medium Priority
Last Modified: 2013-11-23
I have a native method with following signature
public native Vector dvdls(String filePath)
  throws IOException;

This function is meant to return, name of all the files and directories in the filePath directory.

Am implementing this native interface in my C program. I have the list of files and directories present in "filePath" in char array. I want to know how to pass this Char array back to Java as a Vector?

Thanks in advance.

Watch Question

You need to call back to the JVM and ask it to create a Vector for you. Then you need to add all characters to the vector.

I have the feeling, this is not a good idea. Try to create an Java array instead and fill it with characters.
It's a pain to construct objects in your JNI lib.  Changing the signature to:

public native void dvdls(String filePath, Vector list);

will save you at least the pain of constructing a new vector object.  then all you have to do is create string objects, and invoke the "add" method on the passed-in vector.


package_name_generated_dvdls(JNIEnv* env, jobject path, jobject vector) {
     // extract the string from path and figure out the list
     // ...
     char** pathList;
     // ...

     jclass vclass = env->FindClass("Ljava/util/Vector");
     jmethodID add = env->GetMethodID(vclass, "add", "(add signature here)");
     for(int i=0;i < npaths;i++) {
          // error and exception checking omitted...
          jboolean added = env->CallBooleanMethod(env, vector, add, env->NewStringUTF(pathList[i]);

You can also omit the Vector argument and create one yourself, see http://java.sun.com/docs/books/tutorial/native1.1/TOC.html for details.
Jim CakalicSenior Engineer

Hmm. I guess there is a reason you don't want to use the File class to do this? File has a method list that returns a String[] of all the files and directories in the directory. Assuming you ultimately wanted File objects, there is also a listFiles method that returns a File[]. You could use it like this:

    File file = new File(filePath);
    String[] files = file.list();
    if (files == null) {
        // this wasn't a directory

Alternately, you can check to see if the path is to a directory first:

    File file = new File(filePath);
    if (file.isDirectory()) {
        String[] files = file.list();
        // do something with the filenames

Assuming you want to turn the array into a Vector and you're using JDK 1.2 or better, you can do this:

    Vector fileList = new Vector(Arrays.asList(files));

If you're stuck prior to JDK 1.2, you have to do the copy manually:

    Vector fileList = new Vector(files.length);
    for (int i = files.length - 1; i >= 0; --i) {
        fileList.add(i, files[i]);

Anyway, my real point is that File can do what you want. So I'm just curious why you don't want to use it.

Best regards,
Jim Cakalic


I did the following way.
I changed the signature of dvdls as follows
void dvdls(String filepath)
And in the C implementation of dvdls, I call a Java function ( present in the JNI interface class) which will create a Vector and fill it. Like this

private Vector list;

public sendArrayResults(String result[]) {
list = new Vector(result.length);
//add elements in result array to this vector

This way I omitted passing Vector as a parameter but I needed one more function.

Thanks for the help.


Explore More ContentExplore courses, solutions, and other research materials related to this topic.