Results 1 to 14 of 14
- 10-18-2008, 09:57 PM #1
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, 02:38 AM #2
Senior Member
- Join Date
- May 2008
- Location
- Makati, Philippines
- Posts
- 234
- Rep Power
- 6
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, 03:01 AM #3
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, 01:00 PM #4
Member
- Join Date
- Oct 2008
- Posts
- 4
- Rep Power
- 0
- 11-03-2008, 07:41 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 11-05-2008, 07:22 PM #6
Member
- Join Date
- Oct 2008
- Posts
- 4
- Rep Power
- 0
- 11-05-2008, 08:49 PM #7
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, 04:24 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 11-06-2008, 01:59 PM #9
Member
- Join Date
- Oct 2008
- Posts
- 4
- Rep Power
- 0
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, 04:34 PM #10
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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, 04:43 PM #11
A simple way to find out is to try it and see what happens.will netbeans ...
- 11-21-2008, 06:24 PM #12
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, 09:41 AM #13
Member
- Join Date
- Mar 2009
- Posts
- 1
- Rep Power
- 0
I solved this by adding the following to my build.xml file:
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.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>
- 08-26-2009, 04:08 AM #14
Member
- Join Date
- Aug 2009
- Posts
- 1
- Rep Power
- 0
Similar Threads
-
Including JAR in applications
By bugger in forum New To JavaReplies: 0Last Post: 01-11-2008, 09:36 AM -
Creating Sub forms in java netbeans 5.0
By java_newbie in forum NetBeansReplies: 14Last Post: 08-06-2007, 07:19 PM -
Creating a Session Ejb in NetBeans IDE
By JavaForums in forum NetBeansReplies: 0Last Post: 07-30-2007, 11:13 PM -
Creating J2ee Modules In NetBeans IDE
By JavaForums in forum NetBeansReplies: 0Last Post: 07-30-2007, 11:13 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks