惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园_首页
T
Threat Research - Cisco Blogs
GbyAI
GbyAI
Y
Y Combinator Blog
美团技术团队
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - 【当耐特】
S
SegmentFault 最新的问题
IT之家
IT之家
Recent Announcements
Recent Announcements
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
阮一峰的网络日志
阮一峰的网络日志
T
The Blog of Author Tim Ferriss
Martin Fowler
Martin Fowler
Microsoft Azure Blog
Microsoft Azure Blog
V
Visual Studio Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
U
Unit 42
WordPress大学
WordPress大学
博客园 - Franky
L
LangChain Blog
人人都是产品经理
人人都是产品经理
小众软件
小众软件
博客园 - 叶小钗
罗磊的独立博客
酷 壳 – CoolShell
酷 壳 – CoolShell
大猫的无限游戏
大猫的无限游戏
云风的 BLOG
云风的 BLOG
Vercel News
Vercel News
雷峰网
雷峰网
腾讯CDC
Google DeepMind News
Google DeepMind News
博客园 - 三生石上(FineUI控件)
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Help Net Security
Help Net Security
C
Check Point Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
N
News and Events Feed by Topic
V2EX - 技术
V2EX - 技术
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Schneier on Security
Schneier on Security
博客园 - 聂微东
A
Arctic Wolf
H
Heimdal Security Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Recent Commits to openclaw:main
Recent Commits to openclaw:main
T
The Exploit Database - CXSecurity.com
C
Cyber Attacks, Cyber Crime and Cyber Security
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Google DeepMind News
Google DeepMind News

博客园 - Mark Jiao

调试Java程序持续占cpu问题 Freemarker notes | Freemarker学习笔记 Spring Struts Hibernate trouble shooting | 一些问题的记载 MySQL 性能调优之查询优化 Tomcat性能调优 | Tomcat Performance Tuning Tomcat配置、管理和问题解决 | Tomcat Configuration, Manangement and Trouble Shooting SWT Browser & XULRunner Windows应用安装制作工具调查报告 CentOS Configuration | CentOS配置 命令行加载IE ActiveX插件 互联网技术要考虑的事情 Parameterized Testing | 参数化测试 Git usage | Git 使用 Introduce products I worked on | 介绍一下我做过的产品 PHPUnit manual note | PHPUnit手册笔记 PHP manual notes | PHP手册笔记 Globalization, Localization, Internationalization and Translation HTTP 超文本传输协议 Design Patter
Java那些事儿
Mark Jiao · 2015-01-30 · via 博客园 - Mark Jiao

How Classes are Found

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:

  • Bootstrap classes - Classes that comprise the Java platform, including the classes in rt.jar and several other important jar files.
  • Extension classes - Classes that use the Java Extension mechanism. These are bundled as .jar files located in the extensions directory.
  • User classes - Classes defined by developers and third parties that do not take advantage of the extension mechanism. You identify the location of these classes using the -classpath option on the command line (the preferred method) or by using the CLASSPATH environment variable.

动态加载类库

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 Cryptography Extension (JCE)

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编码转换、十六进制编码转换等工具。

Oracle JDK/JRE JCE

下载的压缩包中仅有一个目录,也就是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下。

JCE in openjdk

http://openjdk.java.net/groups/security/

Cryptographic Cautions

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.