JNI : calling native code from java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shoikatroy
    New Member
    • Jun 2010
    • 2

    JNI : calling native code from java

    hi i just wanted to parse a file to find certain associated data
    for ex if my text file is :
    <!#first>This is string1
    <!second>THis is string2
    So if user types in first...he will get This is string 1
    iv written a simple C++ code to parse the text file
    but now i need to call that native code from a java source
    i tried to write the native function but dont have much idea abt JNI so could someone help me out pls...


    jstring
    Java_com_exampl e_hellojni_Hell oJni_stringFrom JNI( JNIEnv* env,
    jobject thiz,jstring javaString )
    {



    using namespace std;
    const char *nativeString = env->GetStringUTFCh ars(javaString, 0);
    string val1,val2;
    string inp(nativeStrin g);
    cout<<"enter:";
    cin>>inp;
    string line;
    ifstream myfile ("data.txt") ;
    if(myfile.is_op en())
    {
    while(!myfile.e of())
    {
    getline(myfile, line,'<');

    istringstream iss1(line);
    getline(iss1,va l1,'>');
    val1.erase(0,2) ;
    getline(iss1,va l2);

    if(inp.compare( val1)==0)
    cout<<"\n Our returned string is : "<<val2;
    }
    }
    char buffer[20]
    strcpy(buffer, val2.c_str());


    return *env->NewStringUTF(e nv, buffer);

    }
  • myusernotyours
    New Member
    • Nov 2007
    • 188

    #2
    This more has to do with Java than C/C++, better to ask this kind of question on the Java forum...

    Also you could do this in pure Java. Is there any specific reason for using native code?

    There is a wealth of information that you may want to look at here.

    King Regards,

    Alex.

    Comment

    • shoikatroy
      New Member
      • Jun 2010
      • 2

      #3
      hey thanks...
      theres no real reason as such..
      it can be done with pure java but i wanted to try getting the hang of using native code..so im just trying to learn it myself
      thanks

      Comment

      • myusernotyours
        New Member
        • Nov 2007
        • 188

        #4
        Okay then, I imagined so too. Go ahead and check out that link.
        Get back here if you get into any problems. Good Luck!

        Alex.

        Comment

        Working...