run a jar file (generated by ant) in command line
Asked Answered
J

4

1

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'

How can I run the jar file? running with

java -jar Main.jar main.Main 

gives me:

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

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>

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

Jingoism answered 21/3, 2012 at 14:25 Comment(0)
C
4

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.

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.

Check answered 21/3, 2012 at 14:28 Comment(1)
Exactly: java -cp lib/lib1.jar:lib/lib2.jar:....:Main.jar main.Main solves - Thank youJingoism
N
0

Have you tried:

java -jar Main.jar

Have you checked that Main.jar does in fact include your org/neo4j/graphdb/GraphDatabaseService class?

Nonobservance answered 21/3, 2012 at 14:27 Comment(2)
Hi casey, gives me the same error (other .jar missed): java.lang.NoClassDefFoundError: org/neo4j/graphdb/GraphDatabaseService ThanksJingoism
org/neo4j/graphdb/GraphDatabaseService is another jar file. Problem Solved with java -cp lib/lib1.jar:lib/lib2.jar:....:Main.jar main.Main. Thank youJingoism
L
0

The neo4j.jars are missing at runtime. Add a classpath parameter like:

java -cp yourLibDir -jar Main.jar
Lactose answered 21/3, 2012 at 14:28 Comment(3)
It should be java [-options] class or java [-options] -jar jarfile, not both (see java -help). In addition, you need to enumerate the jars in the lib directory one by one.Check
Yes -cp shuold be right before -jar jarfile. But in my Java help it says: " -cp <class search path of directories and zip/jar files>".Lactose
You had both -jar Main.jar and main.Main, that was the issue. Regarding the jars on the classpath, if you've not experienced the pain of having to add each jar one by one when you run an application on the command line, you haven't done enough Java yet ;-) or you've been able to use the right tools (e.g. ant). The help is mean as "directories of classes" and "zip/jar" files, separately.Check
C
0

You need to add -classpath xxx.jar in your command line

Cabretta answered 21/3, 2012 at 14:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.