Exporting/Importing JAR files

5 April 2007

JAR (Java archive) files are commonly being used by Java developers. JAR file is a deployable package comprising of number of class files and other resources. One benefit is that JAR file uses compression algorithms to reduce the overall size.

Now suppose we have a Java project in Eclipse called Resources. It comprices of 2 classes Student and Teacher.

To export them as JAR from IDE. Follow the preceding steps.

  1. Right click the project name in the Package Explorer and click “Export”.
  2. Under Java folder, click the JAR file and click “Next”.
  3. Check the classes that you want to export. In our case, we checked both files. Also browse to the export path.
  4. Now click Next -> Next -> Finish.

To import Resources.jar in your other project.

  1. In the project properties window (click Project -> Properties -> Java build path -> Libraries -> Add External Jars.
  2. Browse to the JAR file you want to import and click “Finish”.

Review following code samples:

public class Student {private String str_name;

private int int_age;

private String str_major; // constructor

public Student(String str_name, int int_age,String str_major)

{

 this.str_name = str_name;

 this.int_age = int_age;

 this.str_major = str_major;

}public Student()

{

}

public int getInt_age() {

 return int_age;

}

public void setInt_age(int int_age) {

 this.int_age = int_age;

}

public String getStr_major() {

 return str_major;

}

public void setStr_major(String str_major) {

 this.str_major = str_major;

}

public String getStr_name() {

 return str_name;

}

public void setStr_name(String str_name) {

 this.str_name = str_name;

}

}

public class Teacher {

private String str_name;

 private String str_qualification;

 private int int_age;

public Teacher(String str_name, String str_qualification,int int_age)

 {

 	this.str_name = str_name;

 	this.str_qualification = str_qualification;

 	this.int_age = int_age;

}

public Teacher()

 {

 }

public int getInt_age() {

 	return int_age;

 }

public void setInt_age(int int_age) {

 	this.int_age = int_age;

 }

public String getStr_name() {

 	return str_name;

 }

public void setStr_name(String str_name) {

 	this.str_name = str_name;

 }

public String getStr_qualification() {

 	return str_qualification;

 }

public void setStr_qualification(String str_qualification) {

 	this.str_qualification = str_qualification;

 }

}

//JAR file (resources.jar) is imported in our project (UsingResources).

//Now we will use classes Student and Teacher in our new project.

public class School {

public static void main(String[] args) {

Student student1 = new Student("Chris",30,"Maths");

 	Teacher teacher1 = new Teacher("Martin","Phd",50);

//		printing Student data

 	System.out.println("Student name: " + student1.getStr_name());

 	System.out.println("Student age: " + student1.getInt_age());

 	System.out.println("Student major: " + student1.getStr_major());

//		printing Teacher data

 	System.out.println("Teacher name: " + teacher1.getStr_name());

 	System.out.println("Teacher qualification: " + teacher1.getStr_qualification());

 	System.out.println("Teacher age: " + teacher1.getInt_age());

Related Posts:


    Fatal error: Call to undefined function related_posts() in /home/blog/public_html/eclipse-blog/wp-content/themes/Andreas 08 3 columns/single.php on line 23