participate


Java Native Interface (JNI) - Java crash when calling native code
<<   Back to Forum  |   Give us Feedback
This topic has 5 replies on 1 page.
fenril06
Posts:21
Registered: 7/2/04
Java crash when calling native code   
Aug 3, 2004 8:06 AM

 
And again I ve got a big problem with this native code...

Well I 'm using a method which takes a string as argument (actually the path of a file here : C:\\Number.tiff)
I call a wrapper dll which calls a method of another dll.
this method is defined like this: IMG LoadImg(const char filename)

So it takes a const char and not a string...the problem is that when I launch my application without converting the string I have a message written : cannot open tiff.That means I succeeded accessing the method of the dll but failed openig the file.
So I decided to convert the string into a char and then java crashes...
here is the piece of code :

JNIEXPORT void JNICALL Java_OCR_OCRlaunch
(JNIEnv *env, jobject cls, jstring str)
{
//const char *str1 = (*env)->GetStringUTFChars(env, str, 0); // this is the conversion part that makes it crash I guess
HMODULE hLib;
long int IMG;
hLib = LoadLibrary("OCRDLL.dll");
if(hLib)
{
FARPROC func = GetProcAddress(hLib,"LoadImg");

if ( func != NULL )
IMG = func(str);

FreeLibrary(hLib);
}
}

If anyone had an idea I'm really getting lost into this native stuff...thanks
 
bschauwejava
Posts:1,157
Registered: 1/13/04
Re: Java crash when calling native code   
Aug 3, 2004 8:23 AM (reply 1 of 5)  (In reply to original post )

 
1. Remove the const. It cannot be const, because you are assigning it a value.

2. Since you are doing thigs with DLLs, I assume you have VC++. You can debug your wrapper using the VC++ IDE. Just make the "launching program" java.exe, and supply parameters that start your java program.
 
ScottLJ
Posts:303
Registered: 11/6/03
Re: Java crash when calling native code   
Aug 3, 2004 11:25 AM (reply 2 of 5)  (In reply to original post )

 
Actually the code looks fine, since "const char * " means "non-const pointer to const char" rather than "const pointer to non-const char" -- gotta love C!

So, we need more details. Can you debug down to the exact line that fails? Is that an exact copy of your code? It looks like it should not compile, without some casts that are not shown. In fact I'd suggest switching to C++ compilation to make the type checking stricter.

[b][i]-slj-
 
fenril06
Posts:21
Registered: 7/2/04
Re: Java crash when calling native code   
Aug 4, 2004 2:31 AM (reply 3 of 5)  (In reply to #2 )

 
Well thanks for your answers. I compiled it as C++ which avoided me lots of errors.
I changed lots of stuff one hour ago and now it works...Don't really know where the problem was...anyway it works that's the most important and I finally understood a little more about C++.
Thanks men!
 
Boobo
Posts:1
Registered: 3/3/05
Re: Java crash when calling native code   
Mar 3, 2005 12:52 AM (reply 4 of 5)  (In reply to #3 )

 
Hi!

Did you succeed into using the ocrdll.dll lirbary in Java? Could you give me the steps or files to achieve this? I am interested into embedding ocr in a Java app...

Thank you very much!

Cheers
 
_Jack_
Posts:25
Registered: 11/2/04
Re: Java crash when calling native code   
Jun 16, 2005 2:33 AM (reply 5 of 5)  (In reply to original post )

 
alternatively, you can use ready-made OCR library like this one http://www.asprise.com/product/ocr
 
This topic has 5 replies on 1 page.
Back to Forum
 
Read the Developer Forums Code of Conduct

Click to email this message Email this Topic

Edit this Topic
  
 
 
Forums Statistics
    Users Online : 27
  • Guests : 138

About Sun forums
  • Sun Forums is a large collection of user generated discussions. It is here to help you ask questions, find answers, and participate in discussions.

    Check out our guide on Getting started with Sun Forums for a full walkthrough of how to best leverage the benefits of this community.

Powered by Jive Forums