FreeKB - Eclipse Create a Java Archive (JAR) file
Eclipse - Create a Java Archive (JAR) file

Create a new project in Eclipse

In Eclipse, select  File > New > Java Project. Give your project a name, such as Sample and select Finish. In this example, a project called Sample is created.

 


Create a package

In the left panel of Eclipse, expand your project. Right-click on the src folder and select New > Package. Give your package a name and select Finish. In this example, a package named com.sample.main is created.

 


Create Premain.class

Right-click on the package, and select New Class. Give the class a name (probably Premain), and select Finish. In Premain.class, add the following markup.

The premain class is used to avoid error Failed to load Premain-Class manifest attribute.

package com.sample.main;

import java.lang.instrument.Instrumentation;

public class Premain {
    public static void premain(String agentArgument, Instrumentation instrument) {
        System.out.println("Premain Java Agent Loaded");
    }
}

 


Create People.class

Right-click on the package, and select New Class. Give the class a name (probably People), and select Finish.

 

In People.class, add the following markup.

package com.sample.main;

public class People {
	public String getName(){
		return "John Doe";
	}
}

 


Create the JAR

Right-click on your project and select Export. Select JAR file and select Next. Select Browse and select a location on your local PC. Select Finish.

 


View the JAR

Using the jar command, you can view the contents of the JAR file. In this example, the JAR will contain Premain.class and People.class in the com/sample/main/ package.

META-INF/MAINFEST.MF
com/sample/main/People.class
com/sample/main/Premain.class

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter b125b in the box below so that we can be sure you are a human.




Comments

Web design by yours truely - me, myself, and I   |   jeremy.canfield@freekb.net   |