Press enter to see results or esc to cancel.


What is a Jar file, Runnable Jar file

This tutorial explains about a Jar File, Runnable Jar File with a Simple Example.

  1. Java JAR stands for Java Archive in Programming. It is actually a compact or zip format of your java program.
  2. The main use of a Jar comes if you want to run your java program in any other PC where you don’t have any JAVA IDE, Normally you run your java program in your Java IDE like Netbeans or Eclipse.
  3. In Such cases you can zip down your java program in to Jar file and take this jar to any other client PC or Location and run there without any additional programs. All you want is java installed in the client PC.

How to Create a Runnable Jar File.

  1. Now lets create a Runnable Jar File. Here I am showing to create Runnable Jar file in Eclipse IDE.
  2. In eclipse go to Menu File > New > Project. In Wizards just search for Java Project and select the wizard as Java Project.
    
    
  3. Give a project Name.

  4. Now lets create a Small program to create a Text file with Current Date and Time. Now if you run this program as Java Application This run_this.java class will save a text file with name ‘jinutest.txt’ in location ‘e:\\jinutest.txt’.
    You can write any java program here. Even connection to MySql database, Insert Values into database Etc.
  5. Project Structure.

    
    
  6. run_this.java

    package my_main;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.TimeZone;
    public class run_this {
         public static void main(String[] args) throws IOException {
    	TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
    	SimpleDateFormat format = new SimpleDateFormat("dd-MMM-yyyy hh:mm a");
            Date today4 = new Date();
    	String date4 = format.format(today4);
    	BufferedWriter output = null;
    	try {
    	    File file = new File("e:\\jinutest.txt");
    	    output = new BufferedWriter(new FileWriter(file));
    	    output.write(date4);
    	    System.out.println("done");
    	 } catch ( IOException e ) {
    	    e.printStackTrace();
    	 } finally {
    	    if ( output != null ) {
    	       output.close();
    	       }
    	    }
    	}
    }
    
  7. Now to work this program in client PC you have to export this project as Runnable Jar File. To export this Right Click on the Project and Select ‘Export’

    
    
  8. In the export wizard search for Runnable Jar File.

    
    
  9. Select the launch configuration(The class with main method) and the export destination. Then click finish to save the program as runnable jar file.

    
    
  10. Now the runnable jar file with .jar extension would have generated in the destination.

Tags

Comments

Leave a Comment