private void setClassLoader ( String jarPath ) { if ( ! jarPath . endsWith ( ".jar" ) ) { throw new ParsingException ( "The jarPath received doesn't represent a jar file." ) ; } ClassLoader originalCl = Thread . currentThread ( ) . getContextClassLoader ( ) ; URL url = originalCl . getResource ( jarPath ) ; if ( url == null ) { try { url = new URL ( "file:/" + jarPath ) ; } catch ( MalformedURLException e ) { throw new ParsingException ( "Invalid jar name." ) ; } } // Create class loader using given codebase // Use prevCl as parent to maintain current visibility ClassLoader urlCl = URLClassLoader . newInstance ( new URL [ ] { url } , originalCl ) ; Thread . currentThread ( ) . setContextClassLoader ( urlCl ) ; classLoader = urlCl ; }
Creates a new class loader replacing the current one having another path added to the classpath . The new path is the path to the jar received in this class constructor .