`
beneo
  • 浏览: 54381 次
  • 性别: Icon_minigender_1
  • 来自: 希伯來
社区版块
存档分类
最新评论

关于classpath wildcard

    博客分类:
  • java
阅读更多
在java 1.5的时候,我还得写bash脚本去loop获得所有的jar,然后写到classpath下面,现在换到1.6,发现有个classpath wildcard,原来classpath也有通配符

这里

引用

    Class path entries can contain the basename wildcard character *, which is considered equivalent to specifying a list of all the files in the directory with the extension .jar or .JAR. For example, the class path entry foo/* specifies all JAR files in the directory named foo. A classpath entry consisting simply of * expands to a list of all the jar files in the current directory.

    A class path entry that contains * will not match class files. To match both classes and JAR files in a single directory foo, use either foo;foo/* or foo/*;foo. The order chosen determines whether the classes and resources in foo are loaded before JAR files in foo, or vice versa.

    Subdirectories are not searched recursively. For example, foo/* looks for JAR files only in foo, not in foo/bar, foo/baz, etc.

    The order in which the JAR files in a directory are enumerated in the expanded class path is not specified and may vary from platform to platform and even from moment to moment on the same machine. A well-constructed application should not depend upon any particular order. If a specific order is required then the JAR files can be enumerated explicitly in the class path.

    Expansion of wildcards is done early, prior to the invocation of a program's main method, rather than late, during the class-loading process itself. Each element of the input class path containing a wildcard is replaced by the (possibly empty) sequence of elements generated by enumerating the JAR files in the named directory. For example, if the directory foo contains a.jar, b.jar, and c.jar, then the class path foo/* is expanded into foo/a.jar;foo/b.jar;foo/c.jar, and that string would be the value of the system property java.class.path.

    The CLASSPATH environment variable is not treated any differently from the -classpath (or -cp) command-line option. That is, wildcards are honored in all these cases. However, class path wildcards are not honored in the Class-Path jar-manifest header.



那么稍微翻译一下,假设lib是你的依赖
1. 如果a.jar, b.jar, c.jar都在lib下面,你可以 -cp lib/* Main 来把lib下面所有的jar放到cp下
2. 如果a.class, b.jar, c.jar都在lib下面,你可以 -cp lib:lib/* Main 来把lib下面所有的class文件和jar包放到cp下面
3. 如果lib/foo/a.jar,那么lib/*是不会获得a.jar的,因为通配符不会递归
4. 要注意的是,在classloader作用前,/*这种通配符就已经被扩展了;此外,不要让程序对jar包的加载顺序有要求。

have fun....
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics