Re: Accessing .class files in a Jar



Redbeard wrote:

Nigel Wade wrote:
This will only work if the class files they download belong to the same
package
as the class files they are creating for themselves. That seems very unlikely
to me, unless you and the students are both using the "default" package, and
that's probably not a good idea.
Actually, I want them to use the "default" package. They haven't even
learned loops yet, so they certainly are not ready for packages. They
have used import statements, but some may be working on machines where
they won't have sufficient rights to put a file in the ext folder.
Which is why I'm trying to come up with an alternative.

But I tried simply jarring up the class files, and leaving them in the
same folder as the "application" file (main method). They were not
recognized. That is what prompted me to post.

But I'd like to be able to Jar them up and just let the students
download the one Jar file into that directory and access the files
stored there.

They can do that, provided you build your jar correctly. You need to ensure
that
the filesystem within the jar matches the package structure of the class
files.
Then the students will need to use the correct import statements to access
your
classes, and reference the jar in the classpath both for compilation and for
execution.
I also tried putting the files that I want to jar into a package,
jarring up the package, and importing the files. I've done this with
jars that end up in the ext folder. But it doesn't seem to work in
this situation.

I've experimented with this without success. Before I waste too much
time on it, I figured it would make sense to see if anyone knows if it
can even be done.


Certainly it can be done. That's how Java works, installing jar files
downloaded
from the net is in essense what you do when you install the Java SDK/JRE. One
of the strengths of Java is that it's so easy to incorporate existing jars
into
your own applications.
I've done this several times with jars that go in the ext directory, so
I know that works. But I'm not having any luck with just leaving the
jar in the folder with the project.

Thanks for your reply.

It sounds like what I am trying to do SHOULD work. If you any ideas
what I might be doing wrong, I'm all ears.

Java won't look for classes in a jar unless it's told to. Class files are
searched on the classpath, and if this includes the current directory, then
class files in the current directory should be found. However, jar's in the
current directory are not searched. You need to explicitly add a jar to the
classpath. Jars in ext are found because it's always searched.

For example, if I create a simple class and put it in a jar, let's call the
class ClassA and the jar ajar.jar. To create the jar I do:

$ javac ClassA.java
$ jar cf ajar.jar ClassA.class

To be sure that the ClassA is used from the jar I remove both ClassA.java and
ClassA.class.

Then I create a main class which uses ClassA, lets call it MainClass and put
this in MainClass.java. I compile MainClass.java as:

$ javac -cp ajar.jar MainClass.java

and run it as:

$ java -cp ajar.jar:. MainClass

Note that I've had to specify both the jar and "." in the classpath so that java
can locate classes in the jar and in the current directory.

What you want to do is possible.

--
Nigel Wade, System Administrator, Space Plasma Physics Group,
University of Leicester, Leicester, LE1 7RH, UK
E-mail : nmw@xxxxxxxxxxxx
Phone : +44 (0)116 2523548, Fax : +44 (0)116 2523555
.



Relevant Pages

  • Re: Running java programs from class files
    ... java -classpath /xyz/abc MyPackage.MyClass ... But put it in a package and it won't. ... So now that you solved that one, show me how to use a jar library on the ... line in the manifest and put the jar file in the same ...
    (comp.lang.java.programmer)
  • Compiling in Eclipse (WinXP), trying to run java program on Linux, Serializability
    ... Ok I compile my program in Eclipse, works fine in windows of course ... Then I move the class files into a jar, try to run the jar in Suse ... Windows does java -jar its fine for him. ...
    (comp.os.linux.development.apps)
  • Re: Class-Path entry for a jar manifest file
    ... >>directory structure corresponding to the package name structure (as it ... >>of jar names relative to the root of the containing jar file. ... >>You might want to look at the Jar trail of the Java Tutorial. ... The other alternative is to forget about the internal jar layout you ...
    (comp.lang.java.programmer)
  • Re: OT: It aint what it appears to be
    ... power tools and gadgets, "arrr, arrr". ... would not open its twin package. ... Once I even had to call my son-in-law to open a jar ... released the pressure and the lid turns off easily. ...
    (alt.support.mult-sclerosis)
  • Re: Howto: deploy socket server to linux
    ... It will be simple Java app running at Linux ... If your code doesn't use a package declaration, then just putting the class files in the directory where you run java will do the job: ... As well as all this, java supports some things called JAR archives, which are basically zip files with some metadata. ...
    (comp.lang.java.programmer)