MathGroup Archive 2006

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

Search the Archive

Re: Java -> Mathematica -> Java

  • To: mathgroup at smc.vnet.net
  • Subject: [mg63830] Re: [mg63729] Java -> Mathematica -> Java
  • From: Todd Gayley <tgayley at wolfram.com>
  • Date: Wed, 18 Jan 2006 02:38:59 -0500 (EST)
  • Sender: owner-wri-mathgroup at wolfram.com

At 02:22 AM 1/12/2006, Zhao, Chunhua wrote:
>Hi,
>
>I wonder if it is possible to call Mathematica in Java program, and then
>the Mathematica code calls Java. For example,
>
>ml = MathLinkFactory.createKernelLink("-linkmode launch -linkname
>'c:\\program files\\wolfram
>research\\mathematica\\5.1\\mathkernel.exe'");
>String extra = "Needs[\"JLink`\"];" + "InstallJava[];" +
>"AppendTo[$ExtraClassPath, " + '\u0022' + "F:\\Jar\" + '\u0022' + "];" +
>
>                 "class=LoadJavaClass[\"Functions\"];" +
>"m=JavaNew[class];" + "m[setResult[106]];";
>ml.evaluate(extra);
>
>But it doesn't work for me. Any suggestions?


Chunhua,

Do you want to have Mathematica call back into the same Java runtime that 
launched it, or do you want it to launch a separate Java runtime to be the 
target for J/Link calls like LoadJavaClass[] and JavaNew[]? In other words, 
do you specifically want to script objects in the original Java runtime via 
Mathematica code, or do you not care about that feature and are happy 
having a second Java runtime be launched when you call InstallJava[]? If 
you want the launching Java runtime to be used for callbacks, call the 
KernelLink.enableObjectReferences() method. If not, execute Needs["JLink`"] 
and InstallJava[] like you were doing in the example code you sent.

I'll assume that you want to have J/Link Mathematica functions call back 
into the original Java runtime. The following code should work for you. 
Note that I made a number of small changes and fixes, some of them 
important (like adding the discardAnswer() call after launching the 
kernel), and some of them cosmetic (like replacing the use of the 
deprecated $ExtraClassPath with the AddToClassPath function and eliminating 
the unnecessary call to LoadJavaClass before JavaNew). What you were trying 
to do was perfectly standard--if it didn't work it was probably because of 
some small detail that wasn't right.


ml = MathLinkFactory.createKernelLink("-linkmode launch -linkname
'c:\\program files\\wolfram research\\mathematica\\5.1\\mathkernel.exe'");
ml.discardAnswer();
ml.enableObjectReferences();
String extra = "AddToClassPath[\"F:\\\\Jar\"];" +
                "m = JavaNew[\"Functions\"];" +
                "m[setResult[106]];";
ml.evaluate(extra);
// I assume you are not interested in the result, since you ended the M 
code string with
// a semicolon, and thus the result will just be Null anyway.
ml.discardAnswer();



Todd Gayley
Wolfram Research


  • Prev by Date: Re: variable of integration not localized in Integrate and Sum?
  • Next by Date: Re: variable of integration not localized in Integrate and Sum?
  • Previous by thread: Java -> Mathematica -> Java
  • Next by thread: Re: Re: Java -> Mathematica -> Java