Pooja3101
Pooja3101

Reputation: 711

Creating jars from a class and using in in other programs

I am a newbie in Java.

I have a java file A which I want to call in another java program B.

I want to create jar for the A and use it in B by creating the objects and calling the methods for A.

What type of jar is to be created and how can I add it to the library. Please help how to do it in Eclipse.

Also, how to import the jar in B.

Upvotes: 0

Views: 49

Answers (2)

SSB
SSB

Reputation: 789

  1. First create a project in eclipse with class A (Creating project in Eclipse)
  2. Export this project as jar (Exporting jar in Eclipse)
  3. Create another project with class B and set class path of for exported jar (Setting class path in Eclipse)

Now you can use object of A in B.

 import com.A;
 public class B{
    public static void main(String[] args){
           A a=new A();
           //.......
}
 }

Upvotes: 1

tr4pt
tr4pt

Reputation: 317

You can go to File->Export->jar-File name your jar-file (you do not need a runnable jar)

after that you can open your other project B right-click -> properties-> java build path

select libraries and click on add external jars

choose your exported jar-file. Click ok and it is imported to your actual project B

now you can use classes and methods of this jar-File

Upvotes: 3

Related Questions