-1

I'm trying to make an application that will be able to load a class from an external .jar file. However, I need to refer to the class I will load by it's type (it's a grandchild class of DefaultHandler for Sax parser).

I thought I'd do it this way :

I'll define an abstract class Hammer extends DefaultHandler

The class in an external file will extend Hammer (let's call the external class SpecialHammer)

I'll load SpecialHammer using URLLoader.


The problem is that I don't know how (and where) to define Hammer to make sure that the application believes that the SpecialHammer it is loading actually extends the Hammer it knows. I tried putting the same Hammer class in the application and in the external .jar, but it did not work :/

I basically need to know how to share an abstract class between two jars so that both know they're reffering to the same class.

I'm using Eclipse if that's of any help.

Nouth
  • 21
  • 2
  • Just add the external jar to your project's classpath – Brovoker Apr 03 '15 at 13:22
  • That is sadly not the point - I want the application to be extendible without the need to recompile it (It will read xml files, and the user will have to supply a specific class for handling the xml reading - since I can't depend on the xmls being formatted in the same way.) – Nouth Apr 03 '15 at 13:48

2 Answers2

0

I suggest you use Maven for project dependencies.

More information here : https://maven.apache.org/guides/getting-started/index.html

Brovoker
  • 896
  • 5
  • 16
0

The application jar in theory needs to hold an interface, this interface will need to be extended by the class being used to parse so probably best to have it in a dependency both the client and processing application can share.

I think the class to be shared in your example would be Hammer (the abstract class) and DefaultHammer, that way the client has a choice of which class they would extended from.

So you would have the abstract class Hammer and the DefaultHammer in common.jar and both application.jar and client.jar would use common.jar as a dependency.

A little off topic perhaps, but perhaps a better solution, depending on the user/client would be to make use of Java's SPI interface (or roll your own). Or perhaps Byteman that allows you to inject code into the JVM, (is is a DSL that wraps the -agent option on the javac).

Blog about using Byteman

Gavin
  • 1,725
  • 21
  • 34