Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to make my Mathematica function to serve as an event handler for the events generated by a Java object.

Specifically, I have a Java object that connects as a client to a server that periodically pushes updates to all clients. I also have an observer object that calls it's update method whenever the client receives any updates.

My goal is to catch this updates inside Mathematica, and handle them with a Mathematica function

It appears that JLink has a way of doing this with ImplementJavaInterface command Here is how I've used it:

(* create a client that connects to localhost:3000 *)
client = JavaNew["me.kutrumbos.DdpClient", "localhost", MakeJavaObject[3000]];

(* create an observer that monitors client's events *)
observer = JavaNew["me.kutrumbos.examples.ObservableDdpClientObserver"];

(* create a variable that will be updated with data from incoming events *)
collections = {};

(* define event handler that updates collections variable with the new value *)
onCollectionUpdate[msg_] := collections = observer@getJSONObjectsList["coll"];

(* add event handler to the observer *)
    "update" -> "onCollectionUpdate"]];

(* add observer to the client *)

(* connect to the server *)

When I run this inside a notebook, everything works. Every time an event occurs, collections variable gets updated with the data contained in the event.

However, when I put this code in the package, and expose collections by adding collections::usage = "global var" in the preamble of the package, the variable doesn't update, and the event handler doesn't even seem to run.

I tried inserting MessageDialog["Event Handler alert"] inside the event handler to see if the event handler is even called, and I am not getting any pop-up alerts when I generate new events on the server.

Any idea why this might be happening?

share|improve this question
Without trying, my guess would be that once in a package, your symbol onCollectionUpdate becomes something like YourPackage`Private`onCollectionUpdate, and this may be the source of trouble. So, you need to use now "update" -> "`YourPackage`Private`onCollectionUpdate", or whatever the long name for this symbol becomes inside a package. – Leonid Shifrin Jun 20 '13 at 18:47
Besides, you of course will need to load JLink, which I assumed you did in your package either using second argument of BeginPackage, or using Needs["JLink`"] in the implementation section. – Leonid Shifrin Jun 20 '13 at 18:57

As Leonid suggested, I added the full path to the event handler, and it gets called now from inside the package:

    "update" -> "Utils`Private`onCollectionUpdate"]];

Thanks, Leonid!

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.