1

I have a java gui project which I had just finished working on. I was using netbeans to develop this and now I want to pack the entire project into a jar file. Netbeans seems to do this on it's own, but it does not include all dependencies along with it. i.e. If I have some images I have added to the classpath of the project, these images are not loaded when I execute the jar. It shows a bunch of errors in the console when I run it due to it not being able to load the images/files associated with the project.

Is there an option in netbeans to include EVERYTHING and have the program run as if it were running from the IDE?

smac89
  • 39,374
  • 15
  • 132
  • 179
  • 1
    See [this thread](http://stackoverflow.com/questions/10834589/how-can-i-include-external-jar-on-my-netbeans-project) for a similar question. – mattias Nov 07 '14 at 22:09
  • How did you include the resources on the class path? – MadProgrammer Nov 07 '14 at 22:28
  • @MadProgrammer I right-clicked project_name -> properties -> Libraries -> Run ->Add jar/folder and I nagvigated to the folder and added it. The folder is actually already located in my `src` folder. – smac89 Nov 07 '14 at 22:42
  • Netbeans won't actually copy libraries into a Jar file. You have to distribute several Jar's (your Jar plus any libraries) in order to get the "whole" project. [Look at OneJar](http://one-jar.sourceforge.net/) though, it might help you. – markspace Nov 07 '14 at 22:49
  • @Smac89 Netbeans will automatically include all the resources contained within the `src` directory. You could either try adding those resources to your projects `src` directory or including the directory they in as an additional "source" resource... – MadProgrammer Nov 07 '14 at 23:10

3 Answers3

0

Ok so I fixed this, not with Netbeans but with eclipse. I imported the project into eclipse and after adding all dependencies and verifying it is working in eclipse, I click file ->export... ->java-drop-down -> runnable jar ->choose file name, and from launch configuration select your main program and leave every other option as selected then press finish button and it should work.

Worked for me! This is not to say that Eclipse is better than Netbeans, just that it has a better option for generating jar files

smac89
  • 39,374
  • 15
  • 132
  • 179
0

You can use OneJar project to do that. or if it's a Netbeans Ant-based project, you could use a script, so Netbeans will do it for you every time you build.

Ant Zip Task

Community
  • 1
  • 1
Gabriel Espinel
  • 358
  • 2
  • 9
  • OneJar can be difficult if you're using custom class loaders, the problem I have with "single jar" solutions is if your jars contain same named resources (like manifests or configuration files) which need to be loaded individually...just saying – MadProgrammer Nov 07 '14 at 23:09
0

Netbeans will include all non-java files contained within the src directory. You can copy all your resources to this directory and they will be bundled into you jar file.

Additionally, you could include the directory the resources are in as an additional source resource. Right click the project node and select "Properties". Select the "Sources" entry and click "Add Folder", browse and include the top level folder which contains the resources.

enter image description here

If the resources are common resources, shared amongst other projects, I tend to create a library project and place those resources there, including them as a "Project Library" under the "Libraries" of the project.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Yes I saw this option, but as I said before, the folder which is not being included is actually in the src folder. And it is also on the classpath, but Netbeans is still not including it somehow in the exported jar. Anyways I already fixed this with eclipse but I still have a feeling Netbeans should have worked – smac89 Nov 07 '14 at 23:53
  • @Smac89 Well, these are all the approaches I use and I've never had issues – MadProgrammer Nov 08 '14 at 03:21
  • @Smac89 Silly question, why are you double adding the `src` directory? `src` is already in the classpath by default... – MadProgrammer Nov 08 '14 at 03:23
  • I didn't double add it, I am saying that I added the folder which is not being included, to the classpath. This is so that I can use resources from it by simply doing `getClass().getResource("/some-resource")` without having to specify a long path name – smac89 Nov 08 '14 at 04:07
  • @Smac89 Ah, okay, my misunderstanding, sounded like you were adding `src` twice. Did you adding it as "source" instead of via the "libraries"? – MadProgrammer Nov 08 '14 at 04:20