Results 1 to 6 of 6
  1. #1
    ichwar is offline Member
    Join Date
    Jul 2009
    Location
    Rolesville, North Carolina
    Posts
    16
    Rep Power
    0

    Default Packaging all files into a single executable .jar

    Ok, my problem is simply this. When I write even a simple program, netbeans gives me an executable .jar file along with another folder containing a couple more .jar files. Is there any way for me to package all these files/folders into on .jar file that will still run just by being double clicked?

    Thanks.
    if (ichwar == offline) {
    System.out.println("ichwar is busy");}

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    I hope those addition jar files are layouts, in lib folder is it? Actually in that what i'm doing is build another jar file including that lib folder in my final jar. Most of the time I use the same folder structure in distribution.

  3. #3
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    Yes, there is a way to do it. I used to add a couple lines to netbean's build script which did this automatically, sadly it no longer works and I have yet to find out exactly why. Prior to netbeans 6.5.x, you could add the following:

    Java Code:
    <target name="-post-jar">
    	       <jar update="true" destfile="${dist.jar}">
    	        	<zipfileset src="${libs.swing-layout.classpath}"/>
    	        </jar>
    </target>
    to Netbeans/harness/build.xml

    When this started failing, I went back to a bash shell script I wrote ages ago, which I have included below. Feel free to modify it (you'll have to unless you keep all your stuff in exactly the same folders I do).

    Java Code:
    #!/bin/sh
    
    cd ~/Programming/NetBeansProjects/
    echo "Enter Project name"
    read PROJ
    echo "Enter Package Name"
    read PKG
    echo "Enter Main class name"
    read MAIN
    
    cd $PROJ/dist
    
    rm -r /tmp/rejar/
    mkdir /tmp/rejar/
    cp -r * /tmp/rejar/
    cd /tmp/rejar/
    mv lib/swing-layout-1.0.3.jar lib/swing.zip
    unzip lib/swing.zip 
    rm -r lib
    rm -r META-INF
    mv $PROJ".jar" wg.zip
    unzip wg.zip
    echo "Main-Class: "$PKG"."$MAIN > META-INF/MANIFEST.MF
    rm README.TXT
    rm wg.zip
    jar -cmf META-INF/MANIFEST.MF $PROJ".jar" *
    mv $PROJ".jar" ~/Desktop/
    rm -r /tmp/rejar
    echo "Re-Jar'ing is complete, new .jar is on Desktop."
    You can however do the steps manually -- all the jar tools are included with the JDK, you can type them into the command line one by one.
    Last edited by quad64bit; 07-26-2009 at 02:49 AM.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    I agreed, as far as I know that option didn't workout in NetBeans 6.5. So what we can do is keep the same structure in distribution or build a single jar file manually.

  5. #5
    joshuaborn is offline Member
    Join Date
    Jan 2010
    Posts
    1
    Rep Power
    0

    Default Here are some Ant targets to do this

    I stumbled on this thread when googling the same problem. Here are the custom Ant targets I use for this:

    Java Code:
        <target name="-unjar-and-copy-lib-jars">
            <unjar dest="${build.classes.dir}">
                <fileset dir="lib">
                    <include name="**/*.jar"/>
                </fileset>
                <patternset>
                    <exclude name="META-INF/**"/>
                    <exclude name="/*"/>
                </patternset>
            </unjar>
        </target>
    
        <target depends="init,compile,-pre-pre-jar,-pre-jar,-unjar-and-copy-lib-jars" name="fat-jar">
            <property location="${build.classes.dir}" name="build.classes.dir.resolved"/>
            <jar destfile="${dist.jar}">
                <fileset dir="${build.classes.dir}"/>
                <manifest>
                    <attribute name="Main-Class" value="${main.class}"/>
                </manifest>
            </jar>
            <echo>To run this application from the command line without Ant, try:</echo>
            <property location="${dist.jar}" name="dist.jar.resolved"/>
            <echo>java -jar "${dist.jar.resolved}"</echo>
        </target>
    
        <target depends="clean,fat-jar" name="clean-and-fat-jar"/>
    For more info, see my blog post on the matter.

  6. #6
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    Thanks for the post! This is a problem I had ages ago too - hence my script. I've found that in the current versions (java 1.6 and NetBeans 6.8) the separate swing layout libraries are no longer included in the build and that the layout that was used (was it groupLayout?) is now included in the JDK for 1.6. However, unless I rebuild my old gui projects from scratch, netbeans continues to use the old separate swing layout library. Thanks for the post!

    I was really irritated when my modifications to the ant build script just stopped working after 6.5 and honestly don't know anything about writing ant build scripts from scratch (or modifying them to any great degree!).

Similar Threads

  1. About Executable JAR files
    By paluee in forum New To Java
    Replies: 4
    Last Post: 03-31-2009, 07:03 PM
  2. Merged TIF file size is more larger than the sum of single TIF files
    By subrahmanyam.adapa in forum Advanced Java
    Replies: 3
    Last Post: 01-26-2009, 01:19 PM
  3. executable files
    By akinpam in forum Advanced Java
    Replies: 10
    Last Post: 01-06-2009, 04:01 AM
  4. Packaging and accessing data files
    By todd in forum Advanced Java
    Replies: 1
    Last Post: 08-01-2007, 12:27 AM
  5. Replies: 1
    Last Post: 06-06-2007, 04:20 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •