Search code examples
javaant

Ant run failure to load main


I have oversimple Main.java file of next contents:

public class Main {
  public static void main(String[] args) {
    System.out.println("Hello, world!");
  }
}

The file was compiled by ant with next compile section in build.xml:

  <target name="compile">
    <mkdir dir="${build.dir}" />
    <javac srcdir="${src.dir}" destdir="${build.dir}" includeantruntime="false"/>
  </target>

Main.class file has been created in ./build directory. Changing directory to ./build and running"%JAVA_HOME%\bin\java.exe" Main brings correct output. However, running ant -v run from project root directory brungs next error:

run:
     [java] Executing 'C:\Program Files\Microsoft\jdk-11.0.12.7-hotspot\bin\java.exe' with arguments:
     [java] 'Main'
     [java]
     [java] The ' characters around the executable and arguments are
     [java] not part of the command.
     [java] Error: Could not find or load main class Main
     [java] Caused by: java.lang.ClassNotFoundException: Main
     [java] Java Result: 1

This is run section of build.xml:

  <target name="run" depends="compile">
    <java classname="Main" fork="true">
    </java>
  </target>

What is wrong?


Solution

  • @user16320675 was correct. This is a fix a build.xml run section fix:

    <target name="run" depends="build">
      <java classname="${main.class}" classpath="${classpath}:${build.dir}">
      </java>
    </target>