1

Possible Duplicate:
jni converting jstring to char *

There is a function on С (traverser.c module)

long int
Traverser(const char * sTraversingRoot) 
{
    long int nCount;
    struct stat rStatBuf;
    time_t nTime;
    char sActualPath[512];
    PGconn *pConn;

    // Open DB connection
    sprintf(sConnInfo, 
        "hostaddr=%s port=%s connect_timeout=50 dbname=%s user=%s password=%s",
        sIP, sPort, sDBName, sLogin, sPassword);
    pConn = PQconnectdb(sConnInfo);
    if (PQstatus(pConn) == CONNECTION_BAD) {
        AddErrorToLog("No connect\n");
        return 0;
    }

    GetActualPath(sActualPath, sTraversingRoot);

    if (*sActualPath) {
        stat(sActualPath, &rStatBuf);
    } else {
        stat("/", &rStatBuf);
    }

    if (nClock)
        nTime = time(NULL);

    if(S_ISREG(rStatBuf.st_mode)) {
        nCount = 1;
        ProcessFile(pConn, sActualPath);
    }

    if(S_ISDIR(rStatBuf.st_mode)) {
        nCount = _Traverser(pConn, sActualPath);
    }

    if (nClock)
        fprintf(stdout, "Total time : %u second(s)\n", time(NULL) - nTime);
    // Close DB connection
    PQfinish(pConn);

    return nCount;
}

I want to create native with the same name a method on Java

public native void Traverser(String path)

Respectively in the traverser.c module there will be a function

JNIEXPORT void JNICALL Java_ParallelIndexation_Traverser(JNIEnv *env, jobject obj, jstring path) 

The Java_ParallelIndexation_Traverser function is a Traverser function wrapper from the traverser.c module.The question is: How to call a module from Traverser traverser.c in Java_ParallelIndexation_Traverser, passing it the parameter jstring path, thus converting it to a const char * (signature Traverser see above)?

Community
  • 1
  • 1
user1730626
  • 437
  • 1
  • 8
  • 16
  • 1
    So is what you're really asking , How do I get a char * from a java jstring parameter passed to a JNI function? [This may help](http://stackoverflow.com/questions/4181934/jni-converting-jstring-to-char) – WhozCraig Oct 20 '12 at 17:49
  • 1
    It's better to use reduced problems / code samples in SO questions. It looks like 95% of your code has nothing to do with the problem you're facing. – millimoose Oct 20 '12 at 17:55
  • @WhozCraig , You correctly understood me. – user1730626 Oct 20 '12 at 18:18
  • @user1730626 [Then see this link](http://stackoverflow.com/questions/4181934/jni-converting-jstring-to-char) as your question has ben answered multiple times before on StackOverflow. – WhozCraig Oct 20 '12 at 18:56
  • @WhozCraig And what to do if I want to write a native method Traverser (String path), which returns a value? How do I convert a long int type in C in Java type that returns a native method Traverser (String path)? – user1730626 Oct 21 '12 at 06:26
  • @millimoose And what to do if I want to write a native method Traverser (String path), which returns a value? How do I convert a long int type in C in Java type that returns a native method Traverser (String path)? – user1730626 Oct 21 '12 at 06:27
  • 1
    @user1730626 Just return a regular C value. See here for which C types map to which Java types: http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/types.html#wp15773 (This is a document you should've read back to front *before* you started coding.) – millimoose Oct 21 '12 at 13:50

1 Answers1

0

Did I understand your question correctly: how to implement Java_ParallelIndexation_Traverser so that it calls the unmanaged Traveser function?

If so, here's how:

JNIEXPORT void JNICALL Java_ParallelIndexation_Traverser(JNIEnv* env, jobject obj, jstring path)
{
    const jbyte* path2 = env->GetStringUTFChars(path, nullptr);
    if (path2 == nullptr)
        return;

    ::Traverser(path2);

    env->ReleaseStringUTFChars(path, path2);
}

Edit:

Explanation: JNIEnv::GetStringUTFChars converts a jstring to a byte array. You then need to call JNIEnv::ReleaseStringUTFChars to deallocate that byte array.

user1610015
  • 6,561
  • 2
  • 15
  • 18
  • What is it mean:: and env->ReleaseStringUTFChars(path, path2); ? Please, add the comment to your code. – user1730626 Oct 20 '12 at 18:21
  • ,Why it is possible to call:: Traverser (path2) where path2 has the jbyte type *, after all long int Traverser (const char * sTraversingRoot) has parameters of other type? – user1730626 Oct 20 '12 at 18:40
  • @user1730626 Because jbyte is just a typedef for char. – user1610015 Oct 20 '12 at 18:52
  • What is it mean:: before Traverser(path2);? – user1730626 Oct 20 '12 at 19:05
  • It means to use the Traverser function from the global or anonymous namespace. It's unnecessary in this case, I only used it for clarity. – user1610015 Oct 20 '12 at 19:23
  • And what to do if I want to write a native method Traverser (String path), which returns a value? How do I convert a long int type in C in Java type that returns a native method Traverser (String path)? – user1730626 Oct 21 '12 at 05:24
  • And what to do if I want to write a native method Traverser (String path), which returns a value? How do I convert a long int type in C in Java type that returns a native method Traverser (String path)? – user1730626 Oct 21 '12 at 10:52
  • @user1730626 Don't move the goalposts. If your original question was answered, accept an answer and ask additional questions separately, instead of expanding the scope of one question forever. – millimoose Oct 21 '12 at 13:48