• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is there a way in netbeans to include external libraries in project JAR file(s)?

 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

is it possible to tell Netbeans to include external libraries for a project as jar files in the project main jar file and adjust the manifest file so that you can run the application directly from the jar?

I couldn't find a way to do this even with a simple test project. In my "dist" directory for the project all external jars are in an extra "lib" directory which works fine but I don't want to pack and unpack the whole directory in another jar together with the libraries in order to run it on another machine.

Does anyone of you know a way to tweak the project settings so that jars are included? Or is there a reason why one wouldn't do that? I think distribution of an application would be easier for unexperienced Java users this way.

Any ideas? Thanks in advance!

Marco
 
Bjorn Hemmelig
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does someone know?
 
Felipe Reis
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm having the exact same problem.

Could you figure that out Marco?

Does anyone knows how to do that?
 
Campbell Ritchie
Sheriff
Pie
Posts: 50168
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is bound to be an easy way to do it. There is an easy way to find out: to use the documentation on the NetBeans website . . . which gives you this as a starting point.
 
Matthias Antioch
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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. Also, If you are writing for a specific environment, say, a servlet container or even just use a specific library a lot, it will pay off space and bandwidth wise to keep libraries separate, in the classpath of the container, and as a separate download for end users. Then you don't need to include them in future releases, and can update all apps using the library simultaneously.

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.
[ November 20, 2008: Message edited by: Matthias Antioch ]
 
Vasek Vopenka
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.
Sorry for opening a dead thread, but it's already indexed by Google, so there is no reason to start a new one.
I've been solving this problem recently and as you can quess, it is quite anoying to rezip your distribution file all the time you find a small mistake in final distribution (I know you shouldn't create final distribution with mistakes ). So a created a simple python script which does it for you:


You just have to place it in base folder of your project and run it when you create a distribution. It expects standart output from NetBeans. It creates a jar file in your project folder, which contains all included libraries. I've tested it in Linux, but it should work in Windows as well.

I hope it will help.
 
Henrik Solgaard
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is not very hard to tell NetBeans to do it. The following works for me.

Put something like this at the bottom of build.xml, just before "</project>":


You have to change reference.mylib.jar above to match the name of your library. If you have added your libraries to the project with the "Add Project" button in the "Libraries" section of the "Project Properties" dialog, your project.properties file will contain lines like the following for each included library project:

The part before "=" is the string you should use in the src attribute of the zipfileset element.

If you have more than one library, add a zipfileset element for each of them.
 
Adam Outler
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
^^ Yep. Just wanted to add this... it works. I had to modify that to say "dist/lib/" instead of just lib

my code:


1. change your filename


2. Add a new line for each jar file.

3. change the Main-Class to match your project name


Done. Thanks!
 
Marco Cognolato
Greenhorn
Posts: 3
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, an old but good DOS script can do the trick.
Build the java project with Netbeans as usual, then create a directory named 'NewDist' inside NetBeans' project directory and create a file named JarMaker.bat.
Here is the code:



Just double click on it and wait...
At the end of the process a file named YourProjectName.jar will be created inside 'NewDist' directory.

Enjoy.

(P.S.: Remember to include the %JAVA_HOME%/bin in your environment path)
 
Adam Outler
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a horrible way to do it... Why use an external program when you can just build automatically?


I've been adding this into my projects... it takes some tweaking for each of my project build.xmls.. Add this right above the last line "</project>"

This does the following:
1. adds libraries into the output automatically
2. adds the option to use Application.buildnumber anywhere in your project, which tracks each build sequentially
3. adds the option to use Application.revision which tracks Subversion Revision number
4. names the output file a custom name with a SVN revision.



Using this, or something like it tailored to your app, you can really automate your releases.
 
Marco Cognolato
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe it's horrible, but it is valid for all projects without modify any file (including itself) and automatically adds all jar you have included in the project...
 
Adam Outler
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that's only valid for windows. The build scripts work on all platforms.
 
Marco Cognolato
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I usually work on several project in same environment, not with several environment with same project.
But anyway it's very easy to write a csh version on that script and I don't want to force anyone to use that method.
I think it's faster to copy and paste a ready script than modify each single xml build one, just don't use it if you don't like it.
 
Yohan Weerasinghe
Ranch Hand
Posts: 507
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marco Cognolato wrote:Well, an old but good DOS script can do the trick.
Build the java project with Netbeans as usual, then create a directory named 'NewDist' inside NetBeans' project directory and create a file named JarMaker.bat.
Here is the code:



Just double click on it and wait...
At the end of the process a file named YourProjectName.jar will be created inside 'NewDist' directory.

Enjoy.

(P.S.: Remember to include the %JAVA_HOME%/bin in your environment path)



I have tested this and this is very useful. Thanks a lot
 
Vinicius Souza
Ranch Hand
Posts: 70
1
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marco Cognolato wrote:Well, an old but good DOS script can do the trick.
Build the java project with Netbeans as usual, then create a directory named 'NewDist' inside NetBeans' project directory and create a file named JarMaker.bat.
Here is the code:



Just double click on it and wait...
At the end of the process a file named YourProjectName.jar will be created inside 'NewDist' directory.

Enjoy.

(P.S.: Remember to include the %JAVA_HOME%/bin in your environment path)

The easest way to do that, thanks a lot! I really dont know why Netbeans missess this feature.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic