2

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!

2
  • welcome to Stack Overflow. If you find a anser helpful, you can accept it :) Commented Sep 14, 2012 at 14:03
  • Post the code of what you have tried. If post an SSCCE you will get the solution faster. Commented Sep 15, 2012 at 6:27

3 Answers 3

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for adding this answer @AmitD.. it was great help!
0

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

Comments

0

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.