I'm trying to connect to a remote Mathematica 8 kernel (on a Windows machine) using J/Link. On the client machine, I put the JLinkNativeLibrary.dll
file in C:\Documents and Settings\User\My Documents
. But, it gives me the error message "cannot find the required native library named JLinkNativeLibrary
".
Here is my code
public static void main(String[] args) {
KernelLink link;
try {
String jLinkDir = "C:\\Documents and Settings\\User\\My Documents";
System.setProperty("com.wolfram.jlink.libdir", jLinkDir);
link = MathLinkFactory.createKernelLink("-linkmode connect -linkprotocol tcpip -linkname [email protected]");
} catch (MathLinkException ex) {
ex.printStackTrace();
}
}
Exception :
Fatal error: cannot find the required native library named JLinkNativeLibrary. Exception in thread "main" java.lang.UnsatisfiedLinkError: com.wolfram.jlink.NativeLink.MLOpenString(Ljava/lang/String;[Ljava/lang/String;)J at com.wolfram.jlink.NativeLink.MLOpenString(Native Method) at com.wolfram.jlink.NativeLink.(NativeLink.java:117) at com.wolfram.jlink.MathLinkFactory.createMathLink0(MathLinkFactory.java:281) at com.wolfram.jlink.MathLinkFactory.createMathLink(MathLinkFactory.java:194) at com.wolfram.jlink.MathLinkFactory.createKernelLink0(MathLinkFactory.java:171) at com.wolfram.jlink.MathLinkFactory.createKernelLink(MathLinkFactory.java:65) at mathematica.Sample.main(Sample.java:21)
I also wrote the following code where KernelLink
gets created but there is some problem with establishing the connection. It gives an error "MathLinkException: 1: MathLink connection was lost."
public static void main(String[] args) {
KernelLink ml = null;
try {
ml = MathLinkFactory.createKernelLink("-linkmode connect -linkprotocol tcp -linkname [email protected]");
} catch (MathLinkException ex) {
Logger.getLogger(MathematicaSample.class.getName()).log(Level.SEVERE, null, ex);
return;
}
try {
ml.connect(1000000); // Wait at most 10 seconds
} catch (MathLinkException e) {
// If the timeout expires, a MathLinkException will be thrown.
e.printStackTrace();
ml.close();
return; // Or whatever is appropriate.
}
try {
// Get rid of the initial InputNamePacket the kernel will send
// when it is launched.
ml.discardAnswer();
ml.evaluate("<<MyPackage.m");
ml.discardAnswer();
ml.evaluate("2+2");
ml.waitForAnswer();
int result = ml.getInteger();
System.out.println("2 + 2 = " + result);
} catch (MathLinkException e) {
System.out.println("MathLinkException occurred: " + e.getMessage());
} finally {
ml.close();
}
}
}
Does anyone know how to connect to the kernel?