[R-sig-Geo] RE: Using SJava?

Benjamin.STABLER at odot.state.or.us Benjamin.STABLER at odot.state.or.us
Thu Apr 8 21:00:11 CEST 2004


Roger,

To read the contents of a JAR use the java.util.jar package.  In terms of
the first two issues - 1) you shouldn't need to create an object to call a
static method and 2) referencing classes within jars should work.  Let me
first say that I am no Java expert.  I will just share what I (think I) have
figured out over time.  In order for class references to work within jars
and within nested packages you need to reference the class correctly.  I
like to think of packages as just fancy names for folders.  So if I add
"package tools;" to the top of sig.java and then I compile it, I must
reference that folder correctly.  First I have to create a folder in the
folder that I added to the class path and copy sig.class to that folder.  To
call the class in R, I must refer to the class within the tools package.

.JavaInit(config=javaConfig(classPath="F:/_ben/java"))

> .Java("tools.sig","print","Ben Stabler")
[1] "Ben Stabler"                         "Oregon Department of
Transportation"
[3] "Mill Creek Office Building"          "555 13th Street NE Suite 2"

[5] "Salem, OR 97301-4178"             

When you put classes inside jars, the packages (folders) must be included.
So I put the sig.class file in the jar and refer to tools.sig in the method
call.  Note also that I created the jar file with the following command:

jar -cvf sig.jar tools

This added the tools folder to the jar.

> library(SJava)
using JAVA_HOME = C:\j2sdk1.4.2\jre 
> .JavaInit(config=javaConfig(classPath="F:/_ben/sig.jar"))
> .Java("tools.sig","print","Ben Stabler")
[1] "Ben Stabler"                         "Oregon Department of
Transportation"
[3] "Mill Creek Office Building"          "555 13th Street NE Suite 2"

[5] "Salem, OR 97301-4178"               
> 

If I add the tools folder to the jar by referring to the class via the
folder then it gets added:

jar -cvf sig.jar tools/sig.class

> library(SJava)
using JAVA_HOME = C:\j2sdk1.4.2\jre 
> .JavaInit(config=javaConfig(classPath="F:/_ben/sig.jar"))
> .Java("tools.sig","print","Ben Stabler")
[1] "Ben Stabler"                         "Oregon Department of
Transportation"
[3] "Mill Creek Office Building"          "555 13th Street NE Suite 2"

[5] "Salem, OR 97301-4178"               
> 

But if I just add the file (the current working directory is tools)

jar -cvf sig.jar sig.class

then I lost the folder reference and I can't get to the class within the jar

> library(SJava)
using JAVA_HOME = C:\j2sdk1.4.2\jre 
> .JavaInit(config=javaConfig(classPath="F:/_ben/tools/sig.jar"))
> .Java("sig","print","Ben Stabler")
NULL
> .Java("tools.sig","print","Ben Stabler")
NULL
> 

So hopefully that should help clear up some of the classpath and package
reference issues.  There are probably better ways to work with the packages
and classpath - I just don't know them.  These notes work and that is good
enough for my purposes.  

Benjamin Stabler
Transportation Planning Analysis Unit
Oregon Department of Transportation
555 13th Street NE, Suite 2
Salem, OR 97301  Ph: 503-986-4104

>-----Original Message-----
>From: Roger Bivand [mailto:Roger.Bivand at nhh.no]
>Sent: Tuesday, April 06, 2004 5:01 AM
>To: STABLER Benjamin
>Cc: r-sig-geo at stat.math.ethz.ch
>Subject: Re: [R-sig-Geo] RE: Using SJava?
>
>
>Again, very promising, the simple jars can be accessed - what 
>is the next 
>step?
>
>So far we have SJava (the Windows binary downloaded from 
>http://www.stats.ox.ac.uk/pub/RWin) talking to itself, and to 
>two simple 
>Java examples. There are two issues, the classpath one, and 
>whether or not 
>to call .JavaConstructor() to create a copy on the R side of 
>the class. Is 
>there a way of asking a jar to reveal the class names it 
>contains - are 
>the class structures "discoverable". Could I ask "sig" for its 
>length, for 
>example?
>
>I'm keeping this discussion on the list for now, because accessing 
>geotools (and other Java resources) is at least worth trying, 
>and to do 
>that more hands and eyes are needed.
>
>Roger
>
>
>On Mon, 5 Apr 2004 Benjamin.STABLER at odot.state.or.us wrote:
>
>> Frank and Roger,
>> 
>> Sorry about the path reference - I just added the example on 
>the fly in the
>> email and forgot about the R escape character.  Attached is 
>the simply class
>> that I used to test the link.  It just takes a string as an input and
>> returns my email signature with the string substituted for the name.
>> 
>> The R code to run it:
>> 
>> library(SJava)
>> .JavaInit(config=javaConfig(classPath="F:/_ben/java"))
>> .Java("sig","print","Ben Stabler")
>> 
>> 
>> In terms of the jar file, it seems to work for me.  Note 
>that I moved the
>> jar file to a different directory so it did not find the 
>class file by
>> itself as well.
>> 
>> > library(SJava)
>> using JAVA_HOME = C:\j2sdk1.4.2\jre 
>> > .JavaInit(config=javaConfig(classPath="F:/_ben/sig.jar"))
>> > .Java("sig","print","Ben Stabler")
>> [1] "Ben Stabler"                         "Oregon Department of
>> Transportation"
>> [3] "Mill Creek Office Building"          "555 13th Street 
>NE Suite 2"
>> 
>> [5] "Salem, OR 97301-4178"               
>> > 
>> 
>> The print method of the sig class is static so I'm not 
>having any difficulty
>> using static methods and I did not need to create a 
>constructor first.  You
>> shouldn't need a constructor/object for a static method 
>(that is the point
>> of static methods).  Unfortunately my IS department blocked 
>the zip file you
>> sent.  Could you send me the code as text or maybe just copy 
>into email?
>> Thanks.  
>> 
>> Ben
>> 
>> 
>> >-----Original Message-----
>> >From: Frank Hardisty [mailto:HardistF at gwm.sc.edu]
>> >Sent: Monday, April 05, 2004 1:07 PM
>> >To: Roger.Bivand at nhh.no
>> >Cc: STABLER Benjamin
>> >Subject: Re: [R-sig-Geo] RE: Using SJava?
>> >
>> >
>> >Roger and Benjamin,
>> >
>> >Benjamin, your advice is good and the test class does work. 
>Roger, one
>> >difficulty is that we were calling a static method of the 
>class, which
>> >does NOT work for some reason. However, the class can be 
>instantiated
>> >and used from within R. Here's my session with my mistakes 
>edited out:
>> >
>> >[Previously saved workspace restored]
>> >
>> >> library(SJava)
>> >using JAVA_HOME = C:\programs\j2sdk_nb\j2sdk1.4.2 
>> >> 
>.JavaInit(config=javaConfig(classPath="C:/temp/javatest/test.jar"))
>> >> testClass <- .JavaConstructor("TestJava")
>> >> .Java(testClass,"getMessage")
>> >[1] "Hi Roger!"
>> >> 
>> >
>> >Benjamin, do you also find that static methods don't work in SJava?
>> >It's hard to understand, because calls to static methods in the Java
>> >runtime libraries work just fine, for example, 
>> >
>> >> .Java("Math","random")
>> >[1] 0.07714792
>> >
>> >regards,
>> >-Frank
>> >
>> >>>> Roger Bivand <Roger.Bivand at nhh.no> 04/05/04 03:36PM >>>
>> >On Mon, 5 Apr 2004 Benjamin.STABLER at odot.state.or.us wrote:
>> >
>> >> Hopefully, I'm not too late on this one....when you initialize the
>> >JVM you
>> >> need to set the class path as follows:
>> >> 
>> >>
>> 
>>.JavaInit(config=javaConfig(classPath="StringDirectoryPathReference"))
>> >> 
>> >> or
>> >> 
>> >> .JavaInit(config=javaConfig(classPath=c("String1", "String2")))
>> >> 
>> >> for example:
>> >> 
>> >> .JavaInit(config=javaConfig(classPath="C:\javastuff"))
>> >> 
>> >> where javastuff contains my class files.  You need to 
>specify the jar
>> >file
>> >> name if you are trying to include class files within jars.  I have
>> >used it
>> >> with some classes that I wrote.  
>> >
>> >Yes, and Sys.putenv("CLASSPATH"="String1") can be used too, 
>but what do
>> >
>> >you put in .Java() to get into your own jars? Are there 
>escaping issues
>> >in 
>> >the path to the jars? (backslash twice? forwardslash?) 
>Could we put up
>> >
>> >some jars and some instructions for use on a website? Frank Hardisty
>> >sent 
>> >me an example off-list that works from the command line but which I
>> >can't 
>> >find from within R/SJava (Windows XP, the SJava examples that I've
>> >tried 
>> >work for me). 
>> >
>> >The idea would be to see if GeoTools2 can be reached from within R, 
>> >because it could provide (another) way to import/export 
>multiple GIS 
>> >formats.
>> >
>> >Roger
>> >
>> >> 
>> >> I too am excited about linking Java and R, as there is so 
>much useful
>> >code
>> >> in the Java community.
>> >> 
>> >> Benjamin Stabler
>> >> Transportation Planning Analysis Unit
>> >> Oregon Department of Transportation
>> >> 555 13th Street NE, Suite 2
>> >> Salem, OR 97301  Ph: 503-986-4104
>> >> 
>> >> 
>> >> >Message: 5
>> >> >Date: Fri, 2 Apr 2004 20:56:50 +0200 (CEST)
>> >> >From: Roger Bivand <Roger.Bivand at nhh.no>
>> >> >Subject: [R-sig-Geo] Using SJava?
>> >> >To: r-sig-geo at stat.math.ethz.ch 
>> >> >Cc: Frank Hardisty <HardistF at gwm.sc.edu>
>> >> >Message-ID:
>> ><Pine.LNX.4.44.0404022042580.28093-100000 at reclus.nhh.no>
>> >> >Content-Type: TEXT/PLAIN;	charset=US-ASCII
>> >> >
>> >> >Is anyone on this list using SJava or any R/Java connection? 
>> >> >Not only is
>> >> >the JTS Topology Suite that Tim just mentioned interesting, 
>> >> >but so is the
>> >> >upcoming GeoTools2 at http://www.geotools.org/. Both are 
>> >> >likely to be used 
>> >> >quite a lot, and thus probably well-maintained.
>> >> >
>> >> >At the recent Association of American Geographers meeting, 
>> >> >Frank Hardisty
>> >> >asked me about this, and while I was able to install SJava (from
>> >> >http://www.stats.ox.ac.uk/pub/RWin) on a Win XP laptop, we 
>> >> >were not able
>> >> >to see how to get Java within R to see GeoTools in its 
>> >> >classpath.  Both of
>> >> >these Java resources are potentially useful, and R opinions 
>> >> >two or three
>> >> >years ago, that Java is slow, may need revision given increased
>> >machine
>> >> >capacity. I feel we could benefit from mobilizing Java insight 
>> >> >(but I feel
>> >> >personally Java-challenged!).
>> >> >
>> >> >I believe that Duncan Temple-Lang was playing 
>with/working on object
>> >
>> >> >discovery - this could be very relevant in terms of matching 
>> >> >R-internal 
>> >> >object representations with those in existing software that we 
>> >> >could - if 
>> >> >SJava worked - link to. I also think that we could ask him for 
>> >> >advice if 
>> >> >we discussed it first here. What do others think?
>> >> >
>> >> >-- 
>> >> >Roger Bivand
>> >> >Economic Geography Section, Department of Economics, Norwegian 
>> >> >School of
>> >> >Economics and Business Administration, Breiviksveien 40, N-5045
>> >Bergen,
>> >> >Norway. voice: +47 55 95 93 55; fax +47 55 95 93 93
>> >> >e-mail: Roger.Bivand at nhh.no 
>> >> >
>> >> >
>> >> >
>> >> 
>> >> _______________________________________________
>> >> R-sig-Geo mailing list
>> >> R-sig-Geo at stat.math.ethz.ch 
>> >> https://www.stat.math.ethz.ch/mailman/listinfo/r-sig-geo 
>> >> 
>> >
>> >-- 
>> >Roger Bivand
>> >Economic Geography Section, Department of Economics, 
>Norwegian School
>> >of
>> >Economics and Business Administration, Breiviksveien 40, N-5045
>> >Bergen,
>> >Norway. voice: +47 55 95 93 55; fax +47 55 95 93 93
>> >e-mail: Roger.Bivand at nhh.no 
>> >
>> >
>> >
>> 
>> 
>
>-- 
>Roger Bivand
>Economic Geography Section, Department of Economics, Norwegian 
>School of
>Economics and Business Administration, Breiviksveien 40, N-5045 Bergen,
>Norway. voice: +47 55 95 93 55; fax +47 55 95 93 93
>e-mail: Roger.Bivand at nhh.no
>
>




More information about the R-sig-Geo mailing list