java中在类里面添加name_是否有可能在Java中dynamic添加到类path中?

您可以使用java.net.URLClassLoader来加载您想要的任何程序定义的URL列表的类:

公共类URLClassLoader扩展SecureClassLoader

这个类加载器用于从指向JAR文件和目录的URL的searchpath加载类和资源。 任何以“/”结尾的URL都被认为是指向一个目录。 否则,假定该URL是指将根据需要打开的JAR文件。

随后加载类和资源时将使用创buildURLClassLoader实例的线程的AccessControlContext。

加载的类默认授予权限,只能访问创buildURLClassLoader时指定的URL。

由于:1.2

而一些奇特的步法可以扩展它以支持使用通配符path名来拾取整个JAR目录(这个代码有一些对实用方法的引用,但是它们的实现在上下文中应该是显而易见的):

/** * Add classPath to this loader's classpath. *

* The classpath may contain elements that include a generic file base name. A generic basename * is a filename without the extension that may begin and/or end with an asterisk. Use of the * asterisk denotes a partial match. Any files with an extension of ".jar" whose base name match * the specified basename will be added to this class loaders classpath. The case of the filename is ignored. * For example "/somedir/*abc" means all files in somedir that end with "abc.jar", "/somedir/abc*" * means all files that start with "abc" and end with ".jar", and "/somedir/*abc*" means all files * that contain "abc" and end with ".jar". * */ public void addClassPath(String cp) { String seps=File.pathSeparator; // separators if(!File.pathSeparator.equals(";")) { seps+=";"; } // want to accept both system separator and ';' for(StringTokenizer st=new StringTokenizer(cp,seps,false); st.hasMoreTokens(); ) { String pe=st.nextToken(); File fe; String bn=null; if(pe.length()==0) { continue; } fe=new File(pe); if(fe.getName().indexOf('*')!=-1) { bn=fe.getName(); fe=fe.getParentFile(); } if(!fe.isAbsolute() && pe.charAt(0)!='/' && pe.charAt(0)!='\\') { fe=new File(rootPath,fe.getPath()); } try { fe=fe.getCanonicalFile(); } catch(IOException thr) { log.diagln("Skipping non-existent classpath element '"+fe+"' ("+thr+")."); continue; } if(!GenUtil.isBlank(bn)) { fe=new File(fe,bn); } if(classPathElements.contains(fe.getPath())) { log.diagln("Skipping duplicate classpath element '"+fe+"'."); continue; } else { classPathElements.add(fe.getPath()); } if(!GenUtil.isBlank(bn)) { addJars(fe.getParentFile(),bn); } else if(!fe.exists()) { // s/never be due getCanonicalFile() above log.diagln("Could not find classpath element '"+fe+"'"); } else if(fe.isDirectory()) { addURL(createUrl(fe)); } else if(fe.getName().toLowerCase().endsWith(".zip") || fe.getName().toLowerCase().endsWith(".jar")) { addURL(createUrl(fe)); } else { log.diagln("ClassPath element '"+fe+"' is not an existing directory and is not a file ending with '.zip' or '.jar'"); } } log.diagln("Class loader is using classpath: \""+classPath+"\"."); } /** * Adds a set of JAR files using a generic base name to this loader's classpath. See @link:addClassPath(String) for * details of the generic base name. */ public void addJars(File dir, String nam) { String[] jars; // matching jar files if(nam.endsWith(".jar")) { nam=nam.substring(0,(nam.length()-4)); } if(!dir.exists()) { log.diagln("Could not find directory for Class Path element '"+dir+File.separator+nam+".jar'"); return; } if(!dir.canRead()) { log.error("Could not read directory for Class Path element '"+dir+File.separator+nam+".jar'"); return; } FileSelector fs=new FileSelector(true).add("BaseName","EG",nam,true).add("Name","EW",".jar",true); if((jars=dir.list(fs))==null) { log.error("Error accessing directory for Class Path element '"+dir+File.separator+nam+".jar'"); } else if(jars.length==0) { log.diagln("No JAR files match specification '"+new File(dir,nam)+".jar'"); } else { log.diagln("Adding files matching specification '"+dir+File.separator+nam+".jar'"); Arrays.sort(jars,String.CASE_INSENSITIVE_ORDER); for(int xa=0; xa0) { classPath+=File.pathSeparator; } this.classPath+=fe.getPath(); return url; } catch(MalformedURLException thr) { log.diagln("Classpath element '"+fe+"' could not be used to create a valid file system URL"); return null; } }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值