运行 java -jar 命令时出错
我使用以下 ANT 脚本创建了一个 jar 文件
<manifestclasspath property="jar.classpath" jarfile="${bin.dir}/${jar.app.name}" maxparentlevels="0">
<classpath refid="main.class.path" />
</manifestclasspath>
<target name="jar">
<mkdir dir="${build.dir}/lib/isp"/>
<mkdir dir="${build.dir}/lib/jasper"/>
<copy todir="${build.dir}/lib/jasper">
<fileset dir="${lib.jasper.dir}">
<include name="**/*.jar" />
</fileset>
</copy>
<copy todir="${build.dir}/lib/isp">
<fileset dir="${lib.isp.dir}">
<include name="**/*.jar" />
</fileset>
</copy>
<jar jarfile="${bin.dir}/${jar.app.name}"
index="true"
basedir="${classes.dir}" excludes="lib/mytest.jar "
>
<manifest>
<attribute name="Main-Class" value="${main.class}" />
<attribute name="Class-Path" value="${jar.classpath}" />
</manifest>
</jar>
</target>
生成的 jar 文件具有以下 MANIFEST.MF 条目。
Main-Class: dm.jb.Main
Class-Path: lib/isp/OfficeLnFs_2.2.jar lib/isp/RXTXcomm.jar lib/isp/ba
rbecue-1.0.6d.jar lib/isp/commons-logging-1.1.jar lib/isp/forms-1.0.5
.jar lib/isp/gnujaxp.jar lib/isp/helpUI.jar lib/isp/inspInstaller.jar
lib/isp/itext-2.0.1.jar lib/isp/itext-2.0.2.jar lib/isp/jcalendar-1.
3.2.jar lib/isp/jcl.jar lib/isp/jcommon-1.0.10.jar lib/isp/jcommon-1.
0.9.jar lib/isp/jdnc-0_7-all.jar lib/isp/jdnc-runner.jar lib/isp/jdom
.jar lib/isp/jfreechart-1.0.6.jar lib/isp/jlfgr-1_0.jar lib/isp/junit
.jar lib/isp/log4j-1.2.9.jar lib/isp/looks-1.3.2.jar lib/isp/msbase.j
ar lib/isp/mssqlserver.jar lib/isp/msutil.jar lib/isp/mysql-connector
当我尝试运行命令时 java -jar mytest.jar
,它失败并抛出错误,指出 dm.jb.Main 未找到。但我可以通过指定类路径 java -classpath dm.jb.Main 来运行该类,
请帮助我 DM
I created a jar file using the following ANT script
<manifestclasspath property="jar.classpath" jarfile="${bin.dir}/${jar.app.name}" maxparentlevels="0">
<classpath refid="main.class.path" />
</manifestclasspath>
<target name="jar">
<mkdir dir="${build.dir}/lib/isp"/>
<mkdir dir="${build.dir}/lib/jasper"/>
<copy todir="${build.dir}/lib/jasper">
<fileset dir="${lib.jasper.dir}">
<include name="**/*.jar" />
</fileset>
</copy>
<copy todir="${build.dir}/lib/isp">
<fileset dir="${lib.isp.dir}">
<include name="**/*.jar" />
</fileset>
</copy>
<jar jarfile="${bin.dir}/${jar.app.name}"
index="true"
basedir="${classes.dir}" excludes="lib/mytest.jar "
>
<manifest>
<attribute name="Main-Class" value="${main.class}" />
<attribute name="Class-Path" value="${jar.classpath}" />
</manifest>
</jar>
</target>
The resulting jar file has the following MANIFEST.MF entry.
Main-Class: dm.jb.Main
Class-Path: lib/isp/OfficeLnFs_2.2.jar lib/isp/RXTXcomm.jar lib/isp/ba
rbecue-1.0.6d.jar lib/isp/commons-logging-1.1.jar lib/isp/forms-1.0.5
.jar lib/isp/gnujaxp.jar lib/isp/helpUI.jar lib/isp/inspInstaller.jar
lib/isp/itext-2.0.1.jar lib/isp/itext-2.0.2.jar lib/isp/jcalendar-1.
3.2.jar lib/isp/jcl.jar lib/isp/jcommon-1.0.10.jar lib/isp/jcommon-1.
0.9.jar lib/isp/jdnc-0_7-all.jar lib/isp/jdnc-runner.jar lib/isp/jdom
.jar lib/isp/jfreechart-1.0.6.jar lib/isp/jlfgr-1_0.jar lib/isp/junit
.jar lib/isp/log4j-1.2.9.jar lib/isp/looks-1.3.2.jar lib/isp/msbase.j
ar lib/isp/mssqlserver.jar lib/isp/msutil.jar lib/isp/mysql-connector
When I try to run the commandjava -jar mytest.jar
, it fails and throws error saying dm.jb.Main not found. But I could run the class by specifying the classpath java -classpath dm.jb.Main
Please help me
DM
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果你像这样运行你的jar,
那么java将忽略你提供的所有类路径参数,并尝试在清单中使用给定的类路径查找它的资源。但这些路径指向您的本地文件系统,而不是 jar 内部。因此,您必须在可以在 lib/isp/... 找到您的库的目录中启动 jar。
If you run your jar like this
then java will ignore all classpath parameters you give and try to find it's resources with the given classpath inside the manifest. But these pathes point to your local file system and not inside the jar. So you have to start the jar in a directory where your libs can be found at lib/isp/...