
10-18-2008, 10:57 PM
|
|
Member
|
|
Join Date: Oct 2008
Posts: 4
Rep Power: 0
|
|
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
|
|

10-23-2008, 03:38 AM
|
|
Senior Member
|
|
Join Date: May 2008
Location: Makati, Philippines
Posts: 234
Rep Power: 2
|
|
|
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.
|
|

10-23-2008, 04:01 AM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: SouthWest Missouri, USA
Posts: 2,229
Rep Power: 4
|
|
|
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.
|
|

10-23-2008, 02:00 PM
|
|
Member
|
|
Join Date: Oct 2008
Posts: 4
Rep Power: 0
|
|
Originally Posted by Norm
|
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...
|
|

11-03-2008, 08:41 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,256
Rep Power: 11
|
|
Originally Posted by satbobo
|
|
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?
|
|

11-05-2008, 08:22 PM
|
|
Member
|
|
Join Date: Oct 2008
Posts: 4
Rep Power: 0
|
|
Originally Posted by Eranga
|
|
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.
|
|

11-05-2008, 09:49 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: SouthWest Missouri, USA
Posts: 2,229
Rep Power: 4
|
|
|
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.
|
|

11-06-2008, 05:24 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,256
Rep Power: 11
|
|
Originally Posted by satbobo
|
|
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?
|
|

11-06-2008, 02:59 PM
|
|
Member
|
|
Join Date: Oct 2008
Posts: 4
Rep Power: 0
|
|
Originally Posted by Eranga
|
|
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
|
|

11-06-2008, 05:34 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,256
Rep Power: 11
|
|
|
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-06-2008, 05:43 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: SouthWest Missouri, USA
Posts: 2,229
Rep Power: 4
|
|
A simple way to find out is to try it and see what happens.
|
|

11-21-2008, 07:24 PM
|
|
Member
|
|
Join Date: Nov 2008
Posts: 1
Rep Power: 0
|
|
(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.
|
|

03-05-2009, 10:41 AM
|
|
Member
|
|
Join Date: Mar 2009
Posts: 1
Rep Power: 0
|
|
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.
|
|

08-26-2009, 05:08 AM
|
|
Member
|
|
Join Date: Aug 2009
Posts: 1
Rep Power: 0
|
|
Thanks
thanks the last solution work also for me.But i continue wondering why netbeans does not consider it.
thanks again
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT +2. The time now is 05:52 PM.
|
|