简体   繁体   中英

Specifying classpath for classes inside the JAR itself

如果我有一个类org.foobar.MyClass并且想要将它放在JAR文件中,我是否必须将它放在JAR的/ org / foobar /目录中,或者我可以将它放在/ bin / org / foobar /中以某种方式指定/ bin /作为JAR本身内部的类路径?

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.

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.

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM