本文介绍了在命令行中运行一个 jar 文件(由 ant 生成)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可以正常工作的 build.xml 文件.问题是生成的jar文件,我需要在没有'ant run'的情况下运行它

I have a build.xml file that works fine.The problem is that the generated jar file, and I need to run it without 'ant run'

如何运行 jar 文件?运行

How can I run the jar file?running with

java -jar Main.jar main.Main

给我:

Exception in thread "main" java.lang.NoClassDefFoundError: org/neo4j/graphdb/GraphDatabaseService

这是我创建 jar 文件 (build.xml) 的方式:

This is how I'm creating the jar file (build.xml):

<target name="jar" depends="compile">
<mkdir dir="${jar.dir}"/>
    <jar destfile= "${jar.dir}/${ant.project.name}.jar" basedir="${build.dir}">
        <manifest>
            <attribute name="Main-Class" value="${main-class}"/>
        </manifest>
    </jar>
</target>

<target name="compile">
    <mkdir dir="${build.dir}"/>
    <mkdir dir="${build.dir}/${conf.dir}"/>
    <javac srcdir="${src.dir}" destdir="${build.dir}" classpathref="classpath" includeantruntime="false"/>
    <copy todir="${build.dir}/${conf.dir}">
        <fileset dir="${conf.dir}"/>
    </copy>
</target>

<target name="run" >
    <java fork="true" classname="${main-class}">
        <classpath>
            <path refid="classpath"/>
            <path location="${jar.dir}/${ant.project.name}.jar"/>
        </classpath>
    </java>
</target>

<path id="classpath">
    <fileset dir="${lib.dir}" includes="**/*.jar"/>
</path>

ant run"任务在这个 jar 中运行良好.我如何在没有蚂蚁的情况下运行这个项目?

The "ant run" task works fine with this jar.How can I run this project without ant?

推荐答案

使用 java -cp Main.jar main.Main-jar 用于捆绑的 jars清单说明将哪个类用作主类.您对 -cp(以及在您的 ant 构建文件中)所做的只是将它放在主路径上:您将 main.Main 指定为主类明确地,不在清单中.

Use java -cp Main.jar main.Main, -jar is for bundled jars with a Manifest saying which class is to be used as the main class. What you're doing with -cp (and within your ant build file) is just putting it on the main path: you're specifying main.Main as the main class explicitly, not within a manifest.

您还应该将其他 jar 放在类路径上(例如 -cp lib/example1.jar:lib/example2.jar:Main.jar).根据与 **/*.jar 匹配的内容,可能有很多.

You should also put the other jars on the classpath (e.g. -cp lib/example1.jar:lib/example2.jar:Main.jar). Depending on what matches **/*.jar, there may be a number of them.

这篇关于在命令行中运行一个 jar 文件(由 ant 生成)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 05:24