Mathematica and Java
Calling Java from Mathematica
<< JLink`
InstallJava[];
myWindow = JavaNew["[Link]"];
myWindow@setSize[200, 200];
myWindow@setLocation[300, 300];
JavaShow[myWindow];
Constructors["[Link]"]
Fields["[Link]"]
Methods["[Link]", Inherited False]
Methods["[Link]", Inherited False]
Methods["[Link]", "set*"]
Java Mathematica Reference
Java Mathematica
Constructors MyClass obj obj =
= new MyClass( args ); JavaNew["MyClass", args]
Methods [Link]( args ); obj@methodName[args];
Fields [Link] = 1; obj@fieldName =1
var1 = [Link]; var1 = obj@fieldName
Static Methods [Link](args); MyClass`statMethod[args];
Static Fields [Link] = 1 MyClass`statField = 1
var1 = [Link] var1 = MyClass`statField
Import Class import [Link] LoadJavaClass["[Link]"]
Release Object ReleaseJavaObj[obj]
Block { ... } JavaBlock[ ... ]
Getting Information About Classes
Constructors["[Link]"]
Lists all constructors of the Button class.
Fields["[Link]"]
Lists all fields of the Button class.
Methods["[Link]", Inherited False]
Lists all methods of the Button class excluding those inherited from super classes.
Methods["[Link]", "set*"]
Lists all methods of the Button class that begin with set.
Calling Mathematica from Java
Setup the KernelLink
String[] argv1 = {
"-linkmode", "launch", "-linkname",
"c:\\program files\\wolfram
research\\mathematica\\5.2\\[Link]" };
KernelLink ml = [Link](argv1);
Evaluate Expression
[Link]("2+2");
[Link]();
int result = [Link]();
[Link]("2 + 2 = " + result);
Get Image Data
[Link]();
Image img = [Link](
"Plot[Sin[x], {x, 0, 2Pi}]",
450, 450);
Sample Code
KernelLink ml = null;
String[] argv1 = { "-linkmode", "launch", "-linkname",
"c:\\program files\\wolfram research\\mathematica\\5.2\\[Link]" };
// Create a KernelLink
try {
ml = [Link](argv1);
}
catch (MathLinkException e) {
[Link]("Fatal error opening link: " + [Link]());
return;
}
try {
// Get rid of the initial InputNamePacket the kernel
// will send when it is launched.
[Link]();
[Link]("<<MyPackage.m");
[Link]();
[Link]("2+2");
[Link]();
int result = [Link]();
[Link]("2 + 2 = " + result);
// Here's how to send the same input, but not as a string:
[Link]("EvaluatePacket", 1);
[Link]("Plus", 2);
[Link](3);
[Link](3);
[Link]();
[Link]();
result = [Link]();
[Link]("3 + 3 = " + result);
// If you want the result back as a string, use evaluateToInputForm
// or evaluateToOutputForm. The second arg for either is the
// requested page width for formatting the string. Pass 0 for
// PageWidth->Infinity. These methods get the result in one
// step--no need to call waitForAnswer.
String strResult = [Link]("4+4", 0);
[Link]("4 + 4 = " + strResult);
strResult = [Link](" Integrate[Cos[x], x]", 0);
[Link]("Integrate[Cos[x], x] = " + strResult);
}
catch (MathLinkException e)
{
[Link]("MathLinkException occurred: " + [Link]());
}
finally
{
// Close the KernelLink
[Link]();
}
Output from Sample Code
2 + 2 = 4
3 + 3 = 6
4 + 4 = 8
4 + 4 = Sin[x]
Further Reading
J/Link JavaDoc Documentation
Web:
[Link]
Local Drive:
C:\Program Files\Wolfram
Research\Mathematica\5.2\AddOns\JLink\Documentation\JavaDoc\[Link]
Java 1.5 API Reference
[Link]