Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / November 2006

Tip: Looking for answers? Try searching our database.

Accessing .class files in a Jar

Thread view: 
Redbeard - 23 Nov 2006 06:09 GMT
Can this be done?

I am in an educational setting where students will need some class
files that I have created to make a project work.  They need to be able
to download the files via the internet, so I cannot Jar them up and
preinstall them in the ext directory.  And I cannot be certain that
they could accomplish that themselves.

Right now, they will have to download the files individually and place
them in the same directory that they will create their project in.
Assuming that they don't miss a file, that WILL work.

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.

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.

TIA
Kunkhmer - 23 Nov 2006 07:49 GMT
I wrote this batch quite a while ago, you might have a look and do
similar way to build your jar file. If you don't have custom manifest
file just ignore this part "config\PUZZLEMANIFEST.MF"

--- start of file ---

REM Set a temporary Environment Variables
set PUZZLESRC=source\isys\puzzle
set PUZZLECLS=classes
set PUZZLEJAR=jar

ECHO Delete previous build including all classes and jar files
del /q %PUZZLECLS%\manet %PUZZLEJAR%\*.jar

ECHO Compiling entire puzzle game source ...
javac %PUZZLESRC%\functions\*.java %PUZZLESRC%\objects\*.java
%PUZZLESRC%\*.java -d classes\

ECHO Create JAR archive with custom manifest file then move to
apropriate destination
jar cvfm PuzzleSolver.jar config\PUZZLEMANIFEST.MF -C %PUZZLECLS% isys
move PuzzleSolver.jar %PUZZLEJAR%

--- end of file ---

However, this might not be a very good example. but it should work well
for building a small project.

hope it helps,

M.Y
Nigel Wade - 23 Nov 2006 10:34 GMT
> Can this be done?
>
[quoted text clipped - 7 lines]
> them in the same directory that they will create their project in.
> Assuming that they don't miss a file, that WILL work.

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.

> 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'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.

Signature

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

Redbeard - 24 Nov 2006 01:55 GMT
> 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
[quoted text clipped - 5 lines]
> 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
[quoted text clipped - 4 lines]
> 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.
Nigel Wade - 24 Nov 2006 10:25 GMT
>> 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
[quoted text clipped - 40 lines]
> 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.

Signature

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

Redbeard - 25 Nov 2006 03:04 GMT
> You need to explicitly add a jar to the classpath.

Which is what I have not been doing.

> 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:
[quoted text clipped - 16 lines]
> 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.

Thanks!  This is what I was doing.

> What you want to do is possible.
Most of the students will be locked out of the command prompt -
education setting - very tight security.  But they should be using the
same IDE, so I should be able to give them directions how to set the
classpath via the IDE.

Thanks again!
Nigel Wade - 27 Nov 2006 10:41 GMT
>> 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.
[quoted text clipped - 8 lines]
>
> Thanks again!

Most IDEs allow that functionality.  The ones I am familiar with (NetBeans and
Eclipse) both allow that. I'm not a fan of IDEs for learning, your students
will probably spend far too much time learning the IDE, rather than learning
Java. I'm sure you are finding out just how much time learning to perform
simple tasks within an IDE can occupy...

Signature

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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.