11package scalive ;
22
33import java .io .File ;
4+ import java .lang .reflect .InvocationTargetException ;
45import java .lang .reflect .Method ;
6+ import java .net .MalformedURLException ;
57import java .net .URL ;
68import java .net .URLClassLoader ;
79import java .util .Arrays ;
810
911public class Classpath {
10- private static Method addURL = getAddURL ();
12+ private static final Method addURL = getAddURL ();
1113
1214 // http://stackoverflow.com/questions/8222976/why-urlclassloader-addurl-protected-in-java
1315 private static Method getAddURL () {
1416 try {
1517 Method method = URLClassLoader .class .getDeclaredMethod ("addURL" , URL .class );
1618 method .setAccessible (true );
1719 return method ;
18- } catch (Exception e ) {
19- e .printStackTrace ();
20- return null ;
20+ } catch (NoSuchMethodException e ) {
21+ throw new RuntimeException (e );
2122 }
2223 }
2324
@@ -30,7 +31,7 @@ private static Method getAddURL() {
3031 *
3132 * @param jarPrefix JAR file name prefix to search; example: "scalive-agent" will match "scalive-agent-xxx.jar"
3233 */
33- public static String findJar (String [] jarSearchDirs , String jarPrefix ) throws Exception {
34+ public static String findJar (String [] jarSearchDirs , String jarPrefix ) throws IllegalStateException {
3435 String maxBaseName = null ;
3536 File maxFile = null ;
3637 for (String jarSearchDir : jarSearchDirs ) {
@@ -50,22 +51,26 @@ public static String findJar(String[] jarSearchDirs, String jarPrefix) throws Ex
5051 }
5152
5253 if (maxFile == null )
53- throw new Exception ("Could not find " + jarPrefix + " in " + join (jarSearchDirs , File .pathSeparator ));
54+ throw new IllegalStateException ("Could not find " + jarPrefix + " in " + join (jarSearchDirs , File .pathSeparator ));
5455 else
5556 return maxFile .getPath ();
5657 }
5758
58- public static void addPath (URLClassLoader cl , String path ) throws Exception {
59+ public static void addPath (
60+ URLClassLoader cl , String path
61+ ) throws MalformedURLException , InvocationTargetException , IllegalAccessException {
5962 URL url = new File (path ).toURI ().toURL ();
6063 URL [] urls = cl .getURLs ();
6164 if (!Arrays .asList (urls ).contains (url )) addURL .invoke (cl , url );
6265 }
6366
6467 /** Combination of {@link #findJar(String[], String)} and {@link #addPath(URLClassLoader, String)}. */
65- public static void findAndAddJar (URLClassLoader cl , String [] jarSearchDirs , String jarPrefix ) throws Exception {
68+ public static void findAndAddJar (
69+ URLClassLoader cl , String [] jarSearchDirs , String jarPrefix
70+ ) throws IllegalAccessException , InvocationTargetException , MalformedURLException {
6671 String jar = findJar (jarSearchDirs , jarPrefix );
6772 addPath (cl , jar );
68- System . out . println ( "[Scalive] Load " + jar );
73+ Log . log ( " Load " + jar );
6974 }
7075
7176 /**
@@ -74,7 +79,7 @@ public static void findAndAddJar(URLClassLoader cl, String[] jarSearchDirs, Stri
7479 */
7580 public static void findAndAddJar (
7681 URLClassLoader cl , String representativeClass , String [] jarSearchDirs , String jarPrefix
77- ) throws Exception {
82+ ) throws IllegalAccessException , MalformedURLException , InvocationTargetException {
7883 try {
7984 Class .forName (representativeClass , true , cl );
8085 } catch (ClassNotFoundException e ) {
@@ -88,7 +93,9 @@ public static String getClasspath(URLClassLoader cl) {
8893 return join (urls , File .pathSeparator );
8994 }
9095
91- public static String getScalaVersion (ClassLoader cl ) throws Exception {
96+ public static String getScalaVersion (
97+ ClassLoader cl
98+ ) throws ClassNotFoundException , NoSuchMethodException , InvocationTargetException , IllegalAccessException {
9299 Class <?> k = Class .forName ("scala.util.Properties" , true , cl );
93100 Method m = k .getDeclaredMethod ("versionNumberString" );
94101 return (String ) m .invoke (k );
0 commit comments