?
Solved

Plese help me! A problem about invoking java code in C++ Builder use JNI. Thanks!

Posted on 2003-03-12
17
Medium Priority
?
420 Views
Last Modified: 2013-11-23
Sorry, I am not good at English.


////////////////////////////////////////////////////////////////
//Index.java
////////////////////////////////////////////////////////////////
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.DateField;
import org.apache.lucene.analysis.*;

import org.apache.lucene.analysis.cn.*;
import org.apache.lucene.analysis.cn.ChineseAnalyzer;

import java.util.Hashtable;
import java.util.Date;
import java.io.File;
import java.io.Reader;
import java.io.FileInputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.lang.String;
import javax.swing.JOptionPane;

class FileDocument
{
  public static Document Document(File f)
       throws java.io.FileNotFoundException
  {

    Document doc = new Document();
    doc.add(Field.Text("path", f.getPath()));
    doc.add(Field.Keyword("modified",
                    DateField.timeToString(f.lastModified())));
    FileInputStream is = new FileInputStream(f);
    Reader reader = new BufferedReader(new InputStreamReader(is));
    doc.add(Field.Text("contents", reader));
    return doc;
  }

  private FileDocument() {}
}

public class Index
{
        public static void main(String args[])
        {
            File TxtFile;
            String AppPath = System.getProperty("user.dir")+"\\";
            String ResultFullPath = AppPath + "result.txt";
            
            TextPath = args[0];
            IndexPath = args[1];

            JOptionPane.showMessageDialog(null, TextPath+IndexPath, "alert", JOptionPane.ERROR_MESSAGE);
            
            try
            {
                  IndexWriter awriter = new IndexWriter(IndexPath, new StandardAnalyzer(), true);
                  indexDocs(awriter, new File(TextPath));
                  awriter.optimize();
                  awriter.close();
            } catch (Exception e)
            {
                    ;;
            }
        }
      public static void indexDocs(IndexWriter writer, File file) throws Exception
      {
        if (file.isDirectory())
        {
          String[] files = file.list();
          for (int i = 0; i < files.length; i++)
            indexDocs(writer, new File(file, files[i]));
        } else
        {
          System.out.println("adding " + file);
          writer.addDocument(FileDocument.Document(file));
        }
      }
}

Compile and run in command line success!

////////////////////////////////////////////////////////////////
//C++ Builder Code
////////////////////////////////////////////////////////////////

#define MAIN_CLASS  "Index"

void __fastcall TMainForm::Button1Click(TObject *Sender)
{
    char infor[255];
      jmethodID mid;
      jclass cls;
      int result;
      jint square;
      jboolean not;

    if(MainForm->jvm == NULL || MainForm->env == NULL) return;
      cls = MainForm->env->FindClass(MAIN_CLASS);
      if( cls != 0 )
    {
        mid = MainForm->env->GetStaticMethodID(cls, "main", "([Ljava/lang/String;)V");
            if( mid!= 0 )
            {
                    int i= 1, ret;
                  jstring TxtFileName, IndexPath;
                  jobjectArray args;
                  char *WinStr;

                  WinStr = "E:\\\\GoleDic\\\\text\\\\";
                  TxtFileName = WindowsTojstring(MainForm->env, WinStr);

                  WinStr = "E:\\\\GoleDic\\\\index\\\\";
                  IndexPath = WindowsTojstring(MainForm->env, WinStr);

                  args= MainForm->env->NewObjectArray(2, MainForm->env->FindClass("java/lang/String"), 0);
                  MainForm->env->SetObjectArrayElement(args, 0, TxtFileName);
                  MainForm->env->SetObjectArrayElement(args, 1, IndexPath);

                  MainForm->env->CallStaticVoidMethod(cls, mid, args);
            }
    }
      return;
}


But when invoking the java code in C++ Builder,

"MainForm->env->FindClass(MAIN_CLASS)" return NULL, it can't find this class named "Index"!

when I change the java code to such this:

            ...
            
            try
            {
                  /*
                  IndexWriter awriter = new IndexWriter(IndexPath, new StandardAnalyzer(), true);
                  indexDocs(awriter, new File(TextPath));
                  awriter.optimize();
                  awriter.close();
                  */
            } catch (Exception e)
            {
                    ;;
            }
            
            ...
            
"MainForm->env->FindClass(MAIN_CLASS)" will find this class!

I should also mention the my knowledge in java and my english is very poor, so the answer could be something very simple!

Please advise, thanks!

0
Comment
Question by:fjhst
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 4
17 Comments
 

Author Comment

by:fjhst
ID: 8124892
why i just have 50 points? what does it mean?
0
 

Author Comment

by:fjhst
ID: 8158517
Plese Help Me!
0
 
LVL 9

Expert Comment

by:Venci75
ID: 8158586
Is this something related to the lucene classes?
How do you start the jvm? Are the lucene classes in the classpath?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:fjhst
ID: 8163864
/////////////////////////////////////////////////////////
//Create Java Virtual machine:
/////////////////////////////////////////////////////////


void __fastcall TMainForm::FormCreate(TObject *Sender)
{
    jint ret = -1;
    HMODULE hLib;
    JavaVMInitArgs vm_args;
    JavaVMOption options[2];
    JNI_CREATEJAVAVM JNI_CreateJavaVM = NULL;

    Memo1->Clear();

    options[0].optionString = "-Djava.compiler=NONE";
    options[1].optionString = "-Djava.class.path=.";

    vm_args.version = JNI_VERSION_1_4;
    vm_args.options = options;
    vm_args.nOptions = 2;
    vm_args.ignoreUnrecognized = JNI_FALSE;

    MainForm->jvm = NULL;
    MainForm->env = NULL;

    hLib = LoadLibrary("C:\\Program Files\\Java\\j2re1.4.1_02\\bin\\client\\jvm.dll");
    if(hLib == NULL)
    {
        Memo1->Lines->Add("Can't load jvm.dll!");
        return;
    }

    /*Create Java VM*/
    JNI_CreateJavaVM = (JNI_CREATEJAVAVM) GetProcAddress(hLib, "JNI_CreateJavaVM");
    if(JNI_CreateJavaVM != NULL)
    {
        ret = (*JNI_CreateJavaVM)(&MainForm->jvm,(void**)&MainForm->env,&vm_args);
        if(ret<0)
        {
            MainForm->jvm = NULL;
            MainForm->env = NULL;
            Memo1->Lines->Add("Sorry,JVM is not set up!");
        }else
        {
            Memo1->Lines->Add("Hello,JVM!");
        }
    }
    else
    {
        Memo1->Lines->Add("Can't Get JNI_CreatJavaVM address!");
        return;
    }

    return;
}

/////////////////////////////////////////////////////////
//Destroy Java Virtual machine:
/////////////////////////////////////////////////////////
void __fastcall TMainForm::FormDestroy(TObject *Sender)
{
    if(MainForm->jvm != NULL && MainForm->env != NULL)
    {
         MainForm->jvm->DestroyJavaVM();
    }
}

I am using the Windows 2000 profession, the AUTOEXEC.BAT file is:
set path=c:\j2sdk1.4.1_02\bin;%path%
set classpath=.;c:\j2sdk1.4.1_02\lib\tools.jar;.;C:\lucene\lucene.jar;
set JAVA_HOME=c:\j2sdk1.4.1_02
set PATH=%PATH%;%ANT_HOME%\bin

I checked the value and it contains the right directories.

Is there anything obvious in the code that is wrong?
Are the options OK?
Has anybody experienced the same behavior?

Any help or workaround will be greatly appreciated

0
 

Author Comment

by:fjhst
ID: 8163868
in addition, the main.h is:

class TMainForm : public TForm
{
__published:     // IDE-managed Components
    TButton *Button1;
    TMemo *Memo1;
    TButton *Button2;
    void __fastcall Button1Click(TObject *Sender);
    void __fastcall FormCreate(TObject *Sender);
    void __fastcall FormDestroy(TObject *Sender);
    void __fastcall Button2Click(TObject *Sender);
private:     // User declarations
     JNIEnv *env;
     JavaVM *jvm;
public:          // User declarations
    __fastcall TMainForm(TComponent* Owner);
};
0
 

Author Comment

by:fjhst
ID: 8164386
Anyone can download this project from here:
http://huatao.xiloo.com/GoleDic.rar
0
 
LVL 9

Expert Comment

by:Venci75
ID: 8164804
I think that this is the line that causes the problem:
options[1].optionString = "-Djava.class.path=.";

the class path must contain the lucene jar - otherwise you won't be able to load the MAIN_CLASS because it references some lucene classes. The line should be something like this:
options[1].optionString = "-Djava.class.path=.;c:/.../lucene.jar";


0
 

Author Comment

by:fjhst
ID: 8164988
but when I change the options[1].optionString  to
options[1].optionString = "-Djava.class.path=.; C:\lucene\lucene.jar";

MainForm->env->FindClass(MAIN_CLASS) also return NULL!
0
 

Author Comment

by:fjhst
ID: 8164996
options[1].optionString = "-Djava.class.path=.; C:/lucene/lucene.jar";

MainForm->env->FindClass(MAIN_CLASS) return NULL too!

0
 
LVL 9

Accepted Solution

by:
Venci75 earned 200 total points
ID: 8165013
are you sure that there is only one lucene jar? if the jars are more than one - then you will have to add them to the classpath as well.
Also you should not have ' ' afther the ';' in the classpath:
options[1].optionString = "-Djava.class.path=.;C:\lucene\lucene.jar";

and finally - in C you must escape the '\' with '\\' - so your classpath should look like this:
options[1].optionString = "-Djava.class.path=.;C:\\lucene\\lucene.jar";

0
 

Author Comment

by:fjhst
ID: 8165193
Let me try thus...
0
 

Author Comment

by:fjhst
ID: 8165205
yes, just only one class: lucene.jar
0
 

Author Comment

by:fjhst
ID: 8165220
I RESOLVE THIS PROBLEM!!!!!!!!!
thank you very much!!!!!!!!!!!!
I RESOLVE THIS PROBLEM!!!!!!!!!
thank you very much!!!!!!!!!!!!
I RESOLVE THIS PROBLEM!!!!!!!!!
thank you very much!!!!!!!!!!!!
I RESOLVE THIS PROBLEM!!!!!!!!!
thank you very much!!!!!!!!!!!!
I RESOLVE THIS PROBLEM!!!!!!!!!
thank you very much!!!!!!!!!!!!
I RESOLVE THIS PROBLEM!!!!!!!!!
thank you very much!!!!!!!!!!!!
I RESOLVE THIS PROBLEM!!!!!!!!!
thank you very much!!!!!!!!!!!!
I RESOLVE THIS PROBLEM!!!!!!!!!
thank you very much!!!!!!!!!!!!
I RESOLVE THIS PROBLEM!!!!!!!!!
thank you very much!!!!!!!!!!!!
I RESOLVE THIS PROBLEM!!!!!!!!!
thank you very much!!!!!!!!!!!!
I RESOLVE THIS PROBLEM!!!!!!!!!
thank you very much!!!!!!!!!!!!

0
 

Expert Comment

by:CleanupPing
ID: 9058729
fjhst:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
LVL 9

Expert Comment

by:Venci75
ID: 9360848
As I see from the fjhst's comment - the problem is resolved and since I was the only one participated in this question - I think that the solution was mine
0

Featured Post

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question