levanth levanth - 21 days ago 10
Java Question

Ant compile with javac Main Class not found

I already searched here for a solution but none worked for me:
I have a build script written in Ant:

<project name="Musifx" basedir="." default="main">
<property name="src.dir" value="src"/>

<property name="build.dir" value="out"/>
<property name="classes.dir" value="${build.dir}/classes"/>
<property name="jar.dir" value="${build.dir}/jar"/>
<property name="properties.file" value="${src.dir}/qs_musifx.properties"/>
<property name="lib.dir" value="lib"/>
<property name="main-class" value="quakk.musifx.main.Main"/>

<target name="clean">
<delete dir="${build.dir}"/>
</target>

<target name="compile">
<mkdir dir="${classes.dir}"/>
<javac includeantruntime="false" srcdir="${src.dir}" destdir="${classes.dir}" classpath="${lib.dir}/Logger.jar" includes="${lib.dir}/*.jar"/>
<copy todir="${classes.dir}">
<fileset dir="${src.dir}" excludes="**/*.java"/>
</copy>
</target>

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

<target name="clean-build" depends="clean,jar"/>

<target name="main" depends="clean,run"/>
</project>


My folder structure is ./quakk/musifx/main/ and my package is quakk.musifx.main

As I said, I tried various solutions posted here but every time I try to run my "main" target in Ant I get the following Error

Fehler: Hauptklasse quakk.musifx.main.Main konnte nicht gefunden oder geladen werden


Which translates roughly to Mainclass: quakk.musifx.main.Main could not be found or loaded.

I also tried to change the class path or the name of the Mainclass and tried to change my main-class property to simply "Main" or "quakk.musifx.main"
or to include the class path in the compile target but nothing worked for me.

By the way I am using JDK 8 and this is a JavaFX Application

Edit: Output of jar tvf Musifx.jar

0 Thu Jul 30 00:00:00 CEST 2015 META-INF/
139 Wed Jul 29 23:59:58 CEST 2015 META-INF/MANIFEST.MF
0 Wed Jul 29 23:59:58 CEST 2015 quakk/
0 Wed Jul 29 23:59:58 CEST 2015 quakk/musifx/
0 Wed Jul 29 23:59:58 CEST 2015 quakk/musifx/main/
425 Wed Jul 29 23:59:58 CEST 2015 qs_musifx.properties
1866 Wed Jul 29 23:59:58 CEST 2015 quakk/musifx/main/main.fxml
1076 Wed Jul 29 23:59:58 CEST 2015 quakk/musifx/main/styles.css

Answer

I just needed to change my targets for compile and jar:

<target name="compile">
<mkdir dir="${classes.dir}"/>
<javac includeantruntime="false" srcdir="${src.dir}" destdir="${classes.dir}">
    <classpath>
        <pathelement path="${lib.dir}/Logger.jar"/>
    </classpath>
</javac>
<copy todir="${classes.dir}">
  <fileset dir="${src.dir}" excludes="**/*.java"/>
</copy>
</target>

and the jar target

<target name="jar" depends="compile">
<mkdir dir="${jar.dir}"/>

<jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
    <zipgroupfileset dir="${lib.dir}" includes="Logger.jar" />
    <manifest>
        <attribute name="Main-Class" value="${main-class}"/>
    </manifest>
</jar>
</target>

Now it works and compiles to my jar file and the Application works as it should

Comments