Skip to content
Advertisement

How can I create .JAR to Do a specific task , then use this JAR in another project?

e.g : i have a simple code that take an array list of numbers and computes Sum. I am created a .JAR of this code. now my question is how can I import this JAR in another project and pass array list to it , and JAR give me the result to reuse it ??

Advertisement

Answer

This is an example 1- in a directory commycompanymyproject create Task.java

JavaScript

2- in a directory commycompanymyprojectsupport create MyTask.java

JavaScript

3- compile both .java with command $javac com/mycompany/myproject/Task.java and command $javac com/mycompany/myproject/support/MyTask.java

4- create .jar file with command $jar -cvf task.jar com/mycompany/myproject/Task.class com/mycompany/myproject/support/MyTask.class (I decided to put “task” as name of my .jar file)

At this point you have created you .JAR and you can use it in another project. Let’s see how to do it.

5- take your task.jar file and put it where you have defined your CLASSPATH System Variable

6- create Main.java in any directory.

JavaScript

7- comnpile Main.java with $javac Main.java

8- take Main.class(result of compilation of Main.java) and put it where you have defined your CLASSPATH System Variable.

9- go to your CLASSPATH directory and execute command $java Main

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement