MathGroup Archive 2001

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

Search the Archive

Re: JLink an animated gif

  • To: mathgroup at
  • Subject: [mg30724] Re: JLink an animated gif
  • From: tgayley at (Todd Gayley)
  • Date: Sat, 8 Sep 2001 02:56:06 -0400 (EDT)
  • Organization: Wolfram Research, Inc.
  • References: <9nallf$npe$>
  • Sender: owner-wri-mathgroup at

On Fri, 7 Sep 2001 14:30:07 +0000 (UTC), Ulrich Hofstoetter
<ulrich.hofstoetter at> wrote:

>I am trying to get an animation back into java as animated gif.
>I could use Export and store the animation into a temporary file, but
>as the animation should go into a database, i would prefer not to use
>temporary files.
>When i use ExportString[symbol, "GIF"] in a notebook, the output seems
>to be an animated gif, but if i use
>ml.evaluate("ExportString[Symbol, \"GIF\"]"); // ml is an Instance of
>String animation = ml.getString();
>i get a MathLinkException and the output looks like Postscript, here is
>the last output from a PacketListener (only the last lines from the 
>postscript and the following packets), as well as the Stacktrace.
>.76964 .61568 L
>.77508 .6142 L
>.78014 .61251 L
>.79161 .60753 L
>.80269 .60125 L
>.81314 .59404 L
>.83267 .57728 L
>.87311 .5301 L
>.91613 .46443 L
>.95756 .39045 L
>.9974 .31407 L
>1 .30902 L
>% End of Graphics
>Packet type was DisplayEndPacket. Contents follows.
>Packet type was ReturnPacket. Contents follows.
>MathLinkException: 3: MLGet out of sequence
>        at com.wolfram.jlink.NativeLink.getString(Unknown Source)
>        at com.wolfram.jlink.WrappedKernelLink.getString(Unknown Source)
>        at Ani.<init>(
>        at Ani.main(
>If anybody could please direct me to a solution,


The problem is simply that you have forgotten to call ml.waitForAnswer() before calling

An "MLGet out of sequence" MathLinkException virtually always means that you have called
an inappropriate "get" function to read off the link. Your getString() is being called
when what is waiting on the link is the function ReturnPacket (more likely it is
DisplayPacket, as you are getting the PostScript Mathematica generates for all the plots;
more on this below).

Here is a correct example:

   ml.evaluate("ExportString[Table[Plot[x^n,{x,0,2}], {n, 6}], \"GIF\"]");
   String gifData = ml.getString();

I'm glad to see that people are using the PacketPrinter class for monitoring the flow of
packets from Mathematica, which is an extremely useful debugging aid. All that PostScript
you are seeing is useless, and you can prevent it from being generated by setting
$DisplayFunction appropriately. As you know, when you execute something like
Table[Plot[x^n,{x,0,2}], {n, 6}] in a notebook, you get a list of Graphics expressions as
the result, but you also get a bunch of PostScript generated for each plot, which the
front end renders for you. If you don't want to see the plots, you can play some standard
tricks with DisplayFunction or $DisplayFunction. When you execute this same command from
Java, you almost certainly don't have any interest in getting PostScript prior to the
result, so you will usually want to prevent the kernel from trying to display the plots:

   ml.evaluate("Block[{$DisplayFunction = Identity}, ExportString[Table[Plot[x^n,{x,0,2}],
{n, 6}], \"GIF\"]]");
   String gifData = ml.getString();

Now you won't see a bunch of DisplayPackets with PostScript in your PacketPrinter output.

--Todd Gayley
Wolfram Research

  • Prev by Date: Re: Extract coefs. and exponents from a list . Package problem
  • Next by Date: Re: FindMinimum and least square minimization
  • Previous by thread: JLink an animated gif
  • Next by thread: SqrtMod function