Antonio López
Antonio López

Reputation: 139

Using methods from a .jar file in Java

Question about Java. I'm using Eclipse, and I've created a simple project called Main. In this project there's a class called MainClass.

public class MainClass
{
}

I have another project called Math. It has only one class called Functions, and it's got one method called add:

public class Functions
{
    public int add(int a, int b)
    {
        return a+b;
    }
}

I've exported this project called Math into a .jar file. I want to use the add method from the .jar file in the project Main, for example:

public class MainClass
{
    int x = add(1, 2);
}

What do I have to do? Thanks for your time.

Edit: Ok, I've added the jar in the lib folder, but now, how do I use add(int a, int b)? It says it is "undefined".

2nd Edit: I created an instance of the class Functions, so now I can use the methods. Thanks for your replies!

Upvotes: 2

Views: 5799

Answers (3)

Samuel
Samuel

Reputation: 17171

You need to include the .jar file in Main's classpath.

Right click on the Main project in Eclipse, click properties, click "Java Build Path", click "Libraries", click add jar or add external jar and find the .jar file. That's it!

Upvotes: 0

Kamal
Kamal

Reputation: 4088

You should include the .jar in your .classpath file. Once this step is done, you can use the method.

Upvotes: 0

Amit Deshpande
Amit Deshpande

Reputation: 19185

create folderlib add your jar to it. goto Project->Properties->Java Build Path-> Libraries->Add Jar and you are done.

Upvotes: 3

Related Questions