MathGroup Archive 2011

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: How-to create a mathematica notebook in a Java program ?

On 20/11/2011 10:35, Nilo de Roock wrote:
> The Java program below is self-contained and works. It starts a Mathematica kernel, asks the kernel for Sum[k^2,{k,1,11}] and retrieves and prints the answer. I want to do it differently. I want the kernel to create a Notebook, put Sum[k^2,{k,1,11}] in it, evaluate it and show it on screen. - Is that possible from within a Java program? - If yes, then how do I do it??
> package graphica;
>   import com.wolfram.jlink.*;
>   /**
>    *
>    * @author Nilo
>    */
> public class MathematicaTester {
> public static void main(String[] args) {
>      KernelLink ml = null;
>      String jLinkDir = "C:\\Program Files\\Wolfram Research\\Mathematica\\8.0\\SystemFiles\\Links\\JLink";
>      System.setProperty("com.wolfram.jlink.libdir", jLinkDir);
>      try {
>          ml = MathLinkFactory.createKernelLink("-linkmode launch -linkname'C:\\Program Files\\Wolfram Research\\Mathematica\\8.0\\MathKernel.exe'");
>          ml.discardAnswer();
>          String expr = "Sum[k^2,{k,1,11}]";
>          ml.evaluate(expr);
>          ml.waitForAnswer();
>          String x = ml.getString();
>          System.out.println("Result = " + x);
>      } catch (MathLinkException e) {
>          System.out.println("Fatal error opening link: " +
>          e.getMessage());
>          return;
>      }
> }
> }

Since notebooks are in fact text files, you could write such a file from 
Java, and then just start Mathematica on that notebook. This would have 
two shortcomings:

1)     The user would still need to actually execute the command with 

2)     The 'BoxForm' for the above command is complicated, so assuming 
you actually want to send a variety of commands, this would create a 

The solution to these problems is a little devious. You need to put some 
code in a .m file, and start a notebook that contains a Dynamic 
construction of the form:

The code you write into somefile.m has to do the calculation and place 
the result in a popup notebook. Since the code is in a .m file, it 
doesn't have a box structure - you just use InputForm.

Note that the popup notebook could also contain the HoldForm of your 
original expression.

A notebook containing the above Dynamic construct is obviously hard to 
make, because you can't open it without it 'activating'. This is why it 
tests for a value in a particular variable - so you can set this 
variable in another notebook before you open this notebook!

Clearly the above is not completely trivial to set up (essentially 
because the FE doesn't normally start executing without user input) - so 
I guess I would ask why you can't write the whole thing in Mathematica, 
possibly using J/Link to access anything you need from Java.

David Bailey

  • Prev by Date: Re: What is the point of having Initializations in DynamicModule and Manipulate?
  • Next by Date: Re: Timing graphics in the real world
  • Previous by thread: How-to create a mathematica notebook in a Java program ?
  • Next by thread: minimization of a matrix