From: jalbers on
I have been experimenting with JLINK trying to get it to work.

Looking at an example in a Java Programming Book, found the code to
display a message dialog box:
JOptionPane.showMessageDialog(null,"Welcome to Java")

I am trying to get this to work in Mathematica but I am getting an
error message about arguments on the 4th line ???

Get["JLink`"]
InstallJava[]
k = JavaNew["javax.swing.JOptionPane"]
k(a)showMessageDialog["Welcome to Java"]

Also why does the LoadClass[] in my Mathematica 7 give error messages
for anything. For example:
LoadClass["javax.comm.CommPortIdentifier"] and
LoadClass["javax.swing.JOptionPane"] both give error messages.

I don't know very much about Java. What am I doing wrong? Any help
would be greatly appreciated. Thanks

From: David Bailey on
On 28/05/10 12:23, jalbers wrote:
> Get["JLink`"]
> InstallJava[]
> k = JavaNew["javax.swing.JOptionPane"]
> k(a)showMessageDialog["Welcome to Java"]

Here is some working code:

Needs["JLink`"]

InstallJava[];
LoadJavaClass["javax.swing.JOptionPane"];

javax`swing`JOptionPane`showMessageDialog[Null,
JavaNew["java.lang.String", "Welcome to Java"]];

Note that the method you are trying to call is a static method, so you
load the class as shown, and all the static methods appear as functions
in the nested contexts corresponding to the full Java class name.

Note also that the method you needed to call was a slightly tricky case
because it takes a string in the form of an object. This meant that it
was necessary to explicitly create a string object (which then gets
automatically cast to object type. Methods that take explicit string
arguments don't need this - J/Link does this step automatically.

I am not sure why

LoadJavaClass["javax.comm.CommPortIdentifier"]

fails - I suspect it lies in an extra library that you will need to
download and add to your class path.

If you are going to make more than trivial use of Java, I would suggest
putting your code in a Java file and compiling it - using J/Link to call
one or more of the methods you have written.

You do need some knowledge of Java to do much with JLink :(

David Bailey

http://www.dbaileyconsultancy.co.uk