10 Replies Latest reply: Jan 29, 2007 12:59 AM by 807607 RSS

    How to include externel library in JAR file?

    807607
      Hi all =)

      I have a program that uses an external library (in a JAR file) I would like to compile my program as a JAR and have it include the external library that it needs to run, so that the external library would not need to be on the computer running my program. How can I do this?

      Thanks =)
      Koneko349

      Message was edited by:
      Koneko349
        • 1. Re: How to include externel library in JAR file?
          796254
          Can't be done. The best you can do is deploy the 3rd party JARs along with your executable JAR and add them to the manifest Class-Path attribute. But they can't go inside your executable JAR. The class loader won't look for them there.

          %
          • 2. Re: How to include externel library in JAR file?
            807607
            Ahhh ic, so lets say that I want to put my executable JAR and the external library JAR in the same folder and then run my JAR, how would i set that up? I have been using eclipse to export my program to a JAR and i don't think its doing it right =( Can you give me instructions on how to make the JAR or do you have a link?
            • 3. Re: How to include externel library in JAR file?
              796254
              Ahhh ic, so lets say that I want to put my executable
              JAR and the external library JAR in the same folder
              and then run my JAR, how would i set that up?
              Not sure what you're asking. You've got 3rd party JARs and your executable JAR in the same directory. Very good. What are you asking me?
              I have
              been using eclipse to export my program to a JAR and
              i don't think its doing it right =(
              It's only doing what you're telling it to do. What does "not right" look like?

              Does your executable JAR have a manifest? Does it spell out the main class and classpath? If not, make it do so.

              >Can you give me
              instructions on how to make the JAR or do you have a
              link?
              The instructions on how to use the jar tool are in the Java tool docs:

              http://java.sun.com/j2se/1.4.2/docs/tooldocs/windows/jar.html

              %
              • 4. Re: How to include externel library in JAR file?
                807607
                Great =) one last thing, will it automatically put the correct entry into the manifest and .classpath? Also how do I tell my JAR to look for the external library in the same directory it was launched from?
                • 5. Re: How to include externel library in JAR file?
                  796254
                  Great =) one last thing, will it automatically put
                  the correct entry into the manifest and .classpath?
                  "It" being Eclipse? I doubt it. How will it know?

                  Don't depend on anything being done without your knowledge. I think if you create a META-INF directory and put a proper manifest.mf file in it, Eclipse can be told to bundle it up with your JAR. You have to create it.
                  Also how do I tell my JAR to look for the external
                  library in the same directory it was launched from?
                  You can tell it to look in whatever path you wish when you specify the entries to Class-Path.

                  %
                  • 6. Re: How to include externel library in JAR file?
                    807607
                    Sorry I'm a noob when it comes to making a java program run outside eclipse =P Any good resources you can give me to help with correct classpath entires and manifest file.
                    • 7. Re: How to include externel library in JAR file?
                      796254
                      The one I gave you is the best. Read the java tool docs. Or Google for "executable jar tutorial" and pick one.

                      %
                      • 8. Re: How to include externel library in JAR file?
                        807607
                        mk =) thanks
                        • 9. Re: How to include externel library in JAR file?
                          807607
                          Ok I was able to make my JAR and launch my application correctly. However whenever I click a button that has a method using my external library I get the following error:

                          C:\Documents and Settings\Koneko>java -jar D:\Mangment.jar

                          Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: org/apache/poi/poifs/filesystem/POIFSFileSystem at managment_system.ManagmentGUI.getNextStat(ManagmentGUI.java:569) at managment_system.ManagmentGUI$1.actionPerformed(ManagmentGUI.java:195)


                          I do not get this error when I run and use my program from within the IDE.

                          The JAR that has my external library (and has the class for POIFileSystem) is also on my D drive and i referenced it correctly (I think) In my classpath:
                           <?xml version="1.0" encoding="UTF-8"?>
                          <classpath>
                               <classpathentry kind="src" path=""/>
                               <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
                               <classpathentry kind="lib" path="D:/poi.jar"/>
                               <classpathentry kind="output" path=""/>
                          </classpath>
                          I would really like to get this working and I'm very confused XP
                          • 10. Re: How to include externel library in JAR file?
                            807607
                            If there is any other code or information that would be helpful in resolving this please let me know and I will post it. I would really like to get my program running on its own.