MathGroup Archive 2010

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

Search the Archive

Re: Trying to do some stuff with JLINK ???

  • To: mathgroup at smc.vnet.net
  • Subject: [mg110029] Re: Trying to do some stuff with JLINK ???
  • From: David Bailey <dave at removedbailey.co.uk>
  • Date: Sat, 29 May 2010 04:46:25 -0400 (EDT)
  • References: <hto93d$kso$1@smc.vnet.net>

On 28/05/10 12:23, jalbers wrote:
> Get["JLink`"]
> InstallJava[]
> k = JavaNew["javax.swing.JOptionPane"]
> k@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


  • Prev by Date: Re: Q about ParametricPlot3D
  • Next by Date: Re: Lists: how to?
  • Previous by thread: Trying to do some stuff with JLINK ???
  • Next by thread: Lists: how to?