Ranjeet
Ranjeet

Reputation: 1

creating our own jar and using it in another project

I want to create our own jar which has some simple methods like a print method.

I created the jar of that java project and imported it into an other project I am building path also.

The problem is that I am able to create the object of the classes which reside in the jar but I am unable to call the methods of that class. i am using eclipse 3.4 (Eclipse Ganymede (3.4))version

Upvotes: 0

Views: 706

Answers (1)

Michael
Michael

Reputation: 2510

Sounds like if you are successfully building the JAR that you are not including it in the classpath when you compile / run your application. You can do that if you are compiling/running from the command line with the -cp or -classpath option. Both perform the same function.

To compile:

javac -cp .:PathToMyJar/MyJar.jar MyMainClass.java

To Run:

java -cp .:PathToMyJar/MyJar.jar MyMainClass

The above commands will look in the current directory ('.') and in the MyJar.jar file specified. On Windows you will separate the items in the classpath with a semicolon and on Linux/Unix/OS X you will use a colon.

If you are using an IDE you will need to somehow add the JAR file to the classpath for your project. How to do that is IDE specific.

Upvotes: 1

Related Questions