Netbeans Export to Jar, include all library files
Asked Answered
Q

4

12

What I really need is an equivalent of this from Eclipse with Netbeans:

enter image description here

I want to Extract required libraries into generated JAR so I can distribute it without the need of including the lib folder everywhere it goes.

Here's the original question.

I'm trying to build a jar out of Netbeans that will package all the library files inside it. I've heard it's possible with Eclipse by choosing Export. With Netbeans, if I "Clean and Build" it will create an executable jar in the dist folder, but all the libraries it depends on are in a folder called lib. I need it all to be in one jar file. I'm thinking it has to do with the build.xml file. Here's what mine says. What am I getting wrong here?

Thanks for the help.

<?xml version="1.0" encoding="UTF-8"?>
<project name="BONotifier" default="default" basedir=".">
  <description>Builds, tests, and runs the project BONotifier.</description>
  <import file="nbproject/build-impl.xml"/>
  <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="controller.MainController"/>
      </manifest>
    </jar>
  </target>
</project>

Update: The code above is in build.xml. The following is in build-impl.xml. I'm guessing this means that build.xml is run during build-impl.xml.

<target name="-pre-init">
    <!-- Empty placeholder for easier customization. -->
    <!-- You can override this target in the ../build.xml file. -->
</target>

Note I got this from this forum.

Quinquepartite answered 8/5, 2012 at 19:20 Comment(1)
Try to add this jar task to "nbproject/build-impl.xml" where jar creation task is.Ayakoayala
C
16

I prefer this solution since there is nothing to download, all based on Ant build (~20 lines). That way there is no additional dependency to bother with. If you have a couple of minutes, you can learn about Ant too.

It seems like the link to the original Article is broken (thanks Oracle). Here is another link.

The original article is also available from Oracle link.

Cheater answered 8/5, 2012 at 21:50 Comment(2)
@Cheater This link is broken now. Any chance of fixing it or including the relevant information directly in your answer?Physiologist
@Physiologist I've updated the link, feel free to update my answer if you can paraphrase the solution.Cheater
E
5
  1. Open the generated jar with 7zip (or others tools)
  2. Add the jar library into the folder
  3. Edit the manifest to set the class path
Elvinelvina answered 3/10, 2013 at 13:20 Comment(0)
C
1

Okay, so this is my solution. I too had the problem my programmme compiling and running on Netbeans but it failing when I tried Java -jar MyJarFile.jar Now, I don't fully understand Maven and I think this why was having trouble getting Netbeans 8.0.2 to include my jar file in a library to put them into a jar file. I was thinking about how I used to use jar files with no Maven in Eclipse.

It's Maven that compiles all the dependanices and plugins. Not Netbeans. (If you can get Netbeans to do this please tell us how (^.^)v )

[Solved - for Linux] by opening a terminal.

Then

cd /MyRootDirectoryForMyProject

Next

mvn org.apache.maven.plugins:maven-compiler-plugin:compile

Next

mvn install

This will create jar file in the target directory.

MyJarFile-1.0-jar-with-dependencies.jar

Now

cd target

(You may need to run: chmod +x MyJarFile-1.0-jar-with-dependencies.jar) And finally

java -jar MyJarFile-1.0-jar-with-dependencies.jar

Please see

https://cwiki.apache.org/confluence/display/MAVEN/LifecyclePhaseNotFoundException

I'll post this solution in on a couple of other pages with a similar problem. Hopefully I can save somebody from a week of frustration.

Charis answered 13/2, 2015 at 0:43 Comment(0)
M
-1

There's no built in way to do this. You can, however, use external tools to archive this like OneJar:

http://one-jar.sourceforge.net/

Edit: Apparently this is even the way the Netbeans team expects you to do it: http://wiki.netbeans.org/PackagingADistributableJavaApp

Morphinism answered 8/5, 2012 at 19:49 Comment(1)
I see your update. I still recommend you to go for OneJar. It's much more versatile, plus, see my edit :9Morphinism

© 2022 - 2024 — McMap. All rights reserved.