0

I wrote a project which contains some classes I want to reuse in other projects.

I know I can copy paste and change the package so that I can reuse them in other projects.

But is there a better way? I am thinking I should export them as a library. And when I need it, I can add it as external JAR file in Java build path. Is it possible and how can I do it?

Thx!!

nomnom
  • 1,560
  • 5
  • 17
  • 31
  • 2
    Export the project as a jar like http://stackoverflow.com/questions/423938/java-export-to-an-jar-file-in-eclipse then add the jar to the build path configuration of the project that requires it. – David Oct 16 '13 at 07:51
  • yes, it is possible and exporting as a jar and using it is the correct way ;) – RamonBoza Oct 16 '13 at 07:51

3 Answers3

4
  • Right click on project in which you need to reuse the java class
  • Choose the Build Path option
  • In the Projects tab add the project which contains the class you need to reuse

And you are good to go. This will add the dependency of one project to another and you will be able to access classes of that project.

Eclipse Java Build path

Narendra Pathai
  • 41,187
  • 18
  • 82
  • 120
0

these are based on the requirement, for instances if you have functionality used details which is required by xxx and yyy (let us these are wars), in this case you can have user details as jar and include this jar in both xxx and yyy . in this way you no need to copy files where ever you are required. and other way it is easy to maintain.

let us say you want add a functionality to user details, you can add it once and it will be reflected where ever it is referenced.

hence move the files to a separate package and make it is a jar. later you can have this jar as a dependency(maven) where ever you required.

pappu_kutty
  • 2,378
  • 8
  • 49
  • 93
0

You can use "import" Write import and then the path to the classes you want to use.

You have to write it over "public class".

plsgogame
  • 1,334
  • 15
  • 28
Jens
  • 1
  • 1