【问题标题】:Cannot compile in ANT, but compiles in Eclipse无法在 ANT 中编译,但在 Eclipse 中编译
【发布时间】:2015-05-18 21:55:47
【问题描述】:

我是第一次使用 ANT 并开发了 KnightsTour 程序的淡化版本。我第一次在 Eclipse 中开发没有问题,我正在尝试在 ANT 中编译,但没有成功。我没有对这两个类进行任何更改,但 JAVAC 编译器指出我的一个类 AVAILABLELOCATIONS 已重复。

这是编译器错误。

    [javac] C:\Users\Admiral Sudoku\Documents\Eduboard\Ken Ton\build.xml:19: war
ning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; se
t to false for repeatable builds
    [javac] Compiling 2 source files to C:\Users\Admiral Sudoku\Documents\Eduboa
rd\Ken Ton\build\classes
    [javac] C:\Users\Admiral Sudoku\Documents\Eduboard\Ken Ton\src\Knightstour\A
vailableLocations.java:1: error: duplicate class: AvailableLocations
    [javac] public class AvailableLocations {
    [javac]        ^
    [javac] C:\Users\Admiral Sudoku\Documents\Eduboard\Ken Ton\src\Knightstour\m
ainclass.java:6: error: cannot access AvailableLocations
    [javac]    public static Vector checkmovesavailable (int[][] board, Availabl
eLocations knightlocation, int x, int y)
    [javac]                                                             ^
    [javac]   bad source file: C:\Users\Admiral Sudoku\Documents\Eduboard\Ken To
n\src\Knightstour\AvailableLocations.java
    [javac]     file does not contain class Knightstour.AvailableLocations
    [javac]     Please remove or make sure it appears in the correct subdirector
y of the sourcepath.

这是 AvailableLocations 类。

public class AvailableLocations {

    int x;
    int y;

    public AvailableLocations(int x_coord, int y_coord)
    {
        x = x_coord;
        y = y_coord;
    }
}

这是我的主要课程:

package Knightstour;
import java.util.*;


public class mainclass{
   public static Vector checkmovesavailable (int[][] board, AvailableLocations knightlocation, int x, int y)
    {
        Vector availablemoves = new Vector();

        if (knightlocation.x - 2 >= 0)
        {
            if (knightlocation.y + 1 < y)
            {
                if (board[knightlocation.x - 2][knightlocation.y + 1] == 0)
                {
                    AvailableLocations location = new AvailableLocations(knightlocation.x- 2, knightlocation.y+ 1 );
                    availablemoves.addElement(location);
                }               
            }

            if (knightlocation.y - 1 >= 0)
            {
                if (board[knightlocation.x - 2][knightlocation.y - 1] == 0)
                {
                    AvailableLocations location = new AvailableLocations(knightlocation.x - 2, knightlocation.y - 1 );
                    availablemoves.addElement(location);
                }   

            }
        }

        if (knightlocation.x + 2 < x)
        {
            if (knightlocation.y + 1 < y)
            {
                if (board[knightlocation.x + 2][knightlocation.y + 1] == 0)
                {
                    AvailableLocations location = new AvailableLocations(knightlocation.x+ 2, knightlocation.y+ 1 );
                    availablemoves.addElement(location);
                }               
            }

            if (knightlocation.y - 1 >= 0)
            {
                if (board[knightlocation.x + 2][knightlocation.y - 1] == 0)
                {
                    AvailableLocations location = new AvailableLocations(knightlocation.x + 2, knightlocation.y- 1 );
                    availablemoves.addElement(location);
                }   

            }
        }

        if (knightlocation.y - 2 >= 0)
        {
            if (knightlocation.x+ 1 < x)
            {
                if (board[knightlocation.x+ 1][knightlocation.y - 2] == 0)
                {
                    AvailableLocations location = new AvailableLocations(knightlocation.x + 1, knightlocation.y- 2 );
                    availablemoves.addElement(location);
                }               
            }

            if (knightlocation.x - 1 >= 0)
            {
                if (board[knightlocation.x- 1][knightlocation.y - 2] == 0)
                {
                    AvailableLocations location = new AvailableLocations(knightlocation.x- 1, knightlocation.y - 2 );
                    availablemoves.addElement(location);
                }   

            }
        }

        if (knightlocation.y + 2 < y)
        {
            if (knightlocation.x + 1 < x)
            {
                if (board[knightlocation.x+ 1][knightlocation.y + 2] == 0)
                {
                    AvailableLocations location = new AvailableLocations(knightlocation.x + 1, knightlocation.y + 2 );
                    availablemoves.addElement(location);
                }               
            }

            if (knightlocation.x - 1 >= 0)
            {
                if (board[knightlocation.x - 1][knightlocation.y + 2] == 0)
                {
                    AvailableLocations location = new AvailableLocations(knightlocation.x- 1, knightlocation.y + 2 );
                    availablemoves.addElement(location);
                }   

            }
        }


        return availablemoves;
    }


    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int x = Integer.parseInt(args[0]);
        int y = Integer.parseInt(args[1]);
        int maxattempts = Integer.parseInt(args[2]);

        int [][] board = new int[x][y];
        AvailableLocations knightlocation = new AvailableLocations(0, 0);
        board [0][0] = 1;

        Vector locationsvisited = new Vector();
        locationsvisited.addElement(knightlocation);

        int i = 0;

        while (i <= maxattempts)
        {
            i++;
            Vector movesavailable = checkmovesavailable(board, knightlocation,x, y);
            int numofmovesavailable = movesavailable.size();
            if (numofmovesavailable == 0)
            {
                break;
            }

            Random rand = new Random();
            int selectmove = rand.nextInt(numofmovesavailable - 1 + 1) + 1;

            knightlocation = (AvailableLocations) movesavailable.elementAt(selectmove -1);
            board[knightlocation.x][knightlocation.y] = i + 1;

            if (i == x *y)
            {
                break;
            }


        }


    for(int k = 0; k<x; k++)
    {
        for(int j = 0; j<y; j++)
        {
            if (board[k][j] == 0)
            {
                System.out.print("x ");
            }else{
                System.out.print(board[k][j]);
                System.out.print(" ");
            }
        }
        System.out.print("\n");
    }
    }
}

根据要求:ANT 构建的详细信息:

<property name="src.dir"     value="src"/>

<property name="build.dir"   value="build"/>
<property name="classes.dir" value="${build.dir}/classes"/>
<property name="jar.dir"     value="${build.dir}/jar"/>

<property name="main-class"  value="Knightstour.mainclass"/>



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

<target name="compile">
    <mkdir dir="${classes.dir}"/>
    <javac srcdir="${src.dir}" destdir="${classes.dir}"/>
</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 jar="${jar.dir}/${ant.project.name}.jar" fork="true"/>
</target>

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

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

【问题讨论】:

  • 我们需要您的 ANT build.xml 的详细信息

标签: java ant javac


【解决方案1】:

您已将 AvailableLocations 类与 mainclass 放在同一目录中,但 AvailableLocations.java 似乎缺少 package 语句。尝试将 package Knightstour; 添加到 AvailableLocations。

【讨论】:

    猜你喜欢
    • 2022-01-20
    • 1970-01-01
    • 2013-05-21
    • 2011-09-18
    • 1970-01-01
    • 2011-04-18
    • 1970-01-01
    • 1970-01-01
    • 2014-04-05
    相关资源
    最近更新 更多