Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Just rephrasing the question to make sure I it correctly;</p> <p>You want to be able to upload an arbitrary jar file to the server and then use the contained classes/resources in the JVM? Without restarting the JVM and/or editing your configuration ofcourse.</p> <p>If that's the case, then you should load the jar into a classloader (chaining your current classloader if needed) and then load the class from there.</p> <p>Assuming you store the jar-file physically on the server you could for example do something like:</p> <pre><code>public static Class&lt;?&gt; loadClass(String className, String jarFileLocation) throws MalformedURLException, ClassNotFoundException { URL jarUrl = new File(jarFileLocation).toURI().toURL(); ClassLoader classLoader = new URLClassLoader(new URL[] {jarUrl }, MyClass.class.getClassLoader()); return classLoader.loadClass(className); } public static Object executeMethodOndClass(String methodName, Class&lt;?&gt;[] parameterTypes, Object[] parameters, String className, String jarFileLocation) throws MalformedURLException, ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException { Class&lt;?&gt; loadedClass = loadClass(className, jarFileLocation); Method method = loadedClass.getMethod(methodName, parameterTypes); Object instance = loadedClass.newInstance(); return method.invoke(instance, parameters); } </code></pre> <p>Ps. this is crude code, I didn't even compile or test it; it <em>should</em> work, but nothing more then that and there is the chance I overlooked something or made a typo ;-)</p> <p>Pps. allowing custom jar files to be uploaded and classes from it to be executed does bring a number of (security) risks with it.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload