dcsimg
Plese help me! A problem about invoking java code in C++ Builder use JNI. Thanks!
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   fjhst_fjhst
Posted On:   Tuesday, March 18, 2003 01:10 AM

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;    More>>
			
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!
   <<Less

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

Posted By:   fjhst_fjhst  
Posted On:   Tuesday, March 18, 2003 09:28 PM

Anyone can download this project from here:
http://huatao.xiloo.com/GoleDic.rar

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

Posted By:   fjhst_fjhst  
Posted On:   Tuesday, March 18, 2003 04:19 AM

Why No-one answer my question?
About | Sitemap | Contact