Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Making Executable Jar Files.

  1. #1
    Member
    Join Date
    Jan 2012
    Posts
    107
    My Mood
    Grumpy
    Thanks
    6
    Thanked 2 Times in 1 Post

    Post Making Executable Jar Files.

    Index:

    1. PATH & CLASSPATH Environment Variables
    2. Making Code
    3. Compiling Your Code
    4. The Set Up
    5. Making A Jar File









    1. First You Need To Set You're PATH & CLASSPATH In your Environment Variables.

    Environment Variables.jpg

    Notice: You Version Of Java jdk Might Not Be The Same, So Here's How To Find It:

    Start -> My Computer -> Local Disk -> Program Files -> Java

    You Should See A Folder Like jdk#.#.#_## Or Something In That Nature
    This is you version of jdk that needed to be in the PATH & CLASSPATH In your Environment Variables

    Example(s):

    User Variables For [user]
    PATH C:\Program Files\Java\jdk#.#.#_##\bin

    System Variables
    CLASSPATH C:\Program Files\Java\jdk#.#.#_##\bin



    Once you sent that your ready to go.










    2. Making Code:

    NOTICE: MAKE A NEW FOLDER ON YOUR DESKTOP TO CONTAIN EVERYTHING
    NOTICE: MY FOLDERS NAME IS "ExampleJar"

    This Is For A Jar Using 2 Classes:

    ExampleClass.java
    import javax.swing.JFrame;
     
    public class ExampleClass{
    	public static void main(String[] args){
     
    		ExampleClass2 JFrameclass = new ExampleClass2();
    		JFrameclass.setSize(200,50);
    		JFrameclass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		JFrameclass.setVisible(true);
    	}
    }

    ExampleClass2.java
    import javax.swing.JFrame;
    import javax.swing.JLabel;
     
    public class ExampleClass2 extends JFrame{
     
    	private JLabel ExampleLabel;
    		public ExampleClass2(){
     
    			super("Example");
     
    			ExampleLabel = new JLabel("Example",10);
    			add(ExampleLabel);
     
    	}
     
    }

    Save This As .Java Files Using Notepad


    MAIN-INF

    So now you need to tell your jar file which is the main class, This is called MANIFEST.
    For this tutorial The main class ExampleClass.

    Main-Class: ExampleClass
    Save this a manifest.txt
    NOTICE: THIS IS SUPER IMPORTANT:
    AFTER TYPING "Main-Class: ExampleClass" PRESS ENTER OR YOU WILL GET A ERROR!













    3. Compiling Your Code

    Short & Sweet:

    Simple Batch Compiler: Type:
    @echo off
    if exist "C:\Program Files (x86)\" set programfiles=C:\Program Files (x86)
    set pro=%programfiles%\Java\jdk1.7.0_02
    set pro2=%programfiles%\Java\jdk1.7.0_02
    set jac=\bin\javac.exe
    set jav=\bin\java.exe
    :co1
    set b=0
    set t=7
    goto co2
    :co2
    color %b%%t%
    :menu
    cls
    title The Perfect Compiler version 2.0
    echo Please choose an option below to do that function.
    echo.
    echo c - Compile your server.
    echo.
    echo.
    set /p c=Option:
    if %c%==c goto c
    if %c%==C goto c
    if %c%==* goto er
    goto er
    :color
    cls
    echo Would you like to change the backround color, change
    echo the text color, or reset it to default?
    echo.
    echo back - Change the backround color.
    echo text - Change the text color.
    echo def - Change all colors back to default.
    echo.
    echo.
    cls
    :c
    cls
    title Compiler
    goto cjc
    :c2
    if exist src (goto cl) else (goto c3)
    :c3
    if exist *.java (goto cl2) else (goto ce)
    :c4
    %javac% -cp . *.java
    echo Files Compiled Successfully!
    pause
    cls
    goto menu
    :c5
    %javac% -cp . .\src\*.java
    echo Files Compiled Successfully!
    pause
    move .\src\*.class .\classes\
    cls
    goto menu
    :ce
    cls
    echo You need to have Java files for this Compiler to Compile.
    pause
    cls
    goto menu
    :er
    cls
    title Error
    echo Invalid command. Please make sure the commands you type
    echo in are correct.
    pause
    cls
    goto menu
    :e
    cls
    exit
    :cjc
    if exist "%pro%_06%jac%" (goto sjc) else (goto cjc2)
    :cjc2
    if exist "%pro%_07%jac%" (goto sjc2) else (goto cjc3)
    :cjc3
    if exist "%pro%_08%jac%" (goto sjc3) else (goto cjc4)
    :cjc4
    if exist "%pro%_09%jac%" (goto sjc4) else (goto cjc5)
    :cjc5
    if exist "%pro%_10%jac%" (goto sjc5) else (goto cjc6)
    :cjc6
    if exist "%pro2%%jac%" (goto sjc6) else (goto cjc7)
    :cjc7
    if exist "%pro%%jac%" (goto sjc7) else (goto cjc8)
    :cjc8
    if exist "%pro%_01%jac%" (goto sjc8) else (goto cjc9)
    :cjc9
    if exist "%pro%_02%jac%" (goto sjc9) else (goto cjc10)
    :cjc10
    if exist "%pro%_03%jac%" (goto sjc10) else (goto cjc11)
    :cjc11
    if exist "%pro%_04%jac%" (goto sjc11) else (goto cjc12)
    :cjc12
    if exist "%pro%_05%jac%" (goto sjc12) else (goto je)
    :sjc
    set javac="%pro%_06%jac%"
    goto c2
    :sjc2
    set javac="%pro%_07%jac%"
    goto c2
    :sjc3
    set javac="%pro%_08%jac%"
    goto c2
    :sjc4
    set javac="%pro%_09%jac%"
    goto c2
    :sjc5
    set javac="%pro%_10%jac%"
    goto c2
    :sjc6
    set javac="%pro2%%jac%"
    goto c2
    :sjc7
    set javac="%pro%%jac%"
    goto c2
    :sjc8
    set javac="%pro%_01%jac%"
    goto c2
    :sjc9
    set javac="%pro%_02%jac%"
    goto c2
    :sjc10
    set javac="%pro%_03%jac%"
    goto c2
    :sjc11
    set javac="%pro%_04%jac%"
    goto c2
    :sjc12
    set javac="%pro%_05%jac%"
    goto c2
    :cj
    if exist "%pro%_06%jav%" (goto sj) else (goto cj2)
    :cj2
    if exist "%pro%_07%jav%" (goto sj2) else (goto cj3)
    :cj3
    if exist "%pro%_08%jav%" (goto sj3) else (goto cj4)
    :cj4
    if exist "%pro%_09%jav%" (goto sj4) else (goto cj5)
    :cj5
    if exist "%pro%_10%jav%" (goto sj5) else (goto cj6)
    :cj6
    if exist "%pro2%%jav%" (goto sj6) else (goto cj7)
    :cj7
    if exist "%pro%%jav%" (goto sj7) else (goto cj8)
    :cj8
    if exist "%pro%_01%jav%" (goto sj8) else (goto cj9)
    :cj9
    if exist "%pro%_02%jav%" (goto sj9) else (goto cj10)
    :cj10
    if exist "%pro%_03%jav%" (goto sj10) else (goto cj11)
    :cj11
    if exist "%pro%_04%jav%" (goto sj11) else (goto cj12)
    :cj12
    if exist "%pro%_05%jav%" (goto sj12) else (goto je)
    :sj
    set java="%pro%_06%jav%"
    goto r2
    :sj2
    set java="%pro%_07%jav%"
    goto r2
    :sj3
    set java="%pro%_08%jav%"
    goto r2
    :sj4
    set java="%pro%_09%jav%"
    goto r2
    :sj5
    set java="%pro%_10%jav%"
    goto r2
    :sj6
    set java="%pro2%%jav%"
    goto r2
    :sj7
    set java="%pro%%jav%"
    goto r2
    :sj8
    set java="%pro%_01%jav%"
    goto r2
    :sj9
    set java="%pro%_02%jav%"
    goto r2
    :sj10
    set java="%pro%_03%jav%"
    goto r2
    :sj11
    set java="%pro%_04%jav%"
    goto r2
    :sj12
    set java="%pro%_05%jav%"
    goto r2
    :je
    cls
    title Error
    echo You do not have JDK 5.0 or JDK 6.0. Go to od Taharok's tutorial
    echo on the moparscape Tuturoials section for step-by-step instructions
    echo link to download the newest JDK Update.
    pause
    cls
    goto menu
    :sl
    cls
    echo Ran Server at %time% and on %date%. >> "Compile Logs.txt"
    echo Server was run using %java%. >> "Compile Logs.txt"
    echo Thank-you for using Mod Taharok's 'Perfect' Compiler! >> "Compile Logs.txt"
    echo. >> "Compile Logs.txt"
    goto r5
    :sl2
    cls
    echo Ran Server at %time% and on %date%. >> "Compile Logs.txt"
    echo Server was run using %java%. >> "Compile Logs.txt"
    echo Thank-you for using Mod Taharok's 'Perfect' Compiler! >> "Compile Logs.txt"
    echo. >> "Compile Logs.txt"
    goto r6
    :cl
    cls
    echo Compiled Java files at %time% and on %date%. >> "Compile Logs.txt"
    echo Java files were compiled using %javac%. >> "Compile Logs.txt"
    echo Thank-you for using Mod Taharok's 'Perfect' Compiler! >> "Compile Logs.txt"
    echo. >> "Compile Logs.txt"
    goto c5
    :cl2
    cls
    echo Compiled Java files at %time% and on %date%. >> "Compile Logs.txt"
    echo Java files were compiled using %javac%. >> "Compile Logs.txt"
    echo Thank-you for using Mod Taharok's 'Perfect' Compiler! >> "Compile Logs.txt"
    echo. >> "Compile Logs.txt"
    goto c4
    :reset
    cls
    echo Resetting the Compiler's Logs...
    pause
    del "Compile Logs.txt"
    echo ----------Compiler Logs---------- >> "Compile Logs.txt"
    echo. >> "Compile Logs.txt"
    goto menu

    Save As Compiler.bat

    NOTICE: THIS COMPILER DOES NOT BELONG TO ME!
    NOTICE: ADD THIS COMPILER TO ExampleJar/ OR THE NAME OF YOUR FOLDER!

    Owner Of Compiler: Moparscape








    4. The Set Up:

    This is what you should have so far:
    WhatYouShouldHave.PNG


    5. COMPILING:

    Double-Click The Compiler
    Press "C"
    Press Enter

    If Done Correctly You Should See
    Files Compiled Successfully!
    Press any key to continue . . .

    Also ExampleClass.class & ExampleClass2.class
    Also A Compile Logs: You Can Delete That, That Is Not Needed (its in the compiler)

    This Is What You Should Have Now:
    WhatYouShouldHaveNow.PNG






    5. Making A Jar File:

    Notice: Spelling Is Absolutely Importing, Make Sure Everything Is Correct Before Proceeding.

    First:

    Open ExampleJar/Your Folder on your desktop
    Right click ExampleClass.java
    Open Properties
    You Will See Location: This Is Very Important. (LEAVE PROPERTIES OPEN)


    Second:

    Start-> Run-> CMD

    We Need To Change Are Directory To Are Local Disk
    So In This Case My Local Disk Is C:

    So Type: cd C:\
    You should See C:\

    Now Remember The Location From Earlier?

    Type cd "*Location Form Earlier*"



    Example:

    I Typed Whats In Bold

    C:\>cd "Documents and Settings\Owner\Desktop\ExampleJar

    Now The New Directory Is:
    C:\Documents and Settings\Owner\Desktop\ExampleJar>



    Making The Jar File:

    Follow Closely (MOST IMPORTANT PART)

    Now In CMD Where It Says: C:\Documents and Settings\Owner\Desktop\ExampleJar>

    Type:

    C:\Documents and Settings\Owner\Desktop\ExampleJar>jar cfmv ExampleJar.jar MANIFEST.txt ExampleClass.class ExampleClass2.class

    You Should Then See:

    added manifest
    adding: ExampleClass.class(in = 438) (out= 319)(deflated 27%)
    adding: ExampleClass2.class(in = 453) (out= 300)(deflated 33%)

    C:\Documents and Settings\Owner\Desktop\ExampleJar>

    NOTICE: IF YOU SEE
    java.io.FileNotFoundException: MAINFEST.txt (The system cannot find the file spe
    cified)
            at java.io.FileInputStream.open(Native Method)
            at java.io.FileInputStream.<init>(FileInputStream.java:138)
            at java.io.FileInputStream.<init>(FileInputStream.java:97)
            at sun.tools.jar.Main.run(Main.java:171)
            at sun.tools.jar.Main.main(Main.java:1177)
    THE FILES IN NOT THERE YOU YOU HAD MADE A SPELLING ERROR


    Now there should be a jar file in ExampleJar On your desktop.

    You Have Created Your Very Own Executable Jar File
    Last edited by Java Programmer; January 17th, 2012 at 12:45 AM.

  2. The Following 2 Users Say Thank You to Java Programmer For This Useful Post:

    ChristopherLowe (January 17th, 2012), Mr.777 (January 17th, 2012)


  3. #2
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Making Executable Jar Files.

    Nice tutorial. But some recommendations to improve it.
    Why do we need to write a complete compiler to compile classes? Just simply compile them from the java compiler.
    Make an explanation of creating jar file.
    jar cfmv ExampleJar.jar MANIFEST.txt ExampleClass.class ExampleClass2.class
    You must elaborate what each word do and how.
    Also, differentiate between PATH and CLASSPATH.

    These all answers can be found on google, i know but everything else can also be found. So, to make it a good and complete tutorial you must elaborate it with the above told suggestions. I hope you will take these suggestions as positive. Anyways, nice tutorial and thanks.

  4. #3
    Forum VIP
    Join Date
    Jun 2011
    Posts
    317
    My Mood
    Bored
    Thanks
    47
    Thanked 89 Times in 74 Posts
    Blog Entries
    4

    Default Re: Making Executable Jar Files.

    Nice tutorial. There is just a couple of things I have say about it though.

    That batch script ... ouch. All those nasty goto's with arbitrary variables indexed beyond any hope of readability are not the nastiest little dragon in this god forsaken, windows only script. The first warning as to it's worthlessness is it's title - Perfect Compiler. It is nearly as far away from being a compiler as it is to being perfect. Next up on my little list is it's assumption that I installed my SDK to C:\Program Files (x86)\Java\jdk1.7.0_02 which is only slightly off my actual JDK path of E:\jdk1.7.0_01. Finally, I don't really see the point in writing the output of javac to a file like that. I sure as s**t want to know what the compiler has to say for it's self and opening up my text editor to do it shouldn't be necessary.

    Now since you are not the author of the aforementioned batch script I really hope you don't any offense because the rest of the tutorial is really very good and I know how much work these things take, so thanks and well done.

    As for addressing the problems with the script I would suggest replacing that segment with the following...

    Next, install Ant. It is a mature, stable, cross platform and very intelligent build system written by the good people at the Apache Foundation. In their own words:

    Quote Originally Posted by Apache.org
    Apache Ant is a Java library and command-line tool whose mission is to drive processes described in build files as targets and extension points dependent upon each other. The main known usage of Ant is the build of Java applications. Ant supplies a number of built-in tasks allowing to compile, assemble, test and run Java applications. Ant can also be used effectively to build non Java applications, for instance C or C++ applications. More generally, Ant can be used to pilot any type of process which can be described in terms of targets and tasks.

    Ant is extremely flexible and does not impose coding conventions or directory layouts to the Java projects which adopt it as a build tool.
    I don't pretend to be an expert on ant but here is my secret reusable weapon for building jar files. Save this file as build.xml in the projects root directory.

    <!--
     
    	Build file
     
    	Author: Christopher Lowe   lowey2002_at_g{mail}
    	Last update: 17/1/2012 (v0.136)
     
     
    -->
     
     
    <!--  Change your projects name -->
    <project name="myproject" basedir="." default="main">
     
    	<!-- Change your package -->
    	<property name="package"		value="com.mydomain.myproject"/>
     
    	<!-- Change your main executable -->
    	<property name="main-class"		value="com.mydomain.myproject.Main"/>
     
    	<!-- Change your version -->
    	<property name="version"		value="0-0-1"/>    <!--do not use a fullstop '.'  -->
     
     
     
    	<!-- This is the projects directory structure -->
    	<property name="src.dir"              value="src"/>   
    	<property name="build.dir"            value="build"/>
    	<property name="classes.dir"        value="build/classes"/>
    	<property name="jar.dir"               value="build/jar"/>
    	<property name="archive.dir"	       value="archive"/>
    	<property name="lib.dir"		       value="lib"/>
    	<property name="resources.dir"     value="res"/>
     
     
    	<!--  External jar's  -->
    	<path id="project.classpath">
    		<fileset dir="${lib.dir}" includes="**/*.jar"/>
    	</path>
     
     
    	<!--  Local resources -->	
    	<fileset id="resources" dir="${resources.dir}">
    		<include name="**/*.properties"/>
    		<include name="**/*.gif"/>
    		<include name="**/*.jpg"/>
    		<include name="**/*.png"/>
    		<include name="**/*.ogg"/>
    		<include name="**/*.wav"/>
    		<include name="**/*.mp3"/>
    		<include name="**/*.ico"/>	
    	</fileset>
     
     
    	<!-- Usage -->
    	<target name="usage">
    		<echo>
    ${ant.project.name}
    ***************************************
    version: ${version}
     
      usage:  
         ant compile
                     -- compiles the source code
     
         ant build
                     -- builds the jar file
     
         ant run
                     -- runs the jar file
     
         ant save
                     -- zips all source to /archive
     
    		</echo>
    	</target>
     
     
     
    	<!--  Clean the build directory -->
    	<target name="clean">
    		<delete dir="${build.dir}"/>
    	</target>
     
     
    	<!--  Compile -->
    	<target name="compile" depends="clean">
    		<mkdir dir="${classes.dir}"/>
    			<javac srcdir="${src.dir}" destdir="${classes.dir}" includeantruntime="true">
    				<classpath refid="project.classpath"/>
    			</javac>
    	</target>
     
     
    	<!--  Package into a jar -->
    	<target name="build" depends="compile">
    		<mkdir dir="${jar.dir}"/>
    		<jar destfile="${jar.dir}/${ant.project.name}-${version}.jar" basedir="${classes.dir}">
    			<fileset refid="resources"/>
    			<zipgroupfileset dir="${lib.dir}" includes="**/*.jar"/>
    			<manifest>
    				<attribute name="Main-Class" value="${main-class}"/>
    			</manifest>
    		</jar>
    	</target>
     
     
    	<!--  Execute jar -->
    	<target name="run" depends="build">		
    		<java jar="${jar.dir}/${ant.project.name}-${version}.jar" fork="true"/>
    	</target>
     
     
    	<!--  Archive source -->
    	<target name="save">
    		<tstamp/>
    		<zip destfile="${archive.dir}/${ant.project.name}-${version}-${DSTAMP}${TSTAMP}.zip" basedir="src"/>
    	</target>
     
     
    	<!--  The main method prints the usage of this file. Can be changed to run if you like  -->
    	<target name="main" depends="usage" />
     
     
    </project>

    You will need to change the first three lines of this file. Change myproject to the name of your project. Also change the package com.mydomain.myproject and the main-class com.mydomain.myproject.Main to the entry point of your program. That is, the class that contains your main method.

    With this file, you can compile, jar and run your program from a single command
    $ ant compile
    $ ant build
    $ ant run

    Or if you are feeling especially lazy, why not create a batch script (windows) so you don't even need to open the command prompt. Save this as run.bat
    @echo off
    ant run
    set /P theuserinput="Press [ENTER] to finish"


    The file readme.txt in the attached zip file explains the directory structure. The important thing is to make sure that the directories in src/ match up with the package in the source and the ant script. So if your package is:
    package com.mydomain.myproject;

    Your main class would live in the directory:
    src/com/mydomain/myproject/Main.java

    The attached file template.zip contains the entire thing ready to go.
    Attached Files Attached Files
    Last edited by ChristopherLowe; January 18th, 2012 at 01:10 AM. Reason: removed explitives

  5. #4
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Making Executable Jar Files.

    I sure as s##t want to know what the compiler has to say
    You might want to clean that post up a little as this forum should be family friendly.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  6. #5
    Member
    Join Date
    Jan 2012
    Posts
    107
    My Mood
    Grumpy
    Thanks
    6
    Thanked 2 Times in 1 Post

    Default Re: Making Executable Jar Files.

    .-. Well well.

Similar Threads

  1. .jar executable
    By mwr76 in forum Java Theory & Questions
    Replies: 2
    Last Post: October 9th, 2011, 11:27 AM
  2. making a .bat file that compiles your java files
    By sp11k3t3ht3rd in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 24th, 2010, 12:55 AM
  3. Jar Executable File Not Working
    By Kimimaru in forum Java Theory & Questions
    Replies: 6
    Last Post: October 15th, 2010, 09:32 PM
  4. Jar Executable File
    By The_Mexican in forum What's Wrong With My Code?
    Replies: 43
    Last Post: June 23rd, 2010, 03:53 PM
  5. Making executable JAR more "executable"
    By ni4ni in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 1st, 2010, 01:19 PM