code_junkie

Why System.setProperty() cannot change the classpath at run time !

Nov 12th, 2011
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. import java.lang.reflect.InvocationTargetException;
  2. import java.lang.reflect.Method;
  3.  
  4. import java.io.File;
  5. import java.io.IOException;
  6.  
  7. import java.net.URL;
  8. import java.net.URLClassLoader;
  9.  
  10. /**
  11. * Allows programs to modify the classpath during runtime.
  12. */
  13. public class ClassPathUpdater {
  14. /** Used to find the method signature. */
  15. private static final Class[] PARAMETERS = new Class[]{ URL.class };
  16.  
  17. /** Class containing the private addURL method. */
  18. private static final Class<?> CLASS_LOADER = URLClassLoader.class;
  19.  
  20. /**
  21. * Adds a new path to the classloader. If the given string points to a file,
  22. * then that file's parent file (i.e., directory) is used as the
  23. * directory to add to the classpath. If the given string represents a
  24. * directory, then the directory is directly added to the classpath.
  25. *
  26. * @param s The directory to add to the classpath (or a file, which
  27. * will relegate to its directory).
  28. */
  29. public static void add( String s )
  30. throws IOException, NoSuchMethodException, IllegalAccessException,
  31. InvocationTargetException {
  32. add( new File( s ) );
  33. }
  34.  
  35. /**
  36. * Adds a new path to the classloader. If the given file object is
  37. * a file, then its parent file (i.e., directory) is used as the directory
  38. * to add to the classpath. If the given string represents a directory,
  39. * then the directory it represents is added.
  40. *
  41. * @param f The directory (or enclosing directory if a file) to add to the
  42. * classpath.
  43. */
  44. public static void add( File f )
  45. throws IOException, NoSuchMethodException, IllegalAccessException,
  46. InvocationTargetException {
  47. f = f.isDirectory() ? f : f.getParentFile();
  48. add( f.toURI().toURL() );
  49. }
  50.  
  51. /**
  52. * Adds a new path to the classloader. The class must point to a directory,
  53. * not a file.
  54. *
  55. * @param url The path to include when searching the classpath.
  56. */
  57. public static void add( URL url )
  58. throws IOException, NoSuchMethodException, IllegalAccessException,
  59. InvocationTargetException {
  60. Method method = CLASS_LOADER.getDeclaredMethod( "addURL", PARAMETERS );
  61. method.setAccessible( true );
  62. method.invoke( getClassLoader(), new Object[]{ url } );
  63. }
  64.  
  65. private static URLClassLoader getClassLoader() {
  66. return (URLClassLoader)ClassLoader.getSystemClassLoader();
  67. }
  68. }
  69.  
  70. try {
  71. ClassPathUpdater.add( "/home/user/dev/java/app/build/com/package/Filename.class" );
  72. }
  73. catch( Exception e ) {
  74. e.printStackTrace();
  75. }
  76.  
  77. /*
  78. Add the URL handler to the handler property. This informs
  79. IBMJSSE what URL handler to use to handle the safkeyring
  80. support. In this case IBMJCE.
  81. */
  82. System.setProperty("java.protocol.handler.pkgs", "com.ibm.crypto.provider");
  83.  
  84. System.setProperty("javax.net.ssl.keyStore", context.getRealPath(KEYSTORE));
  85. System.setProperty("javax.net.ssl.keyStorePassword", "password");
  86. System.setProperty("javax.net.ssl.trustStore", context.getRealPath(TRUSTSTORE));
  87. System.setProperty("javax.net.debug", "ssl");
  88. HttpClient httpClient = new HttpClient();
  89. GetMethod httpGet = new GetMethod("https://something.com");
  90. httpClient.executeMethod(httpGet);
  91. return new String(httpGet.getResponseBody());
Add Comment
Please, Sign In to add comment