From: Andrey on
Hello,

I wrote a java program (which runs some calculation on math kernel)
with JLink interface but it crashes regularly and randomly with the
exception:
"Exception message: MathLink connection was lost.
Exception class: com.wolfram.jlink.MathLinkException
Exception cause: null".

My system: winXP(prof), Mathematica 7.0.1.

I decided that the problem is with JLinkNativeLibrary.dll (which loads
during JLink load) and I recompile it for my machine (the only
warnings were the followings:
"JLinkNativeLibrary.c: In function
`Java_com_wolfram_jlink_NativeLink_getNativeWindowHandle':
JLinkNativeLibrary.c:1910: warning: cast from pointer to integer of
different size
JLinkNativeLibrary.c: In function
`Java_com_wolfram_jlink_NativeLink_appToFront':
JLinkNativeLibrary.c:1960: warning: cast to pointer from integer of
different size
....\MINGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\mingw32\bin\ld.exe:
warning: cannot find entry symbol _DllMainCRTStartup(a)12; defaulting to
6bf81000").
But problem remains.
Then I tried to recompile the JLink.jar (and after that recompile
JLinkNativeLibrary.dll again) with no success.

Did anybody meet such the problem?

Or could anybody suggest anything or help?


Regards,

-Andrey



From: David Bailey on
Andrey wrote:
> Hello,
>
> I wrote a java program (which runs some calculation on math kernel)
> with JLink interface but it crashes regularly and randomly with the
> exception:
> "Exception message: MathLink connection was lost.
> Exception class: com.wolfram.jlink.MathLinkException
> Exception cause: null".
>
> My system: winXP(prof), Mathematica 7.0.1.
>
> I decided that the problem is with JLinkNativeLibrary.dll (which loads
> during JLink load) and I recompile it for my machine (the only
> warnings were the followings:
> "JLinkNativeLibrary.c: In function
> `Java_com_wolfram_jlink_NativeLink_getNativeWindowHandle':
> JLinkNativeLibrary.c:1910: warning: cast from pointer to integer of
> different size
> JLinkNativeLibrary.c: In function
> `Java_com_wolfram_jlink_NativeLink_appToFront':
> JLinkNativeLibrary.c:1960: warning: cast to pointer from integer of
> different size
> ...\MINGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\mingw32\bin\ld.exe:
> warning: cannot find entry symbol _DllMainCRTStartup(a)12; defaulting to
> 6bf81000").
> But problem remains.
> Then I tried to recompile the JLink.jar (and after that recompile
> JLinkNativeLibrary.dll again) with no success.
>
> Did anybody meet such the problem?
>
> Or could anybody suggest anything or help?
>
>
> Regards,
>
> -Andrey
>
>
>
I use J/Link a lot, and I have not encountered this problem. The most
likely explanation, I think, is that your Java program breaks in some
catastrophic way or loops indefinitely. If it is feasible, I would try
compiling your Java code with a main method and try to test it as a
free-standing Java program. I would also re-install the DLL files from
the Mathematica installation disk, just in case your recompilation
introduced any further issues.

David Bailey
http://www.dbaileyconsultancy.co.uk

From: Andrey on
Dear David Bailey,

On Feb 9, 10:48 am, David Bailey <d...(a)removedbailey.co.uk> wrote:
> ...
> I use J/Link a lot, and I have not encountered this problem. The most
> likely explanation, I think, is that your Java program breaks in some
> catastrophic way or loops indefinitely.

Yes, it can be so, but the same java code works ok on some others
systems (I knew two of them, one was winXP and other was Linux).

It looks like there is a problem with transition of floating-point
numbers from Mathematica to java...

I will try to transfer only strings...

> If it is feasible, I would try
> compiling your Java code with a main method and try to test it as a
> free-standing Java program. I would also re-install the DLL files from
> the Mathematica installation disk, just in case your recompilation
> introduced any further issues.

I've been working in such a way.

>
> David Bailey http://www.dbaileyconsultancy.co.uk

Thank You for the answer.

Regards,

-Andrey

From: David Bailey on
Andrey wrote:
> Dear David Bailey,
>
> On Feb 9, 10:48 am, David Bailey <d...(a)removedbailey.co.uk> wrote:
>> ...
>> I use J/Link a lot, and I have not encountered this problem. The most
>> likely explanation, I think, is that your Java program breaks in some
>> catastrophic way or loops indefinitely.
>
> Yes, it can be so, but the same java code works ok on some others
> systems (I knew two of them, one was winXP and other was Linux).
>
> It looks like there is a problem with transition of floating-point
> numbers from Mathematica to java...
>
> I will try to transfer only strings...
>
>> If it is feasible, I would try
>> compiling your Java code with a main method and try to test it as a
>> free-standing Java program. I would also re-install the DLL files from
>> the Mathematica installation disk, just in case your recompilation
>> introduced any further issues.
>
> I've been working in such a way.
>
>> David Bailey http://www.dbaileyconsultancy.co.uk
>
> Thank You for the answer.
>
> Regards,
>
> -Andrey
>
A good way to explore this problem might be to call ShowJavaConsole[] to
produce a console, and then add calls to

System.out.println(whatever)

in your Java code.

Mathematica Real's correspond to Java's double's - other than that,
there should be no problem transferring floating point quantities, but
if you generate NaN's, there could be problems, I don't know.

David Bailey
http://www.dbaileyconsultancy.co.uk


David Bailey
http://www.dbaileyconsultancy.co.uk

From: Andrey on
The problem appears after

ml.waitForAnswer();

(where ml is KernelLink).

The stack trace shows that exception arise in class
com.wolfram.jlink.NativeLink in the following method:
....
public synchronized int nextPacket() throws MathLinkException {

if (link == 0) {
throw new MathLinkException(MLE_LINK_IS_NULL, LINK_NULL_MESSAGE);
}
int pkt = MLNextPacket(link); // <-- Probably Here
int errCode = MLError(link);
if(errCode == 0) {
return pkt;
} else {
throw new MathLinkException(errCode, MLErrorMessage(link));
}
}
....
So, it looks like the problem is in native library.

Of course, probably, it's possible to avoid the problem by rewriting
the mathematica code...

-Andrey

On Feb 11, 1:16 pm, David Bailey <d...(a)removedbailey.co.uk> wrote:

> A good way to explore this problem might be to call ShowJavaConsole[] to
> produce a console, and then add calls to
>
> System.out.println(whatever)
>
> in your Java code.

I do not understand now how to do it and how it can help.

But thanks for the suggestion.

-Andrey