user24877
user24877

Reputation: 155

Specifying classpath for classes inside the JAR itself

If I have a class org.foobar.MyClass and want to put it in a JAR file, do I have to put it in the JAR's /org/foobar/ directory, or can I put it in /bin/org/foobar/ and somehow specify /bin/ as classpath inside the JAR itself?

Upvotes: 4

Views: 5492

Answers (5)

Chochos
Chochos

Reputation: 5159

you can include the Class-Path property in your Manifest, listing the jar files your app depends on. The paths will be considered relative to the location of your executable JAR.

For example if your app.jar contains this in the MANIFEST.MF:

Class-Path: lib1.jar,lib/lib2.jar

Then the JVM will expect to find lib1.jar in the same dir as app.jar and a subdirectory called lib containing lib2.jar.

Upvotes: 3

Eddie
Eddie

Reputation: 54421

The usual way that everyone does this -- so far I've never seen a JAR do something different -- is to put class org.foobar.MyClass in the JAR file at the JAR's /org/foobar/ directory. I can't imagine a good reason for doing something differently, as it would impede normal use of this JAR by anyone not doing unusual things.

Upvotes: 1

Tom Hawtin - tackline
Tom Hawtin - tackline

Reputation: 147124

The name in the jar file has to match the class name. Unless of course you write a custom class loader to do weird stuff.

You can specify a "directory" within jar file using a jar URL. However, I don't think URLClassLoader handles this correctly (not tried).

A feature that does exist as standard is to add other jar files on to the classpath by specifying them in the manifest.

Upvotes: 0

matt b
matt b

Reputation: 139921

Why would you do the latter? It's easier to just put it in the correct directory based on the package name, which avoids having to edit the MANIFEST file or doing any other sort of special settings.

Upvotes: -1

Matthew Brubaker
Matthew Brubaker

Reputation: 3117

I believe you can accomplish this by using the MANIFEST file. If I remember correctly, you can specify the location of a particular file within the jar.

Upvotes: 0

Related Questions