How to create a Mathematica Notebook in Java?

前端 未结 3 1165
滥情空心
滥情空心 2021-01-14 03:30

I am looking for the prototypical \'Hello World\' program that creates a Mathematica Notebook file.

I have this working program.

 package graphica;

         


        
3条回答
  •  被撕碎了的回忆
    2021-01-14 04:24

    It took some research but I managed to answer the question myself.

     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'");
            //test-1
                 ml.discardAnswer();
                 String expr = "Sum[k,{k,1,11}]";
                 ml.evaluate(expr);
                 ml.waitForAnswer();
                 String x = ml.getString();
                 System.out.println("Result = " + x);
           //test-2
                 expr = "UsingFrontEnd[nb=NotebookPut[Notebook[{Cell[\"Graphics3D[Cuboid[]]\", \"Input\"]}]]]";
                 System.out.println("Result = " + ml.evaluateToOutputForm(expr, 40) );
                 expr = "UsingFrontEnd[NotebookSave[nb,\"TERRANOVA1\"]]";
                 System.out.println("Result = " + ml.evaluateToOutputForm(expr, 40) );
    
             } catch (MathLinkException e) { 
                 System.out.println("Fatal error opening link: " + 
                 e.getMessage()); 
                 return; 
             }
         }
     }
    

提交回复
热议问题