简体   繁体   English

无法在ANT中编译,但在Eclipse中编译

[英]Cannot compile in ANT, but compiles in Eclipse

I am using ANT for the very first time and developing a watered down version of the KnightsTour program. 我是第一次使用ANT,并且正在开发KnightsTour程序的精简版本。 I first developed in in Eclipse had no issues, I am trying to compile in ANT, but I am having no success. 我最初是在Eclipse中开发的,没有问题,我正在尝试在ANT中进行编译,但是没有成功。 I made no changes to the 2 classes, yet the JAVAC compiler states that one of my classes AVAILABLELOCATIONS has been duplicated. 我没有对这两个类进行任何更改,但是JAVAC编译器指出我的一个类AVAILABLELOCATIONS已被复制。

Here is the compiler error. 这是编译器错误。

    [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.

Here is the AvailableLocations class. 这是AvailableLocations类。

public class AvailableLocations {

    int x;
    int y;

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

Here is my main class: 这是我的主要课程:

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");
    }
    }
}

As requested: Details of ANT build: 根据要求: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"/>

You have placed your AvailableLocations class in the same directory as mainclass, but AvailableLocations.java appears to be missing a package statement. 您已将AvailableLocations类与mainclass放在同一目录中,但是AvailableLocations.java似乎缺少package语句。 Try adding package Knightstour; 尝试添加package Knightstour; to AvailableLocations. 到AvailableLocations。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM