aspose file tools*
The moose likes Beginning Java and the fly likes How to make our own jar files and how to acess the methods in the jar Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to make our own jar files and how to acess the methods in the jar" Watch "How to make our own jar files and how to acess the methods in the jar" New topic
Author

How to make our own jar files and how to acess the methods in the jar

Hemanth Kumar Simhadri
Ranch Hand

Joined: Apr 22, 2010
Posts: 32
I have some requirement where in I have to make a new jar file which contains some classes which have several methods. How to make the jar file and how to access the methods in the jar file similar to standard java methods like string class methods, array methods e.t.c

Example: I have to write a new class "MyMath" which contains some methods which does some mathematical operations like addition, multiplication and division. I have to make the jar of this class and make these methods available for any java program like the normal java string functions.


Class MyMath {

int divide(int x, int y){
int k = x/y;
return k;
}

int multiply(int x, int y){
int k = x*y;
return k;
}

-------
-------
}

Now I want to write a class "TestClass" and use the above methods directly by making the jar of the above class

Class TestClass {
public static void main(String[] args)
{
MyMath myMath = new MyMath();
int k1 = myMath.divide(6,2);
int k2 = myMath.multiply(6,2);
System.out.println("The value of K1 is --> "+k1);
System.out.println("The value of k2 is --> "+k2);
}


}

I did like this. I compiled the class "MyMath" and placed it in a folder called "TestJar" and zipped the folder "TestJar".
Now I renamed the zip "TestJar.zip" to "TestJar.jar". I copied this jar file in the paths "D:\Java\jre1.5.0_17\lib" and "D:\Java\jre1.5.0_17\lib\ext"

But still my class TestClass is still not able to recognize the class "MyMath" and is giving error

D:\TestJar>javac TestClass.java
TestClass.java:5: cannot find symbol
symbol : class Divide
location: class TestClass
MyMath myMath = new MyMath();
^

Please help me in this issue




Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13884
    
  10

Welcome to JavaRanch.

Hemanth Kumar Simhadri wrote:I did like this. I compiled the class "MyMath" and placed it in a folder called "TestJar" and zipped the folder "TestJar".
Now I renamed the zip "TestJar.zip" to "TestJar.jar".

The JDK contains a tool to make JAR files: the jar tool. Use that to make JAR files instead of using a ZIP program to make sure that you get a valid JAR file. See Java Archive (JAR) Files for more information.

Hemanth Kumar Simhadri wrote:I copied this jar file in the paths "D:\Java\jre1.5.0_17\lib" and "D:\Java\jre1.5.0_17\lib\ext"

Don't do that. To use the JAR file, make sure you put it in your classpath while compiling and running the application that uses the JAR file.

Especially putting it in the lib directory of the JDK is not a good idea; that directory is not meant to put custom JAR files in, and Java won't even look there to find your JAR file.

Some more useful links:
Setting the class path
Managing Source and Class Files
Managing the Java classpath


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Inder Kumar Rathore
Ranch Hand

Joined: Apr 14, 2010
Posts: 41


D:\Java\jre1.5.0_17\lib" and "D:\Java\jre1.5.0_17\lib\ext"

this class path will be searched by jvm not by the compiler

so move your jar file to D:\Java\jdk1.6.0_03\jre\lib\ext"

the later classpath is searched by the compiler

again i am not sure that renaming the file will work.....so for creating a jar file use jar utility


Thanks

Living is easy with your eyes closed.....
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13884
    
  10

It's better not to use the lib\ext folder at all - instead, set your classpath correctly.
Hemanth Kumar Simhadri
Ranch Hand

Joined: Apr 22, 2010
Posts: 32
I made the jar using ecilipse and I have it ready with me. Will the ecilipse able to recognize it when we add the path of the jar in the class path. When we create an object of the class "MyMath" and invoke the methods using the object, will the ecilipse identify the class? is there anything extra to be done inorder to make the ecilipse identify the class in any java class made in any project using ecilipse
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13884
    
  10

If you're doing this in Eclipse, then you can specify for your project which libraries it uses. In Eclipse, right-click the project in the Package Explorer, and choose Properties. Go to Java Build Path, then to Libraries, and click "Add JARs" or "Add External JARs" to add the JAR file to your project as a library.
Hemanth Kumar Simhadri
Ranch Hand

Joined: Apr 22, 2010
Posts: 32
Hello Jasper Young. Thank you for the reply. I tried the way what you told. I added my new jar file in the project. Now I can see the jar in the tree "Referenced Libraries". I added my jar file using the option "Add External Jars".

When I create an object for the class "MyMath" which is in the jar file it is giving error in ecilipse.

I wrote the code like this...

MyMath myMath = new MyMath();

The error message that I am getting in the ecilipse is "MyMath cannot be resolved to a type"
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to make our own jar files and how to acess the methods in the jar
 
Similar Threads
Threads
Help.....Thread
Synchronizing question
I don't know what's wrong with this chess game
Java 6 Compiler Error with "System.out.println("x " + x);"