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.

Executing executable Jar file from Java Program

Sample code to execute jar file from Java Program. Pre-requisite 1. Need to have executable jar file. 2. Copy the code in your workspace and Run the code. package com.test.reusable.enterprise.service; import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import org.apache.commons.lang.exception.ExceptionUtils; public class ExecuteJarFile { public static void main(String[] args) { try { //Method1 to Execute Jar file From Java Program ProcessBuilder pb = new ProcessBuilder("java", "-jar", "HelloWorld.jar"); // command to execute jar file with fileName.jar pb.redirectErrorStream(true); pb.directory(new File("C:/www/")); // Directory path where your jar file is placed. Process p = pb.start();

Remove element from Array Javascript

Remove element from Array Javascript ----------------------------------------------------------------------- const array = ["test1", "test2", "test3", "test4"]; console.log(array); const index = array.indexOf("test2"); if (index > -1) {   array.splice(index, 1); } // **** array = ["test1", "test3", "test4"] console.log(array);