是否有办法在classpath中包括一个目录内的所有jar文件?
我正在尝试java -classpath lib/*.jar:. my.package.Program
,但它无法找到肯定在这些jar中的类文件。 我是否需要将每个jar文件分别添加到classpath中?
如果你真的需要动态地指定所有的.jar文件,你可以使用shell脚本,或者Apache Ant。有一个叫做Commons Launcher的共享项目,它基本上可以让你把你的启动脚本指定为一个Ant构建文件(如果你明白我的意思)。
然后,你可以指定类似的东西。
<path id="base.class.path">
<pathelement path="${resources.dir}"/>
<fileset dir="${extensions.dir}" includes="*.jar" />
<fileset dir="${lib.dir}" includes="*.jar"/>
</path>
在你的启动构建文件中,这将以正确的classpath启动你的应用程序。