This is documentation for Mathematica 5, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.2)

Documentation / Mathematica / Add-ons & Links / J/Link / Part 2. Writing Java Programs That Use Mathematica /

2.4 Sample Program

Here is a basic Java program that launches the Mathematica kernel, uses it for some computations, and then shuts it down. This program is provided in source code and compiled form in the JLink/Examples/Part2 directory. The usual MathLink arguments including the path to the kernel are given on the command line you use to launch the program, and some typical examples are given below. You will have to adjust the Mathematica kernel path for your system. If you have your CLASSPATH environment variable set to include JLink.jar, then you can leave off the -classpath specification in these command lines. It is assumed that these commands are executed from the JLink/Examples/Part2 directory.

(Windows)

java -classpath .;..\..\JLink.jar SampleProgram -linkmode launch -linkname "c:\program files\wolfram research\mathematica\5.0\mathkernel.exe"



(Unix)

java -classpath .:../../JLink.jar SampleProgram -linkmode launch -linkname 'math -mathlink'



(Mac OS X from a terminal window)

java -classpath .:../../JLink.jar SampleProgram -linkmode launch -linkname '"/Applications/Mathematica 5.0.app/Contents/MacOS/MathKernel" -mathlink'

Here is the code from SampleProgram.java. This program demonstrates launching the kernel with MathLinkFactory.createKernelLink(), and several different ways to send computations to Mathematica and read the result.

import com.wolfram.jlink.*;



public class SampleProgram {



    public static void main(String[] argv) {



        KernelLink ml = null;



        try {

            ml = MathLinkFactory.createKernelLink(argv);

        } 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("<<MyPackage.m");

            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);



        } catch (MathLinkException e) {

            System.out.println("MathLinkException occurred: " + e.getMessage());

        } finally {

            ml.close();

        }

    }

}