From: John_V on
I'm trying to learn how to use J/Link, and I have run into an elementary
(I hope) problem when I try to access static methods and fields. For
example, the Java Math class has static fields for E and PI and a number
of static methods, abs() among them. Here's what happens when I try to
use them:

My inputs:

Needs["JLink`"]
InstallJava[]
myMathClass = LoadJavaClass["java.lang.Math"]
Fields[myMathClass]
pi = myMathClass`PI
myMathClass`abs[-5]

/Mathematica/'s outputs:

Out[2]= LinkObject[...]

Out[3]= JavaClass[java.lang.Math, <> ]

Out[4]//TableForm=
static final double E
static final double PI

Out[5]= myMathClass`PI

Out[6]= myMathClass`abs[-5]

I believe the fact that Out[4] correctly lists java.lang.Math's static
fields means LoadJavaClass worked, but I expected Out[5] = 3.1415... and
Out[6] = 5. I thought this was a near verbatim transcription of the
examples in the "Calling Methods and Accessing Fields" part of the
J/Link user guide. What am I doing wrong?

Thanks,

John

From: Albert Retey on
Am 15.07.2010 09:10, schrieb John_V:
> I'm trying to learn how to use J/Link, and I have run into an elementary
> (I hope) problem when I try to access static methods and fields. For
> example, the Java Math class has static fields for E and PI and a number
> of static methods, abs() among them. Here's what happens when I try to
> use them:
>
> My inputs:
>
> Needs["JLink`"]
> InstallJava[]
> myMathClass = LoadJavaClass["java.lang.Math"]
> Fields[myMathClass]
> pi = myMathClass`PI
> myMathClass`abs[-5]
>
> /Mathematica/'s outputs:
>
> Out[2]= LinkObject[...]
>
> Out[3]= JavaClass[java.lang.Math, <> ]
>
> Out[4]//TableForm=
> static final double E
> static final double PI
>
> Out[5]= myMathClass`PI
>
> Out[6]= myMathClass`abs[-5]
>
> I believe the fact that Out[4] correctly lists java.lang.Math's static
> fields means LoadJavaClass worked, but I expected Out[5] = 3.1415... and
> Out[6] = 5. I thought this was a near verbatim transcription of the
> examples in the "Calling Methods and Accessing Fields" part of the
> J/Link user guide. What am I doing wrong?

You are just using the wrong namespace, this should work:

Needs["JLink`"]

LoadJavaClass["java.lang.Math"]

java`lang`Math`PI

java`lang`Math`abs[-5]

hth,

albert

From: John_V on
Well, problem solved. I'm answering my own post in case others have been
confused as I was.

As I discovered on my own after posting

Needs["JLink`"]
InstallJava[]
myMathClass = LoadJavaClass["java.lang.Math"]
pi = java`lang`Math`PI
java`lang`Math`abs[-5]


works. An e-mail from Wolfram told me I can also use the shorter form

pi = Math`PI
Math`abs[-5]

The key (and my point of confusion) is that the short form uses the Java
class name, *not* the object returned by LoadJavaClass["..."]

John


On 7/15/2010 3:09 AM, John_V wrote:
> I'm trying to learn how to use J/Link, and I have run into an elementary
> (I hope) problem when I try to access static methods and fields. For
> example, the Java Math class has static fields for E and PI and a number
> of static methods, abs() among them. Here's what happens when I try to
> use them:
>
> My inputs:
>
> Needs["JLink`"]
> InstallJava[]
> myMathClass = LoadJavaClass["java.lang.Math"]
> Fields[myMathClass]
> pi = myMathClass`PI
> myMathClass`abs[-5]
>
> /Mathematica/'s outputs:
>
> Out[2]= LinkObject[...]
>
> Out[3]= JavaClass[java.lang.Math,<> ]
>
> Out[4]//TableForm=
> static final double E
> static final double PI
>
> Out[5]= myMathClass`PI
>
> Out[6]= myMathClass`abs[-5]
>
> I believe the fact that Out[4] correctly lists java.lang.Math's static
> fields means LoadJavaClass worked, but I expected Out[5] = 3.1415... and
> Out[6] = 5. I thought this was a near verbatim transcription of the
> examples in the "Calling Methods and Accessing Fields" part of the
> J/Link user guide. What am I doing wrong?
>
> Thanks,
>
> John
>
>


From: David Bailey on
On 15/07/10 08:10, John_V wrote:
> I'm trying to learn how to use J/Link, and I have run into an elementary
> (I hope) problem when I try to access static methods and fields. For
> example, the Java Math class has static fields for E and PI and a number
> of static methods, abs() among them. Here's what happens when I try to
> use them:
>
> My inputs:
>
> Needs["JLink`"]
> InstallJava[]
> myMathClass = LoadJavaClass["java.lang.Math"]
> Fields[myMathClass]
> pi = myMathClass`PI
> myMathClass`abs[-5]
>
> /Mathematica/'s outputs:
>
> Out[2]= LinkObject[...]
>
> Out[3]= JavaClass[java.lang.Math,<> ]
>
> Out[4]//TableForm=
> static final double E
> static final double PI
>
> Out[5]= myMathClass`PI
>
> Out[6]= myMathClass`abs[-5]
>
> I believe the fact that Out[4] correctly lists java.lang.Math's static
> fields means LoadJavaClass worked, but I expected Out[5] = 3.1415... and
> Out[6] = 5. I thought this was a near verbatim transcription of the
> examples in the "Calling Methods and Accessing Fields" part of the
> J/Link user guide. What am I doing wrong?
>
> Thanks,
>
> John
>

After loading the relevant class, you should be able to access static
members by using the full Java name with context marks instead of the
periods:
In[8]:= java`lang`Math`PI

Out[8]= 3.14159

Note that although these appear like simple Mathematica variables, they
have been setup with SetDelayed, so that evaluateing one accesses the
corresponding variable in Java - to see this, type:

?java`lang`Math`PI

David Bailey
http://www.dbaileyconsultancy.co.uk

From: Raffy on
On Jul 15, 12:10 am, John_V <jvillar.j...(a)gmail.com> wrote:
> I'm trying to learn how to use J/Link, and I have run into an elementary
> (I hope) problem when I try to access static methods and fields. For
> example, the Java Math class has static fields for E and PI and a number
> of static methods, abs() among them. Here's what happens when I try to
> use them:
>
> My inputs:
>
> Needs["JLink`"]
> InstallJava[]
> myMathClass = LoadJavaClass["java.lang.Math"]
> Fields[myMathClass]
> pi = myMathClass`PI
> myMathClass`abs[-5]
>
> /Mathematica/'s outputs:
>
> Out[2]= LinkObject[...]
>
> Out[3]= JavaClass[java.lang.Math, <> ]
>
> Out[4]//TableForm=
> static final double E
> static final double PI
>
> Out[5]= myMathClass`PI
>
> Out[6]= myMathClass`abs[-5]
>
> I believe the fact that Out[4] correctly lists java.lang.Math's static
> fields means LoadJavaClass worked, but I expected Out[5] = 3.1415... and
> Out[6] = 5. I thought this was a near verbatim transcription of the
> examples in the "Calling Methods and Accessing Fields" part of the
> J/Link user guide. What am I doing wrong?
>
> Thanks,
>
> John

LoadJavaClass (by default) exposes the static members in the context
hierarchy that mirror the Java class name.

ie. java.lang.Math -> java`lang`Math

So if you want java.lang.Math.PI, you can access it from
java`lang`Math`PI