Results 1 to 14 of 14
  1. #1
    satbobo is offline Member
    Join Date
    Oct 2008
    Posts
    4
    Rep Power
    0

    Default netbeans: creating a jar including jar libraries

    Hi everyone,

    I am trying to create a jar of my netbeans project which includes some other jars as libraries.
    The problem is that it compiles ok and creates the jar but unfortunately it does not include in the project .jar the libraries needed for the execution.
    Therefore when I run the program it complains with a java.lang.NoClassDefFoundError.

    I should also say that my program does not have a main method since it is a package that will be loaded dynamically by another program. For this reason Netbeans does not create the /lib folder in the /dist folder, but even if it did I would have liked to avoid to copy the /lib folder over.

    Is it any way to tell netbeans to include the libraries within the .jar file?

    Anyone can advise on how I can solve this?
    Cheers

  2. #2
    Eku
    Eku is offline Senior Member
    Join Date
    May 2008
    Location
    Makati, Philippines
    Posts
    234
    Rep Power
    6

    Default

    i hope this works. Try to add the libraries into the JVM.
    Mind only knows what lies near the heart, it alone sees the depth of the soul.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default

    If you are able to get jar files into the project jar file, what program are you going to use to read those internal jar files?
    I don't think the java command knows how to read from jar files contained in a jar file.

  4. #4
    satbobo is offline Member
    Join Date
    Oct 2008
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    If you are able to get jar files into the project jar file, what program are you going to use to read those internal jar files?
    I don't think the java command knows how to read from jar files contained in a jar file.

    yeah I think that's the point... for the time being I solved by adding the compiled classes to the path of my source project. These classes are incorporated into the jar file and everything seems to work. This creates a big jar file though...

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by satbobo View Post
    Is it any way to tell netbeans to include the libraries within the .jar file?
    I've not clear what you mean here? You saying that *.jar file contain another jar file?

  6. #6
    satbobo is offline Member
    Join Date
    Oct 2008
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    I've not clear what you mean here? You saying that *.jar file contain another jar file?

    I meant using a jar as a library. This will work at compilation time because netbeans finds the jar files but when it will create the jar file of the compiled project it will not include the jar libraries and this will throw a class not found exception at execution time.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default

    If you reference classes contained in jar files at compile time, you will have to have them on the classpath at execution time. Which means you will have to copy them to the user's PC.
    One way to have them on the classpath is to use the Class-path: statement in the manifest file.

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by satbobo View Post
    I meant using a jar as a library. This will work at compilation time because netbeans finds the jar files but when it will create the jar file of the compiled project it will not include the jar libraries and this will throw a class not found exception at execution time.
    How did you attach those external jar file in your project? After compiling your project is that external jar files are contain in lib folder?

  9. #9
    satbobo is offline Member
    Join Date
    Oct 2008
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    How did you attach those external jar file in your project? After compiling your project is that external jar files are contain in lib folder?
    At first I didn't and that's why at execution time the jvm was complaining that it couldn't find classes.
    But I wanted to avoid distributing libraries separately. I just would have liked to have a single jar file with everything needed inside of it.

    if I do this: "One way to have them on the classpath is to use the Class-path: statement in the manifest file.

    will I still need to put separately the files in the other machine or will netbeans add my libraries in the project jar?

    Thanks guys

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    If you use them as a jar file then all are added to the lib folder and attach to your main application. If you just use them as separate classes all additional class files are added to the build folder which is all class files are contain. In that case all class files are added into a single jar file, in dist folder, after build the application.

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default

    will netbeans ...
    A simple way to find out is to try it and see what happens.

  12. #12
    MAntioch is offline Member
    Join Date
    Nov 2008
    Posts
    1
    Rep Power
    0

    Default

    (copied from my javaranch post. I'd link to it, but I don't have enough posts here yet)

    Im assuming you want to create a single file, libraries included, from which your application can be run. There are two ways to go about this:

    It is possible to have ant merge your libraries into your final distribution via the zipfileset directive in the build.xml file. Unfortunately, you have to override the appropriate sections of netbeans' build-impl.xml file, and due to the ridiculously over-complicated nature of everything netbeans auto-generates, this is often an exercise in irritation and futility.

    Except for a select few circumstances, what works best for me is to simply merge the files manually. A .jar is basically a .zip with organized contents, and you can open them in almost any .zip capable archive program (I just use gnome's standard archiver, File Roller, and it works great). Backup your jar file and open it in the archiver of your choice, and do the same for each library jar in the library directory. Drag and drop the working folders (IE, everything EXCEPT the META-INF Directory) from each library into your jar's root path (alongside your META-INF and your app's root package). Now drag the META-INF/MANIFEST.MF file from your jar to your Desktop or any other folder. Open it, and erase the Class-Path and X-COMMENT lines. Don't forget to leave a blank newline at the end of the file! Save the new manifest file and drag it back to your jar's META-INF directory, overwriting the old one. Test the jar.

    It sounds really complicated, but its honestly little more than drag and drop, and usually takes under 30 seconds. Note that every time you build in Netbeans you will need to do this over again, but it usually doesn't matter, as only the final, ready-for-release version of your app needs to standalone. While its still under development, you can usually work/test with it with external libraries.

    You'll have to check the legal status of all third party libraries yourself before merging/distributing them. Many are license encumbered, or have special distribution requirements.

  13. #13
    avh4 is offline Member
    Join Date
    Mar 2009
    Posts
    1
    Rep Power
    0

    Default

    I solved this by adding the following to my build.xml file:

    Java Code:
        <target name="-post-jar">
            <jar jarfile="dist/Combined-dist.jar">
                <zipfileset src="${dist.jar}" excludes="META-INF/*" />
                <zipfileset src="lib/commons-io-1.4.jar" excludes="META-INF/*" />
                <manifest>
                    <attribute name="Main-Class" value="com.example.mypackage.Main"/>
                </manifest>
            </jar>
        </target>
    This creates a jar file (Combined-dist.jar) which is the combination of the dist jar and the specified library jars (in this case, commons-io-1.4.jar). You have to be sure to specify your Main Class package for the new jar file or it won't run when you try to open it.

  14. #14
    jpdelatorre is offline Member
    Join Date
    Aug 2009
    Posts
    1
    Rep Power
    0

    Default Thanks

    thanks the last solution work also for me.But i continue wondering why netbeans does not consider it.
    thanks again

Similar Threads

  1. Including JAR in applications
    By bugger in forum New To Java
    Replies: 0
    Last Post: 01-11-2008, 09:36 AM
  2. Creating Sub forms in java netbeans 5.0
    By java_newbie in forum NetBeans
    Replies: 14
    Last Post: 08-06-2007, 07:19 PM
  3. Creating a Session Ejb in NetBeans IDE
    By JavaForums in forum NetBeans
    Replies: 0
    Last Post: 07-30-2007, 11:13 PM
  4. Creating J2ee Modules In NetBeans IDE
    By JavaForums in forum NetBeans
    Replies: 0
    Last Post: 07-30-2007, 11:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •