Skip to content

Using SJava?

1 message · Benjamin.STABLER at odot.state.or.us

#
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"))
[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.
using JAVA_HOME = C:\j2sdk1.4.2\jre
[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
using JAVA_HOME = C:\j2sdk1.4.2\jre
[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
using JAVA_HOME = C:\j2sdk1.4.2\jre
NULL
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