简体   繁体   中英

how to put folders inside a jar file into the classpath?

I am mantaining an old java application which has dcm4che as its library. The dcm4che library is not just a jar file. It is a folder which contains jar files and other folders with some properties files and classes in different folders. I am using WindowsXP and java 1.6.

When we run our application, we need to put those folders, jar files into the classpath by doing something like these:

set CLASSPATH=.;%PROJ_HOME%\classes;^
%THIRDPARTY_HOME%\dcm4che;^
%THIRDPARTY_HOME%\dcm4che\classes;^
%THIRDPARTY_HOME%\dcm4che\resources;^

Now I try to put all of them into one jar file to simplify the sources control. So I create a manifest.txt file with the following content:

Class-Path: classes;resources;lib\dcm4che.jar;getopt.jar

Then I create a dcm4che.jar by running jar:

jar -cfm dcm4che.jar manifest.txt *

A dcm4che.jar is created. The I verify the result by extracting all contents and check the manifest file and my classpath is there:

Manifest-Version: 1.0
Class-Path: classes;resources;lib\dcm4che.jar;getopt.jar
Created-By: 1.6.0_25 (Sun Microsystems Inc.)

Then I modify the application classpath to point to the new generated dcm4che.jar and delete those older entries.

When I run it, I get NoClassDefFoundError. But that class is under the classes folder inside the new created jar file.

I am missing anything?

Java does not support jars in jars. So, the easiest way is to expand this file and add all it content to classpath. I think you can easily do this using script. You anyway have script that runs your application, right? So add several lines that expand this file to temporary or user home directory.

If you do not want to do this or it is forbidden for other reasons you have to implement your own classloader that loads cliesses from nested jar. It is not so hard to do but it requires some efforts. You can use VFS from Jakarta. It will simplify implementation.

But I'd recommend you the first way.

You could use ONE-JAR to package the class files and jar files into your final executable.

A jar file is essentially a zip file.

There are various utilities which allow you to mount zip files as a read-only drive or dvd.

Similarly, there are utilities that mount iso images as a cd or dvd. I am addicted to them because they are very helpful. Especially when a piece of software insists on reading its DVD and I have a few of such software which I wish to run concurrently and I have only one DVD/CD drive.

Once mounted as a ROM drive, hmm... needless to say any further.

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