Importing from a Jar file WITHOUT unzipping it

M

Martyr2

Is there anyway I can put a bunch of classes into a jar file (no entry
point) and simply import that jar file into java applications I am
working on and access the individual classes through something like
dot notation? Much like a package, but actually zipped together in a
jar and my application be able to just pull out classes as needed.

Any guidance you can provide would be fantastic.
 
A

Andreas Leitgeb

Martyr2 said:
Is there anyway I can put a bunch of classes into a jar file (no entry
point) and simply import that jar file into java applications I am
working on and access the individual classes through something like
dot notation? Much like a package, but actually zipped together in a
jar and my application be able to just pull out classes as needed.

Adding your jarfile to the classpath of the java applications seems to
match your expressed wishes to 100%.

Or did I misunderstand the question?
 
M

Mark Space

Martyr2 said:
Is there anyway I can put a bunch of classes into a jar file (no entry
point) and simply import that jar file into java applications I am
working on and access the individual classes through something like
dot notation? Much like a package, but actually zipped together in a
jar and my application be able to just pull out classes as needed.

Any guidance you can provide would be fantastic.

What Andreas said. The normal method is to allow the user to install
and manage the extra libraries themselves. This makes thing easy for
pros who manage their systems themselves. Note that you have to include
the name of the jar file itself in the classpath.

CLASSPATH=/some/path/lib/myLibrary.jar

Just pointing to the directory won't work, that's for loose classes. I
think that .zip files will work exactly the same as .jar files, too.

Also, if your app is a .jar, you have to set the classpath in the
manifest file. The environment variable is ignored for "java -jar".

Other solutions:

1. Get a Java installer, those will install the needed library and set
the path appropriate.

2. Use JWS, it'll download the library as needed.

3. Use OneJar. This is an old and atypical solution, but if you just
need to package one jar inside of another for a quickie application,
it'll work.
 
A

Arne Vajhøj

Martyr2 said:
Is there anyway I can put a bunch of classes into a jar file (no entry
point) and simply import that jar file into java applications I am
working on and access the individual classes through something like
dot notation? Much like a package, but actually zipped together in a
jar and my application be able to just pull out classes as needed.

Any guidance you can provide would be fantastic.

You can extract a file from a jar file to memory and load the
code from memory.

(I can create code to do so, if you are interested)

But as said by others: why bother - the normal classloader
should do it all for you.

Arne
 
L

Lew

Mark said:
CLASSPATH=/some/path/lib/myLibrary.jar

Just pointing to the directory won't work, that's for loose classes.

It will if you wildcard the directory:

-classpath /some/path/lib/*

Normally one should eschew the CLASSPATH envar.
 
A

Andreas Leitgeb

Lew said:
It will if you wildcard the directory:
-classpath /some/path/lib/*

Are you talking about some new feature of 1.6.0_10 here?

This year's spring I was attending some one-day Sun event, and the
speaker talked somewhat self-ironically about various features of
the "next big update 10": (Rephrased roughly from my bubble-memory)

"... We knew paths and wildcards for decades, but, the heck,
why did it take us till 1.6.0_10 to get them together???"
(Laughter from the audience, mostly programmer-types,
including me)
 
L

Lew

Andreas said:
Are you talking about some new feature of 1.6.0_10 here?

Class path entries can contain the basename wildcard character *, which is
considered equivalent to specifying a list of all the files in the directory
with the extension .jar or .JAR.

It came out before update 10.

If you're using obsolete or obsolescent versions of Java, YMMV.
 
L

Lew

Lew said:
It came out before update 10.

If you're using obsolete or obsolescent versions of Java, YMMV.

Based on the copyright date, this feature has been in place for two years now.
 
A

Andreas Leitgeb

Lew said:
Based on the copyright date, this feature has been in place for two years now.

Hmm, you see me confused about whatever I must have missed on that
event. He did talk about something new in a (back then) future version.

Maybe the speaker himself mixed it up (didn't know it was there
already), or I misunderstood him. It's up to you to emphasize
the latter of these options :)

I didn't ever try to use the feature, so far, so it might have been
just under my hands the last couple of years ...

By that time, iirc, 1.6.0_04 (or up to 06) was current.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top