I have recently been playing with the reflection API
and I have made some calls like this Successfully shot down, where I'm dynamically loading my class from a jar archive with URLClassLoader
:
public Square JarUrlClassLoader {public static zero main (string array []) {file jarfile = new file ("PATH_TO_JAR_ARCHIVE.jar"); String classname = "org.test.MyClass"; Try {URL fileURL = jarFile.toURI (). ToURL (); String jarURL = "jar:" + fileURL + "! /"; URL URL [] = {new URL (jarURL)}; URL class loader loader = new URL class loader (url); Object obj = Class.forName (classname, true, loader). NewestInstance (); System.out.println ("The object is: \" "+ obj.toString () +" \ ""); } Grip (malarmudural lexception ex) {ex.printStackTrace (); } Hold (InstantiationException ex) {ex.printStackTrace (); } Hold (IllegalAccessException pre) {ex.printStackTrace (); } Grip (Klassnotfound Express Pre) {ex.printStackTrace (); }}
This snippet works well and seems easy to understand. But when I further assumed that this jar file is already in my Tommak AS Liberal Folder, which is on top of it and listening to some ports. This class org.test.MyClass
can then be loaded into JVM and so I can load it from there instead of loading it from the jar archive.
So I tried the following snippet that does not work:
Public class RemoteUrlClassLoader {Personal Static Last String httpURL = "http: // localhost: 8080 /"; Personal stable last string targetClass = "org.test.MyClass"; Public static zero master (string array []) throws exception {System.out.println ("Creating Classloader:" + httpURL); Url [] urls = {new url (httpURL)}; Last Classloader newClassLoader = New URL Class Loader (URL); Thread.currentThread () setContextClassLoader (newClassLoader). System.out.println ("Loading:" + targetClass); Class urlClass = tap; Try UrlClass = newClassLoader.loadClass (targetClass); } Grip (Klassnotfound Expression E) {e.printStackTrace (); Object obj = null; Try {obj = urlClass.newInstance (); } Hold (InstantiationException e) {e.printStackTrace (); } Hold (IllegalAccessException e) {e.printStackTrace (); } System.out.println ("The object is: \" "+ obj.toString () +" \ ""); }}
When I try to perform a snapet, I get ClassNotFoundException
.
In fact, I am not surprised at such an exception, but I need some explanation, whether or not I am trying to fulfill or: Local or remote JVM (running applications ) Load a class from ? If so, is there a need to change this URL httpURL = "http: // localhost: 8080 /"
or should I change the whole class?
BR
Comments
Post a Comment