Doc哪里有歧义了? addURL的方法注释是Appends the specified URL to the list of URLs to search for classes and resources.这里从来没有说明有支持子目录!你要清楚,这个URL未必就是一定是目录还可能是一个网络地址, 那么列举子目录可能是不被支持的。
This class loader is used to load classes and resources from a search path of URLs referring to both JAR files and directories. Any URL that ends with a '/' is assumed to refer to a directory. Otherwise, the URL is assumed to refer to a JAR file which will be opened as needed. 目录下面只能是普通文件,而不能是jar。你把jar解压以后,就可以了。have a try!
jar文件相当于一个文件夹
所以如果是加载文件夹,应该是相应的class文件在该文件夹内,而不是jar文件在该文件夹内
也就是说,加载文件夹,不会搜索该文件夹的jar,只会搜索该文件夹下的class文件
就好像classpath环境变量不能设置 xxxpath/*.jar一样,必须一个一个jar追加,所以只是设置xxxpath/,就只会搜索xxxpath下的class文件
addURL的方法注释是Appends the specified URL to the list of URLs to search for classes and resources.这里从来没有说明有支持子目录!你要清楚,这个URL未必就是一定是目录还可能是一个网络地址,
那么列举子目录可能是不被支持的。
jar中的class文件不一定在根目录,就如上面的commons-lang
追加一个jar就把org/apache/commons/lang/*.class添加到classpath里了?
这样也是ClassNotFoundException
抱歉,是我的错,怎么把commons-beanutils当成commons-lang了我把commons-lang解压后放到lib上,就可以了明白了,
根是lib
子目录是:org/apache/commons/lang/
文件是:StringUtils所以类找到了
"jar文件相当于一个文件夹"
要把jar当作文件夹看
多谢指点
但是明显是不可能加载org.apache.commons.lang.StringUtils这个类,因为在lib目录中不存在
org\apache\commons\lang\StringUtils.class文件!URLClassLoader不会试图从commons-lang-2.6.jar中加载的,因为你只向URLClassLoader中加入了lib目录,而不是commons-lang-2.6.jar
URLClassLoader把jar当作一般文件看待