Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

JNI: what is the C++ code to convert a jstring the a regular c string?

2,720 views
Skip to first unread message

Tim Bosley

unread,
Mar 18, 1999, 3:00:00 AM3/18/99
to
How do you convert jstring to c string?

I've tried cutting and pasting the code example from the java tutorial
but it did not work. Does anyone know how to do this.

PS. I don't know very much about C.

Here is a copy of the dll I am trying to make to.
------------------------------------------------------------------------------------------------------

#ifndef _execCom_H
#define _execCom_H
#include <iostream.h>
#include "setDir.h"
#include <jni.h>
#include <stdlib.h>
#include <dir.h>

JNIEXPORT void JNICALL Java_setDir_execute
(JNIEnv *env, jclass, jstring wd, jstring com)
{
char buf[128];
const char *WD = (*env)->GetStringUTFChars(env, wd, 0);
printf("%s", str);
(*env)->ReleaseStringUTFChars(env, wd, str);
chdir(WD);
//system(com);
}
#endif _execCom_H

Rammohan Deeduvanu

unread,
Mar 18, 1999, 3:00:00 AM3/18/99
to
I dont see anything wrong with code. However it could be because of the size
of your array or maybe you are not seeing the printf because you are not
flushing your output stream. Try this.
#include <jni.h>
...
...
...

JNIEXPORT jlong JNICALL Java_rtjc_CMisc_cdNative
(JNIEnv* env, jobject this_obj, jstring dir)
{
char *newDir;

newDir = (*env)->GetStringUTFChars(env, dir, 0);
if(_chdir(newDir)!=0)
printf("Failed to change directory to %s\n",newDir);
else
printf("Directory changed to: %s\n",newDir);
(*env)->ReleaseStringUTFChars(env, dir, newDir);

}

Tim Bosley wrote in message <36F134B0...@hotmail.com>...

Cynthia Freedman

unread,
Mar 18, 1999, 3:00:00 AM3/18/99
to
Tim Bosley wrote:
>
> How do you convert jstring to c string?


You almost have it right. But you need to copy from the character
pointer into a character string. Also, your code snippet is a bit
mixed up in the variables names used: you were referencing "str"
instead of "WD".


JNIEXPORT void JNICALL Java_setDir_execute


JNIEnv *env, jclass, jstring wd, jstring com)
{
char buf[128];
const char *WD = (*env)->GetStringUTFChars(env, wd, 0);

strcpy( buf, WD );
printf("%s", buf);
(*env)->ReleaseStringUTFChars(env, wd, WD);
chdir(buf);
...

sue...@my-dejanews.com

unread,
Apr 1, 1999, 3:00:00 AM4/1/99
to
In article <36F14F8A...@mail.arc.nasa.gov>,
I tried this call in cpp file. I got an compile error, 'error C2819: type
'JNIEnv_' does not have an overloaded member 'operator ->'. I'm using jni.h
from JDK1.1.7B. Do I need to extern "C" jni.h?

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own

Blair Wyman

unread,
Apr 2, 1999, 3:00:00 AM4/2/99
to
> > (*env)->ReleaseStringUTFChars(env, wd, str);
[snip]

> I tried this call in cpp file. I got an compile error, 'error C2819: type
> 'JNIEnv_' does not have an overloaded member 'operator ->'. I'm using jni.h
> from JDK1.1.7B. Do I need to extern "C" jni.h?

This is C not C++ form of call -- to transform just take off a level of
indirection and remove the first parm:

(*env)->INTERFACENAME(env,PARMS);
becomes
env->INTERFACENAME(PARMS);

--
___ _ Blair Wyman bwy...@home.com
( /_) / _ ' _ Systems Programmer
_/__)_/_<_/_/_/_' In continuous operation since 1957.

0 new messages