I have the Mathematica notebook (Test.nb in this case), how can I run in from Java? So far I have the following code. If I type:


directly to kernel window, then it evaluates Test.nb, but if I do it using evaluate method (as shown below) - nothing happens, no output, no errors.

import com.wolfram.jlink.*;

public class MathCore {

    public MathCore() {
        // TODO Auto-generated constructor stub

    public static void evalMath(String dir,String fname) {

        KernelLink ml = null; 
        String jLinkDir = "C:\\Program Files\\Wolfram Research\\Mathematica\\10.0\\SystemFiles\\Links\\JLink";
        System.setProperty("com.wolfram.jlink.libdir", jLinkDir);

        try { 
            ml = MathLinkFactory.createKernelLink("-linkmode launch -linkname 'C:\\Program Files\\Wolfram Research\\Mathematica\\10.0\\MathKernel.exe'");

            String expr = "UsingFrontEnd[NotebookEvaluate[ToFileName[\""+dir+"\", \""+fname+"\"]]]";
            String x = ml.getString();
            System.out.println("Result = " + x);

        } catch (MathLinkException e) { 
            System.out.println("Fatal error opening link: " + 

  • $\begingroup$ Welcome to Mathematica.SE! I hope you will become a regular contributor. To get started, 1) take the introductory tour now, 2) when you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge, 3) remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign, and 4) give help too, by answering questions in your areas of expertise. $\endgroup$
    – bbgodfrey
    Oct 8 '15 at 13:06
  • $\begingroup$ Mathematica notebooks are not programs/scripts and are not meant to be run as a unit. Why don't you put the code in a plain text file with the .m extension instead, and run it using Get? It will not require a front end. Notebooks can be converted to .m files. $\endgroup$
    – Szabolcs
    Oct 8 '15 at 13:36
  • $\begingroup$ Can you please explain it in more details or give some link? $\endgroup$ Oct 8 '15 at 14:13
  • $\begingroup$ Actually, @Szabolcs, I would claim that this code should work. But it is an unfortunate truth that the history of NotebookEvaluate running from a standalone kernel is a troubled one. I believe the relevant issue here is that there is a known and reported bug that, if a kernel has a parent link other than the FE, then UsingFrontEnd[NotebookEvaluate[...]] can fail. I partially fixed this for 10.3, but troubles remain. That having been said, there is a minor bug in the code here. See next comment. $\endgroup$
    – John Fultz
    Oct 8 '15 at 15:25
  • $\begingroup$ @KonstantinStolpovsky, in the short run, you're probably going to have to work with Szabolcs' solution, which is to use a package file. Because of bugs I mentioned in my previous comment. But that having been said, your code has a completely unnecessary reference to EvaluatePacket in it. You may wish to correct it in your question. $\endgroup$
    – John Fultz
    Oct 8 '15 at 15:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.