Implementing Extensions

Hey, I’m back again with another question. So I’m in the process of trying to add extension capabilities to my graphics engine. The thing is, I can’t FORCE the user to register the extension with the main library before using the library’s objects and functions. This led me to trying to make extensions automatically register with the main library during initialization. However, classes aren’t loaded until they are referenced, and this leads to this strange issue of “how can two classes interact without the user invoking them?”

static blocks dont get called until the class is loaded…
static final Object variables are only computed if used…
etc…

if anyone has a solution to this, please let me know

You basically need the new module capabilities in Java 9, which have new discovery APIs.

Cas :slight_smile:

Oh noooooooo

You could provide an interface to implement. This can be used by your extension developer. He can generate an artifact and place it in a folder of your engine…you could scan the folder, load the jars, scan all packages (or a certain package) and load the imeplementation of the interface and call a potential init-method.

Wouldn’t this work?

Sorry, I didn’t quite follow this. What do you mean by “artifact”

Since Java 6 there is also a way to load custom plugins/extensions via the java.util.ServiceLoader class.
Have a look here: http://stackoverflow.com/questions/16102010/dynamically-loading-plugin-jars-using-serviceloader
and here: http://docs.oracle.com/javase/6/docs/api/java/util/ServiceLoader.html
and also here: https://docs.oracle.com/javase/tutorial/ext/basics/spi.html

It basically solves the two problems:

  • how to declare which services are exposed by a plugin/module/extension (i.e. via a file in the META-INF directory of the jar)
  • how to load the service classes into the JVM and instantiate it

Or you could use a library like scannotation
-ClaasJG

find my old code ^^ (written in 2012)
is it right - can be better< but it works :wink:
java 1.6


///////////////////////////////
//Mod interface = Sep Proj
public interface inter_1 {
	void Print();
}
///////////////////////////////
///Game = Sep Proj <- attach Mod interface Proj
public static void main(String[] args){
	Class abs_Class = inter_1.class;
	inter_1 a = null;
	try{	
		//////////////////////
		URL url = new URL("file:Clases/CL.jar");//Mod Jar path
		URLClassLoader loader = new URLClassLoader (new URL[] {url});
		Class c1_Class = Class.forName("CL.c2", true, loader);
		//////////////////////
		Constructor<Object> obj_Construcrot;
		obj_Construcrot = c1_Class.getConstructor(null);
		Object obj_class = obj_Construcrot.newInstance(null);
		a = (inter_1) abs_Class.cast(obj_class);
	}catch(Exception e){
		e.printStackTrace();
	} 
	a.Print();
}
///////////////////////////////
//Mod = Sep Proj <- attach Mod interface Proj
///Clases/CL.jar
//CL.jar\CL\c2.class
public class c2 implements inter_1{
	public int a = 1;

	@Override
	public void Print(){
		System.out.println(getClass() + " : 1131");
	}
}

this [icode]Class c1_Class = Class.forName(“CL.c2”, true, loader)[/icode]
can be unify for special run Class name in mods - Like [icode]Mod_Main.class[/icode]

p.s it was written in age of minecraft Mod Api - because we can :stuck_out_tongue:

I wrote an article about dynamic plugin loading that I use.

Check it out in the wiki section - its dead simple and plenty, plenty flexible.

For something that runs everywhere…

OSGi

Wikipedia OSGi

Apache Felix

Eclispse is based on OSGi

A tutorial about OSGi in respect to Eclipse

OSGi is a huge system ._.

Maybe he is just fine with loading jar files with a simple config file with user made metadata.