fjhst
asked on
Plese help me! A problem about invoking java code in C++ Builder use JNI. Thanks!
Sorry, I am not good at English.
////////////////////////// ////////// ////////// ////////// ////////
//Index.java
////////////////////////// ////////// ////////// ////////// ////////
import org.apache.lucene.index.In dexWriter;
import org.apache.lucene.analysis .Analyzer;
import org.apache.lucene.analysis .standard. StandardAn alyzer;
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.Chines eAnalyzer;
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.FileNotFoundExcept ion
{
Document doc = new Document();
doc.add(Field.Text("path", f.getPath()));
doc.add(Field.Keyword("mod ified",
DateField.timeToString(f.l astModifie d())));
FileInputStream is = new FileInputStream(f);
Reader reader = new BufferedReader(new InputStreamReader(is));
doc.add(Field.Text("conten ts", reader));
return doc;
}
private FileDocument() {}
}
public class Index
{
public static void main(String args[])
{
File TxtFile;
String AppPath = System.getProperty("user.d ir")+"\\";
String ResultFullPath = AppPath + "result.txt";
TextPath = args[0];
IndexPath = args[1];
JOptionPane.showMessageDia log(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(FileDoc ument.Docu ment(file) );
}
}
}
Compile and run in command line success!
////////////////////////// ////////// ////////// ////////// ////////
//C++ Builder Code
////////////////////////// ////////// ////////// ////////// ////////
#define MAIN_CLASS "Index"
void __fastcall TMainForm::Button1Click(TO bject *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(M AIN_CLASS) ;
if( cls != 0 )
{
mid = MainForm->env->GetStaticMe thodID(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->NewObjectAr ray(2, MainForm->env->FindClass(" java/lang/ String"), 0);
MainForm->env->SetObjectAr rayElement (args, 0, TxtFileName);
MainForm->env->SetObjectAr rayElement (args, 1, IndexPath);
MainForm->env->CallStaticV oidMethod( 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!
//////////////////////////
//Index.java
//////////////////////////
import org.apache.lucene.index.In
import org.apache.lucene.analysis
import org.apache.lucene.analysis
import org.apache.lucene.document
import org.apache.lucene.document
import org.apache.lucene.document
import org.apache.lucene.analysis
import org.apache.lucene.analysis
import org.apache.lucene.analysis
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.FileNotFoundExcept
{
Document doc = new Document();
doc.add(Field.Text("path",
doc.add(Field.Keyword("mod
DateField.timeToString(f.l
FileInputStream is = new FileInputStream(f);
Reader reader = new BufferedReader(new InputStreamReader(is));
doc.add(Field.Text("conten
return doc;
}
private FileDocument() {}
}
public class Index
{
public static void main(String args[])
{
File TxtFile;
String AppPath = System.getProperty("user.d
String ResultFullPath = AppPath + "result.txt";
TextPath = args[0];
IndexPath = args[1];
JOptionPane.showMessageDia
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
writer.addDocument(FileDoc
}
}
}
Compile and run in command line success!
//////////////////////////
//C++ Builder Code
//////////////////////////
#define MAIN_CLASS "Index"
void __fastcall TMainForm::Button1Click(TO
{
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(M
if( cls != 0 )
{
mid = MainForm->env->GetStaticMe
if( mid!= 0 )
{
int i= 1, ret;
jstring TxtFileName, IndexPath;
jobjectArray args;
char *WinStr;
WinStr = "E:\\\\GoleDic\\\\text\\\\
TxtFileName = WindowsTojstring(MainForm-
WinStr = "E:\\\\GoleDic\\\\index\\\
IndexPath = WindowsTojstring(MainForm-
args= MainForm->env->NewObjectAr
MainForm->env->SetObjectAr
MainForm->env->SetObjectAr
MainForm->env->CallStaticV
}
}
return;
}
But when invoking the java code in C++ Builder,
"MainForm->env->FindClass(
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(
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!
ASKER
Plese Help Me!
Is this something related to the lucene classes?
How do you start the jvm? Are the lucene classes in the classpath?
How do you start the jvm? Are the lucene classes in the classpath?
ASKER
////////////////////////// ////////// ////////// ////////// /
//Create Java Virtual machine:
////////////////////////// ////////// ////////// ////////// /
void __fastcall TMainForm::FormCreate(TObj ect *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\\clie nt\\jvm.dl l");
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)(&MainF orm->jvm,( void**)&Ma inForm->en v,&vm_args );
if(ret<0)
{
MainForm->jvm = NULL;
MainForm->env = NULL;
Memo1->Lines->Add("Sorry,J VM is not set up!");
}else
{
Memo1->Lines->Add("Hello,J VM!");
}
}
else
{
Memo1->Lines->Add("Can't Get JNI_CreatJavaVM address!");
return;
}
return;
}
////////////////////////// ////////// ////////// ////////// /
//Destroy Java Virtual machine:
////////////////////////// ////////// ////////// ////////// /
void __fastcall TMainForm::FormDestroy(TOb ject *Sender)
{
if(MainForm->jvm != NULL && MainForm->env != NULL)
{
MainForm->jvm->DestroyJava VM();
}
}
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\too ls.jar;.;C :\lucene\l ucene.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
//Create Java Virtual machine:
//////////////////////////
void __fastcall TMainForm::FormCreate(TObj
{
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
MainForm->jvm = NULL;
MainForm->env = NULL;
hLib = LoadLibrary("C:\\Program Files\\Java\\j2re1.4.1_02\
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)(&MainF
if(ret<0)
{
MainForm->jvm = NULL;
MainForm->env = NULL;
Memo1->Lines->Add("Sorry,J
}else
{
Memo1->Lines->Add("Hello,J
}
}
else
{
Memo1->Lines->Add("Can't Get JNI_CreatJavaVM address!");
return;
}
return;
}
//////////////////////////
//Destroy Java Virtual machine:
//////////////////////////
void __fastcall TMainForm::FormDestroy(TOb
{
if(MainForm->jvm != NULL && MainForm->env != NULL)
{
MainForm->jvm->DestroyJava
}
}
I am using the Windows 2000 profession, the AUTOEXEC.BAT file is:
set path=c:\j2sdk1.4.1_02\bin;
set classpath=.;c:\j2sdk1.4.1_
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
ASKER
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);
};
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);
};
ASKER
Anyone can download this project from here:
http://huatao.xiloo.com/GoleDic.rar
http://huatao.xiloo.com/GoleDic.rar
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.j ar";
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:/..
ASKER
but when I change the options[1].optionString to
options[1].optionString = "-Djava.class.path=.; C:\lucene\lucene.jar";
MainForm->env->FindClass(M AIN_CLASS) also return NULL!
options[1].optionString = "-Djava.class.path=.; C:\lucene\lucene.jar";
MainForm->env->FindClass(M
ASKER
options[1].optionString = "-Djava.class.path=.; C:/lucene/lucene.jar";
MainForm->env->FindClass(M AIN_CLASS) return NULL too!
MainForm->env->FindClass(M
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Let me try thus...
ASKER
yes, just only one class: lucene.jar
ASKER
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!!!!!!!!!!!!
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!!!!!!!!!!!!
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.
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.
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
ASKER