Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-18-2008, 10:57 PM
Member
 
Join Date: Oct 2008
Posts: 4
Rep Power: 0
satbobo is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 10-23-2008, 03:38 AM
Eku Eku is offline
Senior Member
 
Join Date: May 2008
Location: Makati, Philippines
Posts: 234
Rep Power: 2
Eku is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 10-23-2008, 04:01 AM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SouthWest Missouri, USA
Posts: 2,229
Rep Power: 4
Norm is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 10-23-2008, 02:00 PM
Member
 
Join Date: Oct 2008
Posts: 4
Rep Power: 0
satbobo is on a distinguished road
Default
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...
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 11-03-2008, 08:41 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,256
Rep Power: 11
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Default
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?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Someone helped you? their helpful post.
Help:Forums FAQ|How To Ask Questions The Smart WayResources:The Java Tutorials|Glossary for Java|NetBeans IDE|Sun DownloadsWeb:WritOnceTips:Is your IDE the best?|Which Application Server?
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 11-05-2008, 08:22 PM
Member
 
Join Date: Oct 2008
Posts: 4
Rep Power: 0
satbobo is on a distinguished road
Default
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.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 11-05-2008, 09:49 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SouthWest Missouri, USA
Posts: 2,229
Rep Power: 4
Norm is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 11-06-2008, 05:24 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,256
Rep Power: 11
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Default
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?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Someone helped you? their helpful post.
Help:Forums FAQ|How To Ask Questions The Smart WayResources:The Java Tutorials|Glossary for Java|NetBeans IDE|Sun DownloadsWeb:WritOnceTips:Is your IDE the best?|Which Application Server?
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 11-06-2008, 02:59 PM
Member
 
Join Date: Oct 2008
Posts: 4
Rep Power: 0
satbobo is on a distinguished road
Default
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
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 11-06-2008, 05:34 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,256
Rep Power: 11
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Someone helped you? their helpful post.
Help:Forums FAQ|How To Ask Questions The Smart WayResources:The Java Tutorials|Glossary for Java|NetBeans IDE|Sun DownloadsWeb:WritOnceTips:Is your IDE the best?|Which Application Server?
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 11-06-2008, 05:43 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SouthWest Missouri, USA
Posts: 2,229
Rep Power: 4
Norm is on a distinguished road
Default
Quote:
will netbeans ...
A simple way to find out is to try it and see what happens.
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 11-21-2008, 07:24 PM
Member
 
Join Date: Nov 2008
Posts: 1
Rep Power: 0
MAntioch is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 03-05-2009, 10:41 AM
Member
 
Join Date: Mar 2009
Posts: 1
Rep Power: 0
avh4 is on a distinguished road
Default
I solved this by adding the following to my build.xml file:

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.
Bookmark Post in Technorati
Reply With Quote
  #14 (permalink)  
Old 08-26-2009, 05:08 AM
Member
 
Join Date: Aug 2009
Posts: 1
Rep Power: 0
jpdelatorre is on a distinguished road
Default Thanks
thanks the last solution work also for me.But i continue wondering why netbeans does not consider it.
thanks again
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Including JAR in applications bugger New To Java 0 01-11-2008 10:36 AM
Creating Sub forms in java netbeans 5.0 java_newbie NetBeans 14 08-06-2007 08:19 PM
Creating JUnit Test Cases using NetBeans JavaForums Java Blogs 0 08-06-2007 06:30 PM
Creating a Session Ejb in NetBeans IDE JavaForums NetBeans 0 07-31-2007 12:13 AM
Creating J2ee Modules In NetBeans IDE JavaForums NetBeans 0 07-31-2007 12:13 AM


All times are GMT +2. The time now is 05:52 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org