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
Post a Comment