Skip to main content

Creating Jar file in Java

Creating Jar file through Eclipse.

1. Create New Java Project in Eclipse as shown in below snap.
2. Now create your java(SampleJar)file [Name doesn't matter].
3. Right Click on created Project and now click on Export and after that Navigate to java and select JAR file and after that follow the Instructions as shown in the window. and hence forth your JAR file is created for execution.

4. Now create new Java Project and import SampleJar.jar file which is created as shown in below image.
5. Create Java file TestJar and execute your program with created jar.



TestJar.java

package com.angad.testjar;

import com.angad.jarfile.SampleJar;

public class TestJar {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SampleJar sampleJar = new SampleJar();
System.out.println("Square Method Called from JAR ==>"+sampleJar.square(10));
System.out.println("Cube Method Called from JAR ==>"+sampleJar.cube(10));
}

}

output>

Creating Jar file through Command Prompt.


1.Right-click and create a new folder on your desktop.

2. Either save or drag and drop your Java files into your folder.

3. Now open command prompt.

4. In the command prompt, type "cd Desktop" to change your directory to the  desktop, the location of your folder.

5. By typing "dir" you should see your Java files inside of the folder. In order to turn these into class files, we need to compile them by using the "javac" command. This command requires the JDK and it requires you to change the classpath.

6.To compile the files, type "javac JavaFile.java" - to compile multiple files just put a space between them and use the same command "javac JavaFile1.java JavaFile2.java".

7. Type "dir" again and you should see your Java files and now your class files as well.
Inside the folder, create a new text document. In notepad, you need to show what your main class is for the jar file. Here's how you do this:
   1 Main-Class: MyMainClass
   2
   3 |

8. This part is very important, so make sure to take note of the capitalization and the spacing. Don't add the .class ending to your specified main class. You MUST press enter twice from the line you write on so that your curser ends up exactly where I placed the vertical bar. From that point, save it (to your folder of course) as "MyManifest.txt". The name doesn't really matter, but it's called a manifest document.

7. Now go back into your command prompt and navigate back to your folder.
8. Here's how you make your jar file once you have the files all ready to go. You should be in your folder directory in the command prompt.
Type "jar cfm JarFileName.jar MyManifest.txt JavaFile1.class JavaFile2.class" - Type "dir" and you should see your jar file created.

Comments

Popular posts from this blog

Creating Pdf in multiple Languages in Java

Creating Pdf in multiple Languages in Java This code demonstrate how to create pdf in multiple languages in java by using ITEXT pdf creator. To use this sample code download itext.jar To display different locale on pdf you need to download font known as Arialuni.ttf and added to the project Most important thing is the encoding part set encoding to "Identity-H" (String encoding = "Identity-H";) as well as create font for the same to take effect in pdf create font (Font fontNormal = FontFactory.getFont(("c:/windows/fonts/arialuni.ttf"), encoding,BaseFont.EMBEDDED, 8, Font.NORMAL);) You can download the source code from here import java.io.FileOutputStream; import com.lowagie.text.Chunk; import com.lowagie.text.Document; import com.lowagie.text.Font; import com.lowagie.text.FontFactory; import com.lowagie.text.HeaderFooter; import com.lowagie.text.Paragraph; import com.lowagie.text.Phrase; import com.lowagie.text.pdf....

Basic Java Interview Questions

1. Why java does not support multiple inheritance ? Answer :- To say why java doesn't support inheritance directly like c++ should be the diamond problem faced by c++,(Diamond problem is an ambiguity that arises when two classes B and C inherit from A, and class D inherits from both B and C. If a method in D calls a method defined in A (and does not override it), and B and C have overridden this method differently, then via which class does it inherit: B, or C?) 2. JVM crash is an Error or Exception.? Answer:- JVM crash is an Error as a normal developer as we cannot handle it, but its exception for JVM developer as they can fix it. 3. Print “Hello World!” Without Main Method in Java? Answer:- public class Testing {    static    {        System.out.println("Hello World");    }    public static void main(String[] args)    {    } } ...

Collections in Java

Collections in Java Introduction to Collections Framework The Collections Framework provides a well defined set of interfaces and classes for storing and manipulating the groups of data into a single unit. Introduction to Collections Framework Collections Framework: The Collections Framework provides a well-designed set of interfaces and classes for storing and manipulating the groups of data into a single unit. The collections framework is a unified architecture which is used to represent and manipulate collections. The framework allows the collections to get manipulated independently, additionally it reduces the programming efforts and increases performance. It includes implementation of interfaces and algorithms. Basically it is a unified architecture that consists the following collections: Interfaces: These are the abstract data types that represent collections. With the help of interfaces we manipulate collections independently. A hierarchy is generally formed ...