Class not found in VC6?
Yesterday, under the VC6 & JDK1.5.0_06 environment I tested JNI, encountered the following problems.
After compiling & linking, executed "M_env->FindClass in m_cls = ( "htmlparser_test/Demo"); " the return value m_cls = Null.However, if I remove the code of method pageParser(), everything will be all right.
The following is code of Java & C++:
//Java Code
package htmlparser_test;
import org.htmlparser.Parser;
import org.htmlparser.parserapplications.StringExtractor;
import org.htmlparser.util.ParserException;
import org.htmlparser.beans.StringBean;
public class Demo
{
public String msg = new String("hohoho.......");
public Demo()
{}
public String pageParser(String strRawPage)
{
StringBean sb = new StringBean();
StringBuffer strTxt = new StringBuffer();
int iTmp;
Parser parser = new Parser();
try
{
parser.setInputHTML(strRawPage);
}
catch(ParserException pe)
{
pe.printStackTrace();
}
return sb.getStrings();
}
public Demo(String msg)
{
System.out.println(":" + msg);
this.msg = new String("hohoho.......");
this.counts = null;
}
public String setMessage(String msg)
{
this.msg = msg;
return this.msg;
}
public void throwExcp() throws IllegalAccessException
{
throw new IllegalAccessException("exception occur.");
}
}
//C++ code
.........
if (JNI_CreateJavaVM(&m_jvm, (void**)&m_env, &m_vm_args) < 0)
{
return CREATE_JVM_FAILED;
}
m_cls = m_env->FindClass("htmlparser_test/Demo");
if (m_cls == 0)
{
return CLASS_NOT_FOUND;
}
............
[1852 byte] By [
AllenZhua] at [2007-10-3 4:55:59]

//The following is C++ code:
...............
m_vm_args.version=JNI_VERSION_1_2;
m_options[0].optionString = "-Djava.compiler=NONE"; /* disable JIT */
m_options[1].optionString = "-Djava.class.path=D:\\search_engine\\twfile_reader"; /* user classes */
m_options[2].optionString = "-Djava.library.path=C:\\jdk1.5.0_06\\lib;C:\\jdk1.5.0_06\\htmlparser1_6\\lib"; /* set native library path */
m_options[3].optionString = "-verbose:jni"; /* print JNI-related messages */
m_vm_args.version = JNI_VERSION_1_2;
m_vm_args.nOptions = 4;
m_vm_args.options = m_options;
m_vm_args.ignoreUnrecognized = JNI_TRUE;
if (JNI_CreateJavaVM(&m_jvm, (void**)&m_env, &m_vm_args) < 0)
{
return CREATE_JVM_FAILED;
}
m_cls = m_env->FindClass("htmlparser_test/Demo");
jthrowable e = m_env->ExceptionOccurred();
if( e != NULL )
{
m_env->ExceptionDescribe();
perror("Exception");
}
m_env->ExceptionClear();
if (m_cls == 0)
{
return CLASS_NOT_FOUND;
}
m_mid = m_env->GetMethodID(m_cls,"<init>","()V");
if (m_mid == 0)
{
return METHOD_NOT_FOUND;
}
m_obj = m_env->NewObject(m_cls,m_mid, 0);
if (m_obj == 0)
{
return CREATE_OBJ_FAILED;
}
Now, through the exception catch, I got the following hint:
Exception in thread "main" [Dynamic-linking native method java.io.FileOutputStre
am.writeBytes ... JNI]
java.lang.NoClassDefFoundError: org/htmlparser/util/ParserException
[Dynamic-linking native method java.lang.Throwable.getStackTraceDepth ... JNI]
The following line doesn't seem right:m_options[1].optionString = "-Djava.class.path=D:\\search_engine\\twfile_reader"; /* user classes */
It seems you have omitted the jar files for the HTML Parser (htmlparser.jar, htmllexer.jar, ...).
Next time please paste your code between [code] [/code] tags with the help of the code button just above the message box.
It makes the code readable.
Regards