Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I'm trying to write a Java program that uses Mathematica code, but when I compile it, a window appears asking for MathLink. I enter

c:\\program files\\wolfram research\\mathematica\\9.0\\mathkernel.exe

but this causes an exception to occur. I don't understand what the problem is. Here is the code I am using:

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

My objective is to use Mathematica in java to calculate the maximum of a function. I don't know how to do that, and the documentation on the net is few.

share|improve this question
add comment

1 Answer 1

That code is the SampleProgram.java example from J/Link. At the top of that very file, there is a long comment that explains exactly how to launch it. Also in the ReadMe.html file that sits right next to it. To wit:

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

That dialog box you are seeing is not asking for the path to the Mathematica kernel, but rather a name of a "listening" MathLink, which is for a type of connection when you are not directly launching Mathematica from your Java program.

As for using Mathematica to maximize a function, first make sure you know how to do it in a standard Mathematica session. Once you know exactly what you need to enter in Mathematica to perform your computation, send the exact same expression from Java. SampleProgram.java shows several different ways to send expressions to Mathematica and read the results.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.