<< JLink`
InstallJava[];
myWindow = JavaNew["com.wolfram.jlink.MathJFrame"];
myWindow@setSize[200, 200];
myWindow@setLocation[300, 300];
JavaShow[myWindow];
Constructors["java.awt.Button"]
Fields["java.awt.Button"]
Methods["java.awt.Button", Inherited → False]
Methods["java.awt.Button", Inherited → False]
Methods["java.awt.Button", "set*"]
|
Java
|
Mathematica
|
Constructors
|
MyClass obj
= new MyClass( args );
|
obj =
JavaNew["MyClass", args]
|
|
|
|
Methods
|
obj.methodName( args );
|
obj@methodName[args];
|
|
|
|
Fields
|
obj.fieldName = 1;
|
obj@fieldName =1
|
|
var1 = obj.fieldName;
|
var1 = obj@fieldName
|
|
|
|
Static Methods
|
MyClass.statMethod(args);
|
MyClass`statMethod[args];
|
|
|
|
Static Fields
|
MyClass.statField = 1
|
MyClass`statField = 1
|
|
var1 = MyClass.statField
|
var1 = MyClass`statField
|
|
|
|
Import Class
|
import java.lang.Math
|
LoadJavaClass["java.lang.Math"]
|
|
|
|
Release Object
|
|
ReleaseJavaObj[obj]
|
|
|
|
Block
|
{ ... }
|
JavaBlock[ ... ]
|
|
|
|
Constructors["java.awt.Button"]
Lists all constructors of the Button class.
Fields["java.awt.Button"]
Lists all fields of the Button class.
Methods["java.awt.Button", Inherited → False]
Lists all methods of the Button class excluding those inherited from super classes.
Methods["java.awt.Button", "set*"]
Lists all methods of the Button class that begin with “set”.
Setup the KernelLink
String[] argv1 = {
"-linkmode", "launch", "-linkname",
"c:\\program files\\wolfram research\\mathematica\\5.2\\mathkernel.exe" };
KernelLink ml = MathLinkFactory.createKernelLink(argv1);
ml.evaluate("2+2");
ml.waitForAnswer();
int result = ml.getInteger();
System.out.println("2 + 2 = " + result);
ml.discardAnswer();
Image img = ml.evaluateToImage(
"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\\mathkernel.exe" };
// Create a KernelLink
try {
ml = MathLinkFactory.createKernelLink(argv1);
}
catch (MathLinkException e) {
System.out.println("Fatal error opening link: " + e.getMessage());
return;
}
try {
// Get rid of the initial InputNamePacket the kernel
// will send when it is launched.
ml.discardAnswer();
ml.evaluate("<ml.discardAnswer();
ml.evaluate("2+2");
ml.waitForAnswer();
int result = ml.getInteger();
System.out.println("2 + 2 = " + result);
// Here's how to send the same input, but not as a string:
ml.putFunction("EvaluatePacket", 1);
ml.putFunction("Plus", 2);
ml.put(3);
ml.put(3);
ml.endPacket();
ml.waitForAnswer();
result = ml.getInteger();
System.out.println("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 = ml.evaluateToOutputForm("4+4", 0);
System.out.println("4 + 4 = " + strResult);
strResult = ml.evaluateToOutputForm(" Integrate[Cos[x], x]", 0);
System.out.println("Integrate[Cos[x], x] = " + strResult);
}
catch (MathLinkException e)
{
System.out.println("MathLinkException occurred: " + e.getMessage());
}
finally
{
// Close the KernelLink
ml.close();
}
2 + 2 = 4
3 + 3 = 6
4 + 4 = 8
4 + 4 = Sin[x]
Further Reading
J/Link JavaDoc Documentation
Web:
http://www.wolfram.com/solutions/mathlink/jlink/documentation/api/
Local Drive:
C:\Program Files\Wolfram Research\Mathematica\5.2\AddOns\JLink\Documentation\JavaDoc\index.html
http://java.sun.com/j2se/1.5.0/docs/api/ |