0

I want to create one external jar for Logging Purposes,so that i can import this jar in any java project.But i would like to use the classes and methods of java project in external jar.

Is it possible ?If yes,please help. I think it's possible from Reverse Engineering,but not able to figure out how to use class and specially methods in external jar.

Tanvi Garg
  • 308
  • 5
  • 18
  • I'm not sure I understand what you're asking. Can you provide an example? Since you wrote the jar is to be used in _any_ java application you can't know which classes are part of that application or what they do. So you'd not be able to use them in any meaningful way anyways. So what's the use case? – Thomas Mar 21 '17 at 09:13
  • I want to insert and get my audit logs from database, this can be used in any of the application. so I am planning to create an external jar which will have two methods,one is for insertion and second is to retrieve data,these methods include calling of procedures from database.For calling of procedure,i will use my main project's classes and methods. – Tanvi Garg Mar 21 '17 at 09:30
  • You may want to use build manager such as maven or gradle. What you can do is create a maven project with build target as `jar` . Also the classes from other project that you want to use in this jar should also be common instead of part of that project. They can be taken out as a separate jar and included wherever needed. – Sajjad Mar 21 '17 at 09:38
  • I'm still not sure I understand you correctly but you're probably after callbacks, i.e. the application implements certain interfaces the jar provides and registers implementations of those interfaces with the jar's code in order to enable it to call those procedures. – Thomas Mar 21 '17 at 09:39
  • I already have a maven project of creating API's with build manager maven.I have one class A in API Project and will create one external lib(jar) altogether separate java project and would like to use class A and its methods in that external jar. – Tanvi Garg Mar 21 '17 at 09:40
  • If that class A is logically independent of API project, i.e if it is abstract class or interface that can be implemented by various other projects then it is better to take it out of API project and create a separate module or project that will hold such classes and then that module can be included to class path of both API project as well as external jar project. – Sajjad Mar 21 '17 at 09:49
  • Class A is logically dependent of API Project. – Tanvi Garg Mar 21 '17 at 09:58
  • Then that is a bad design, you may not want to use some class that is coupled with your API project or is specific to API project. Probably pasting piece of your code will help understand the problem better. – Sajjad Mar 21 '17 at 10:34

1 Answers1

0

It is possible, but very bad practice.

You need to import each project into eachother.

Project A imports Project B and Project B imports Project A.

I would recommend you to use eclipse and add the projects as source. You can read how to do that here: Creating a java library with Eclipse

You can read about the bad practice of what you ara about to do here:

Two java libraries importing each other?

Community
  • 1
  • 1
kkflf
  • 2,435
  • 3
  • 26
  • 42
  • I can't import the main project into external jar because when i'll create external jar then the main projects classes are not important. – Tanvi Garg Mar 21 '17 at 09:38
  • If both projects import themselves mutually they can as well be one project. As you said it's very bad practive and probably doesn't solve anything. – Thomas Mar 21 '17 at 09:40