简体   繁体   中英

Creating java library containing 3rd party (jar) libraries

I need to produce a jar file that will be used by others as a library (lets call it 'myLib.jar'), thus the jar does not need to be executable. The library depends on some other 3rd-party jars but i will not expect from the ones who use 'myLib.jar' to include them in his/hers Buildpath.

While searching, i found OneJar and followed the 2 steps at the top of the page.

1. Download one-jar-appgen-0.97.jar
   Generate application, build, and run it.
   $ java -jar one-jar-appgen-0.97.jar

2. Enter project path (project name is last segment): c:/tmp/test-one-jar
   Enter java package name: com.example.onejar
   $ cd c:/tmp/test-one-jar 
   $ ant 
   $ cd build 
   $ java -jar test-one-jar.jar
   test_one_jar main entry point, args=[]
   test_one_jar main is running
   test_one_jar OK.
   Add source code to the src directory, library jars to the lib directory, and rebuild.

Unfortunately, this did not worked for me since when i tried to include 'myLib.jar' in a new project, i was not able to use the expected classes.


So...

Q1 : Is OneJar intended only for executable jars? If not how could i achieve what i have described above.

Q2 : If OneJar is intended only for executables, is there another way to produce the 'myLib.jar' library?


PS: If there is a need to add more information please let me know, in order to edit my quetsion.

All the comments are suggesting to avoid packing the 3rd party libraries into a single jar. There are several good reasons for this:

  1. Packing all 3rd party libraries to one jar, permits the ones who use 'myLib.jar' to have up-to-date versions of the 3rd party libraries.
  2. It is forbitten for any other library to use the same 3rd party libraries which are packed in 'myLib.jar' because this will cause classpath conflicts.
  3. You have to be very careful with the licenses. Every 3rd party library will be probably bounded by a specific license. Many licenses are incompatible and cannot be used together.

So, what did i eventually do?

As @rmlan assured me, there is nothing wrong about notifing to the users of the library to add another library to their classpath. Keeping this in mind, i've created the following folder structure (which is actually what many others do) and zipped it in one compressed file:

myLib
-----bin
----------myLib.jar
-----documentation (mentions 'myLib' dependencies among other stuff)
-----lib (contains the 3rd party libraries)
----------thirdPartyLibrary.jar
----------anotherThirdPartyLibrary.jar
-----source (if it is an open source project)

Finally, in my case i could not use maven or another alternative, but if i could this would make the whole procedure much easier.


EDIT: To create 'myLib.jar' I've used eclipse

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