I have a MathKernel started and controlled via JLink from a JVM. The $CommandLine for this kernel is:
$CommandLine
(* {"C:/Program Files/Wolfram Research/Mathematica/10.0/mathkernel.exe", "-mathlink", "-linkprotocol", "", "-linkmode", "connect", "-linkname", "h7r_shm"} *)
I can open a Frontend from this kernel by sending it the following string for evaluation:
Needs["JLink`"];
FrontEndLaunchCommand = mmaFrontEndPath;
ConnectToFrontEnd[]
and this string to actually open a window with a notebook
UseFrontEnd[
nbObj = CreateWindow[
DocumentNotebook[
{TextCell["Launched by JVM Kernel", "Title"]}],
Visible -> True ]
]
I'd like to be able to set this notebook to use the JVM's kernel for evaluation (rather than its own) so I can use the notebook for easier debugging of the JVM launched kernel.
I've tried creating a link on the JVM's kernel using
feMathLink = LinkCreate["feMathLink"]
and then attempting to connect to this in the Notebook using
jvmLink = LinkConnect["feMathLink"]
I get True from LinkReadyQ[jvmLink] but attempts to read from it using LinkRead[jvmLink] just hang.
I've also tried just setting the Notebook's evaluation kernel to "feMathLink" using
SetOptions[EvaluationNotebook[], Evaluator -> "feMathLink"]
but this also just hangs.
Apologies in advance if this really is in the manual but it is not obvious.
This all comes from a small test program here that seemed too large to post.
Update - Clarification
As suggested by halirutan, I've simplified the question by taking Java/Groovy out of the question and focusing just on the kernel to frontend connection issue.
If you run the following code from a kernel (call it kernel A) in "terminal mode" then it will launch a frontend which is connected to this kernel but has its own kernel (call it kernel B) for evaluation. That is, kernel A can send commands to the frontend via UseFrontEnd[] but this frontend doesn't use kernel A for evaluation.
Needs["JLink`"]
(* this is the program the JLink will use to launch a FrontEnd *)
$FrontEndLaunchCommand = FileNameJoin[{$InstallationDirectory, "Mathematica.exe"}]
(* this connects the kernel to a FrontEnd launched via JLink *)
ConnectToFrontEnd[]
(* at this point, FrontEnd is not visible, it is available only for processing some commands via UseFrontEnd[] *)
(* this uses UseFrontEnd[] to open a new Notebook in a Window making the FrontEnd visible *)
UseFrontEnd[
nbObj = CreateWindow[
DocumentNotebook[{TextCell["Launched by JVM Kernel", "Title"]}],
Visible -> True ]
]
(* you should now see an unnamed notebook with first cell as a Title "Launched by JVM Kernel" *)
I'd like to set the evaluation kernel (currently Kernel B) to be Kernel A, the initializing kernel. The objective is to use the frontend as a debugging tool for a kernel launch via JVM (or terminal in this case).
As noted above, I've tried creating a math link in Kernel A using:
feMathLink = LinkCreate["feMathLink"]
and setting the frontend's evaluation kernel to this link using:
jvmLink = LinkConnect["feMathLink"]
SetOptions[EvaluationNotebook[], Evaluator -> "feMathLink"]
I've also tried reading and writing from this link but it always hangs on me.
I've put a windows batch file and a simple .m file with all of this here. This should enable you to replicate the issue on any standard windows setup without much effort, none if you have version 10 installed in the standard place - just run the runIt.bat in a cmd or Powershell terminal.
Many thanks again to halirutan for suggestions to clarify the question. It really isn't a JVM question at all, although UseFrontEnd[] is a JLink function.
UseFrontEnd
in theJLink`
package was not meant to create a user interface, but for certain things (like image rasterization) that usually needs a front end. I tried it from the command-line and I wasn't even able to create a notebook windows. With yourCreateWindow
command, I get an icon in the task-bar without an actual window. I cannot raise this window. It's like having a symbol for something that is not there. That being said, would it be possible for your to edit your question and first try to do this from command line? I guess this will attract.. $\endgroup$