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

*To*: mathgroup at smc.vnet.net*Subject*: [mg123044] Re: How-to create a mathematica notebook in a Java program ?*From*: David Bailey <dave at removedbailey.co.uk>*Date*: Mon, 21 Nov 2011 04:29:16 -0500 (EST)*Delivered-to*: l-mathgroup@mail-archive0.wolfram.com*References*: <jaal5c$133$1@smc.vnet.net>

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 Shift-Enter. 2) The 'BoxForm' for the above command is complicated, so assuming you actually want to send a variety of commands, this would create a problem. 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: Dynamic[If[! ValueQ[editingFrontEndLauncher],NotebookClose[SelectedNotebook[]]; Get["somefile.m"]], SynchronousUpdating->False] 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 http://www.dbaileyconsultancy.co.uk