

























http://docs.oracle.com/javase/8/docs/technotes/tools/findingclasses.html
The Java launcher, java, initiates the Java virtual machine. The virtual machine searches for and loads classes in this order:
rt.jar and several other important jar files..jar files located in the extensions directory.
package cp; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.net.MalformedURLException; import java.net.URL; import java.net.URLClassLoader; public class AddJar2ClassPath { //此方法虽然改变了classpath,但是不能load Jar用于当前classloader private void updateClassPath(){ Classpath classPath = new Classpath(System.getProperty("java.class.path")); System.out.println(System.getProperty("java.class.path")); classPath.addClasspath(System.getProperty("user.dir")+"/lib/activemq-all-5.10.0.jar"); System.setProperty("java.class.path", classPath.toString()); System.out.println(System.getProperty("java.class.path")); ClassLoader classloader = classPath.getClassLoader(); Thread.currentThread().setContextClassLoader(classloader); } //此方法load Jar后可以用于当前classloader private void loadJar(){ URL urls; try { urls = new URL("file:/"+System.getProperty("user.dir")+"/lib/activemq-all-5.10.0.jar"); //GetPI.class URLClassLoader urlLoader = (URLClassLoader) ClassLoader.getSystemClassLoader(); Class<URLClassLoader> sysclass = URLClassLoader.class; Method method = sysclass.getDeclaredMethod("addURL", new Class[]{URL.class}); method.setAccessible(true); method.invoke(urlLoader, urls); urls = new URL("file:/"+System.getProperty("user.dir")+"/lib/images.jar"); method.invoke(urlLoader, urls); urls = new URL("file:/"+System.getProperty("user.dir")+"/lib/poi-3.11-20141221.jar"); method.invoke(urlLoader, urls); } catch (MalformedURLException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } }
两种方法

package cp; import java.io.File; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.net.URLClassLoader; import java.util.StringTokenizer; import java.util.Vector; /** * Class to handle CLASSPATH construction */ public class Classpath { Vector _elements = new Vector(); public Classpath() {} public Classpath(String initial) { addClasspath(initial); } public boolean addComponent(String component) { if ((component != null) && (component.length() > 0)) { try { File f = new File(component); if (f.exists()) { File key = f.getCanonicalFile(); if (!_elements.contains(key)) { _elements.add(key); return true; } } } catch (IOException e) {} } return false; } public boolean addComponent(File component) { if (component != null) { try { if (component.exists()) { File key = component.getCanonicalFile(); if (!_elements.contains(key)) { _elements.add(key); return true; } } } catch (IOException e) {} } return false; } public boolean addClasspath(String s) { boolean added = false; if (s != null) { StringTokenizer t = new StringTokenizer(s, File.pathSeparator); while (t.hasMoreTokens()) { added |= addComponent(t.nextToken()); } } return added; } public String toString() { StringBuffer cp = new StringBuffer(1024); int cnt = _elements.size(); if (cnt >= 1) { cp.append(((File) (_elements.elementAt(0))).getPath()); } for (int i = 1; i < cnt; i++) { cp.append(File.pathSeparatorChar); cp.append(((File) (_elements.elementAt(i))).getPath()); } return cp.toString(); } public URL[] getUrls() { int cnt = _elements.size(); URL[] urls = new URL[cnt]; for (int i = 0; i < cnt; i++) { try { urls[i] = ((File) (_elements.elementAt(i))).toURL(); } catch (MalformedURLException e) {} } return urls; } public ClassLoader getClassLoader() { URL[] urls = getUrls(); ClassLoader parent = Thread.currentThread().getContextClassLoader(); if (parent == null) { parent = Classpath.class.getClassLoader(); } if (parent == null) { parent = ClassLoader.getSystemClassLoader(); } return new URLClassLoader(urls, parent); } }
class Classpath

private void loadJar(){ try { URLClassLoader urlLoader = (URLClassLoader) ClassLoader.getSystemClassLoader(); Class<URLClassLoader> sysclass = URLClassLoader.class; Method method = sysclass.getDeclaredMethod("addURL", new Class[]{URL.class}); method.setAccessible(true); //TODO 如果Jar不在classpath里面才添加 //System.out.println(System.getProperty("java.class.path")); String classpath=System.getProperty("java.class.path"); String libDir=System.getProperty("user.dir")+"/lib"; File libDirFile=new File(libDir); if(libDirFile.isDirectory()){ File[] libs=libDirFile.listFiles(new FileFilter() { public boolean accept(File file) { if (file.getName().endsWith(".jar")) { return true; } return false; } }); URL urls; if(libs!=null&&classpath!=null) for(int i=0;i<libs.length;i++){ if(!classpath.contains(libs[i].getName())){ System.out.println("Libarary is not found in classpath:"+libs[i].getName()); urls = new URL("file:/"+libDir+"/"+libs[i].getName()); method.invoke(urlLoader, urls); } } else System.out.println("检查并加载类包:"+libDir+"目录为null或者classpath为null!"); } // URL urls = new URL("file:/"+System.getProperty("user.dir")+"/lib/activemq-all-5.10.0.jar"); // method.invoke(urlLoader, urls); // // urls = new URL("file:/"+System.getProperty("user.dir")+"/lib/images.jar"); // method.invoke(urlLoader, urls); // // urls = new URL("file:/"+System.getProperty("user.dir")+"/lib/poi-3.11-20141221.jar"); // method.invoke(urlLoader, urls); } catch (MalformedURLException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } }
动态加载类库
Java几乎各种常用加密算法都能找到对应的实现。因为美国的出口限制,Sun通过权限文件(local_policy.jar、US_export_policy.jar)做了相应限制。
因为某些国家的进口管制限制,Java发布的运行环境包中的加解密有一定的限制。比如默认不允许256位密钥的AES加解密,解决方法就是修改策略文件。
因此存在一些问题:
●密钥长度上不能满足需求(如:java.security.InvalidKeyException: Illegal key size or default parameters);
●部分算法未能支持,如MD4、SHA-224等算法;
●API使用起来还不是很方便;一些常用的进制转换辅助工具未能提供,如Base64编码转换、十六进制编码转换等工具。
下载的压缩包中仅有一个目录,也就是jce目录。该目录中包含了4个文件:README.txt、COPYRIGHT.html、local_policy.jar和US_export_policy.jar。其中包含的两个jar文件正是此次配置中用到的文件。
http://www.oracle.com/technetwork/java/javase/downloads/jce-6-download-429243.html
http://www.oracle.com/technetwork/java/javase/downloads/jce-7-download-432124.html
http://www.oracle.com/technetwork/java/javase/downloads/jce8-download-2133166.htm
如果安装了JRE,将两个jar文件放到%JRE_HOME%\lib\security下覆盖原来文件,记得先备份。
如果安装了JDK,将两个jar文件也放到%JDK_HOME%\jre\lib\security下。
http://openjdk.java.net/groups/security/
Anyone who has worked in cryptography knows the import/export of cryptographic code involves complicated legal issues. The JCE in OpenJDK has an open cryptographic interface, meaning it does not restrict which providers can be used.Compliance with United States export controls and with local law governing the import/export of products incorporating the JCE in the OpenJDK is the responsibility of the licensee.
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。