How can I specify dependencies in the manifest file and then to include it into my .jar file?

13,077

First of all: you don't seem to compile a class called MainClass and all your .java files seem to be in a package, so I assume that MainClass is just a placeholder and you actually use the correct class name here.

You need to specify a Class-Path header that mentions your external .jar to your manifest.txt and deliver the .jar file together with your jar. You need to do this in addition to specifying the -cp at compile time.

Share:
13,077
Roman
Author by

Roman

Updated on June 04, 2022

Comments

  • Roman
    Roman almost 2 years

    I generated .class files by the following command:

    javac -cp \directoryName\external.jar myPackageDirectory\First.java myPackageDirectory\Second.java
    

    I needed to use -cp during compilation and name of .jar file of an "external" library (external.jar) to be able to use this library from my code.


    Using my .class files I have generated my .jar file in the following way:

    jar cfm app.jar manifest.txt myPackageDirectory\*.class
    

    manifest.txt contains just one line:

    Main-Class: myPackageName.First
    

    My problem is that I am not sure that I will be able to run my .jar file on other computers. I think so because during the compilation I specified the location of the .jar file of the external library. So, my .class files (included into the .jar file will try to find the .jar file of the external library in a specific directory and there is no guaranty that that the .jar file of the external library will be in the same directory as on the my computer.


    I heard that the above problem can be solved by a

    usage of a MANIFEST file that I include in my own jar, and which will list dependency locations

    but I do not understand how it works. I do need to specify location of the "external.jar" at the compilation stage (otherwise the compiler complains).

  • Roman
    Roman about 14 years
    Right, I do not compile MainClass (it is just a place holder, I modified that in my post). About the second part, how do I include my manifest file into my .jar file?
  • Joachim Sauer
    Joachim Sauer about 14 years
    You already include a manifest file (your input is called manifest.txt). You just need to modify its input. The rest should be explained in the tutorial I link to.
  • Roman
    Roman about 14 years
    I mean, the way I did it automatically include my manifest file into my jar, right?
  • Joachim Sauer
    Joachim Sauer about 14 years
    Yes, the m argument together with manifest.txt tells jar to use the content of manifest.txt an write it to the new .jar files META-INF/MANIFEST.MF.