MathGroup Archive 2000

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

Search the Archive

Re: J/Link Examples ??

  • To: mathgroup at
  • Subject: [mg22406] Re: J/Link Examples ??
  • From: Todd Gayley <tgayley at>
  • Date: Wed, 1 Mar 2000 00:39:55 -0500 (EST)
  • Sender: owner-wri-mathgroup at

On 18 Feb 2000 12:04:24 -0500, "Ersek, Ted R" <ErsekTR at> wrote:

 >The Wolfram Research web page mentions that J/Link is available.
 >I have to confess that I know very little about Java, but I am more
 >motivated now that it can work nicely with Mathematica, my favorite
 >software. I am a little puzzled about what we can do with J/Link. Will it
 >make it easy for us to make web pages like the Wolfram Research Integrator?
 >Can someone give examples of what we could do with J/Link? It's even better
 >if you can point me to something you made using J/Link.
 >Ted Ersek
 >Mathematica tips, tricks have moved to


J/Link is such a new technology that there aren't very many fully-worked 
out examples of its use, other than the examples included with J/Link itself.

For readers unfamiliar with J/Link, it is freely available at

I know that a lot of people think "the Web" when they hear Java, and J/Link 
is a good tool for writing applications for Mathematica on the Web. There 
are many people out there who are working on such projects right now, and I 
know of a few that have been completed. Perhaps a developer on one of these 
projects will come forward to show what they have done. WRI is working with 
J/Link internally in several different ways, including a reworking of the 
Integrator site, and we will soon have some useful working code accessible 
from the J/Link page. These would be servlet-based solutions for people who 
want to provide Mathematica functionality to Web clients. We are also 
working out the details of a new Web use and licensing policy.

Having said this about Web development, I want to make sure people 
understand that in addition to being a way for Java programs to call 
Mathematica, J/Link provides a very powerful and simple means for 
Mathematica programs to access the functionality of Java classes. It is 
literally the case that every single Java class can be used from 
Mathematica exactly like it is used from Java. The Mathematica environment 
is suddenly enlarged to encompass everything that is available in Java--and 
that is just about everything.

As most readers know, Mathematica has the ability to call routines written 
in other languages via MathLink. Calling such a routine, say a C function, 
requires writing a wrapper program that functions as the "glue" between the 
C function and Mathematica. With many simple functions, all of the MathLink 
code can be written for you by a preprocessing step on a special template 
file that you write. For more complex arguments and return values, like 
arrays, you usually have to get your hands dirty in the details of the 
MathLink API. Thus, for every function, you have to at least write one 
template file, and perhaps a bunch of C code. And there is no support for 
object-oriented programming; you can only pass primitive types (int, char, 
etc.) and arrays of them, not objects. Then you have to compile the code 
and you end up with an executable for just one platform.

J/Link makes all these steps go away completely. You can load Java classes 
directly into Mathematica, create objects, and call methods without any 
preparation whatsoever. If you only want to use existing Java classes, you 
don't even have to know how to program in Java, because you won't need to 
write any Java, just Mathematica. And since Java is portable, everything 
you write can be used unmodified on any platform. This especially frees 
commercial developers who want to write a MathLink-dependent application, 
but who balk at the prospect of having to compile their external code on 
every platform (and flavor of UNIX!) their users might require.

I emphasize this capability of J/Link because I don't want people to think 
that since they have no desire to write a Java program they have no use for 

In fact, J/Link allows users to think about Mathematica development in a 
new way. Previously, if you wanted a feature that was beyond the capability 
of Mathematica, you had to write a MathLink program, possibly a reasonably 
complicated one. You wouldn't bother trying unless you were pretty serious. 
Now, your first thought should be "is this capability available in Java?" 
If the answer is "yes", which it probably is, then you can already do what 
you need to do without ever leaving the Mathematica environment.

The J/Link User Guide has lots of examples of this sort of thing, including 
creating GUIs for Mathematica programs and reading files off the Internet 
by overloading the Get (<<) notation:

In[3]:= <<

I want to present another example that answers a question that crops up 
periodically in this newsgroup, which is "How do I read and write to a 
serial port form Mathematica?" The last time this question was asked, 
someone (sorry, I forget who) pointed out that on Windows you can write to 
a port like this (if I recall):

      strm = OpenWrite["COM1"]
      WriteString[strm, "command"]

This was an interesting tidbit that I had not known before.

For more serious control of a serial port (including any type of reading), 
you would need to write a MathLink program. Now that J/Link is here, your 
first thought should instead be "Does Java have a way to control a serial 
port?" The answer is, of course, yes. There is a standard extension package 
for Java called javax.comm, otherwise known as the Java Communications API.

To go about using J/Link to control a serial port all you need to do is 
look at the javax.comm documentation to see what classes and methods are 
available and how they are used. I gleaned the following from looking at a 
trivial 20-line example program among several that come with the javax.comm 
package. The syntax may look a bit unusual, but it is standard J/Link 
style. Java's dot (.) operator for invoking methods and accessing fields is 
mapped to Mathematica's @ operator. (Note that users with the 1.0.0 version 
of J/Link will need to get 1.0.1, which was posted on 2/14, to run this 

In[1]:= <<JLink`

(* This launches the Java runtime and prepares it for use by Mathematica: *)

In[2]:= InstallJava[];

(* We load one class to start us off: *)

In[3]:= LoadClass["comm.javax.CommPortIdentifier"];

In[4]:= com1 = ComPortIdentifier`getPortIdentifier["COM1"]
                  (* Perhaps "/dev/term/a" on UNIX. *)

(* com1 is now an instance of a Java object: *)

Out[4]= <<JavaObject[comm.javax.CommPortIdentifier]>>

In[5]:= serialPort = com1 at open["J/Link", 2000]

Out[5]= <<JavaObject[]>>

(* Some examples of simple things we can do: *)

In[6]:= serialPort at getBaudRate[]

Out[6]= 9600

In[7]:= serialPort at setSerialPortParams[19200, SerialPort`DATABITSU8, 

(* Here is how to write a command to the port. *)

In[8]:= strm = serialPort at getOutputStream[];

In[9]:= strm at write[ToCharacterCode["command"]]

That's it. We have a full, interpreted scripting environment for 
experimenting with or programming the serial port in a completely portable way.

I apologize for this long, blatantly promotional post, but I want to make 
sure that J/Link reaches its full audience (and hey, the thing is free). We 
hope that people find J/Link to be a useful and valuable addition to the 
Mathematica universe.


Todd Gayley
Director of Java Technology
Wolfram Research, Inc.
tgayley at

  • Prev by Date: Re: Simplifying Problems
  • Next by Date: Signing with Inequality Constraints
  • Previous by thread: Re: Transformation Methods for Pi
  • Next by thread: Signing with Inequality Constraints