Java 通过在Eclipse中正常运行的应用程序创建jar文件时出错
我正在尝试用Java构建AI路径查找应用程序。它在Eclipse中正常工作,没有错误,但我在创建jar文件时收到编译警告,因此它没有运行。错误来自以下类sourcode。请帮帮我!多谢各位Java 通过在Eclipse中正常运行的应用程序创建jar文件时出错,java,jar,executable-jar,Java,Jar,Executable Jar,我正在尝试用Java构建AI路径查找应用程序。它在Eclipse中正常工作,没有错误,但我在创建jar文件时收到编译警告,因此它没有运行。错误来自以下类sourcode。请帮帮我!多谢各位 package aStar; import java.util.ArrayList; import java.util.Collections; import aStar.util.ClosestHeuristic; /** * The class which uses AStar based al
package aStar;
import java.util.ArrayList;
import java.util.Collections;
import aStar.util.ClosestHeuristic;
/**
* The class which uses AStar based algorithm to determine the path.
* @author Dorji Gyaltsen
*/
public class AStarPathFinder{
/** The set of nodes which have been searched(ClosedNode)
* and yet to be searched(OpenNode) */
private ArrayList closedNode = new ArrayList();
private OpenList openNode = new OpenList();
/** The instance of the map being searched */
private Map map;
/** The maximum searchDepth of search */
private int maxSearchDistance;
/** The total set of nodes in the map */
private Node[][] nodes;
/** allowance of diagonal movement */
private boolean DiagonalMovement;
/** The heuristic to determine which node to search first */
private AStarHeuristic heuristic;
/**
* Constructor initialising all the variables
*/
public AStarPathFinder(Map newMap, int newMaxSearchDistance,
boolean newDiagonalMovement, ClosestHeuristic newheuristic) {
map = newMap;
maxSearchDistance = newMaxSearchDistance;
DiagonalMovement = newDiagonalMovement;
heuristic = newheuristic;
/**Node definition and initialisation **/
nodes = new Node[map.getWidth()][map.getHeight()];
for (int x=0;x<nodes.length;x++)
for (int y=0;y<nodes.length;y++)
nodes[x][y] = new Node(x,y);
}
/**
* The main method which find the path based on AStar algorithm **/
public Path searchShortestPath(UnitMover mover, int sx, int sy, int dx, int dy) {
//if the path is blocked
if (map.pathBlocked(mover, dx, dy)){
return null;
}else {
//if the path is not blocked
nodes[sx][sy].pathCost = 0;
nodes[sx][sy].searchDepth = 0;
closedNode.clear();
openNode.clear();
openNode.add(nodes[sx][sy]);
nodes[dx][dy].parent = null;
// When the destination is not reached
int maxSearchDepth = 0;
while ((maxSearchDepth < maxSearchDistance) && (openNode.size() != 0)) {
Node current = getFirstOfOpenNode();
/**if current node is the destination node then stop**/
if (current == nodes[dx][dy]) {
break;
}
else {
clearFromOpenNode(current);
addToClosedNode(current);
// search all curentNode neighbours considering as the next closest node
for (int x=-1;x<2;x++) {
for (int y=-1;y<2;y++) {
// if it is the current node
if ((x == 0) && (y == 0)) {
continue;
}
else if (!DiagonalMovement){
if ((x != 0) && (y != 0))
continue;
}
else {
// determine the location of the neighbour and evaluate it
int xn = x + current.x;
int yn = y + current.y;
if (isValidLocation(mover,sx,sy,xn,yn)) {
float nextNodePathCost = current.pathCost + map.getPathCost(mover, current.x, current.y, xn, yn);
Node neighbour = nodes[xn][yn];
map.pathFinderVisited(xn, yn);
// if the new pathCost we've determined for this node is lower than
// it has been previously makes sure the node hasn't been discarded.
if (nextNodePathCost < neighbour.pathCost) {
if (inopenNode(neighbour))
clearFromOpenNode(neighbour);
else if (inclosedNode(neighbour))
clearFromclosedNode(neighbour);
}
// if the node hasn't already been processed and discarded then
// reset it's pathCost to our current pathCost and add it as a next possible
if (!inopenNode(neighbour) && !(inclosedNode(neighbour))) {
neighbour.pathCost = nextNodePathCost;
neighbour.heuristic = heuristic.getPathCost(map, mover, xn, yn, dx, dy);
maxSearchDepth = Math.max(maxSearchDepth, neighbour.setParentNode(current));
addToopenNode(neighbour);
}
}
}
}
}
}
}
// since we've got an empty openNode openList or we've run out of search
// there was no path. Just return null
if (nodes[dx][dy].parent == null)
return null;
// At this point we've definitely found a path so we can uses the parent
// references of the nodes to find out way from the destination location back
// to the start recording the nodes on the way.
Path path = new Path();
Node destination = nodes[dx][dy];
while (destination != nodes[sx][sy]) {
path.prependStep(destination.x, destination.y);
destination = destination.parent;
}
path.prependStep(sx,sy);
// thats it, we have our path
return path;
}
}
/**
* Get the first element from the openNode openList. This is the next
* one to be searched.
*
* @return The first element in the openNode openList
*/
protected Node getFirstOfOpenNode() {
return (Node) openNode.first();
}
/**
* Add a node to the openNode openList
*
* @param node The node to be added to the openNode openList
*/
protected void addToopenNode(Node node) {
openNode.add(node);
}
/**
* Check if a node is in the openNode openList
*
* @param node The node to check for
* @return True if the node given is in the openNode openList
*/
protected boolean inopenNode(Node node) {
return openNode.contains(node);
}
/**
* clear a node from the openNode openList
*
* @param node The node to clear from the openNode openList
*/
protected void clearFromOpenNode(Node node) {
openNode.clear();
}
/**
* Add a node to the closedNode openList
*
* @param node The node to add to the closedNode openList
*/
protected void addToClosedNode(Node node) {
closedNode.add(node);
}
/**
* Check if the node supplied is in the closedNode openList
*
* @param node The node to search for
* @return True if the node specified is in the closedNode openList
*/
protected boolean inclosedNode(Node node) {
return closedNode.contains(node);
}
/**
* clear a node from the closedNode openList
*
* @param node The node to clear from the closedNode openList
*/
protected void clearFromclosedNode(Node node) {
closedNode.clear();
}
/**
* Check if a given location is valid for the supplied mover
*
* @param mover The mover that would hold a given location
* @param sx The starting x coordinate
* @param sy The starting y coordinate
* @param x The x coordinate of the location to check
* @param y The y coordinate of the location to check
* @return True if the location is valid for the given mover
*/
protected boolean isValidLocation(UnitMover mover, int sx, int sy, int x, int y) {
if((x < 0) || (y < 0) || (x >= map.getWidth()) || (y >= map.getHeight())){
return false;
}
else{
if ((sx != x) || (sy != y))
if(!map.pathBlocked(mover, x, y));
return true;
}
}
/**
* A simple sorted openList
* @author Dorji Gyaltsen
*/
private class OpenList {
/** The openList of elements */
private ArrayList openList = new ArrayList();
/**
* @return The first element from the openList
*/
public Object first() {
return openList.get(0);
}
/**
* Empty the openList
*/
public void clear() {
openList.clear();
}
/**
* Adding an element to open ist and sorting
*/
@SuppressWarnings("unchecked")
public void add(Object o) {
openList.add(o);
Collections.sort(openList);
}
/**
* @return The total number of element in the openList
*/
public int size() {
return openList.size();
}
/**
* @return True if the element is in the openList
*/
public boolean contains(Object o) {
return openList.contains(o);
}
}
/**
* A single node in the search graph
*/
private class Node implements Comparable {
/** The x and y coordinates of the node */
private int x;
private int y;
/** The path pathCost for this node */
private float pathCost;
/** The parent of this node*/
private Node parent;
/** The heuristic pathCost of this node */
private float heuristic;
/** The search searchDepth of this node */
private int searchDepth;
/**
* The creation of a new node
* @param x and y The x and y coordinates of the node
*/
public Node(int x1, int y1) {
x = x1;
y = y1;
}
/**
* Set the parent for this node
* @param parent The parent node which lead to this node
* @return The searchDepth for searching
*/
public int setParentNode(Node parent) {
searchDepth = parent.searchDepth + 1;
this.parent = parent;
return searchDepth;
}
/**
* compareTo(Object)
*/
public int compareTo(Object other) {
Node o = (Node) other;
float f = heuristic + pathCost;
float of = o.heuristic + o.pathCost;
if (f < of) return -1;
else if (f > of) return 1;
else return 0;
}
}
}
包aStar;
导入java.util.ArrayList;
导入java.util.Collections;
导入aStar.util.ClosestHeuristic;
/**
*使用基于AStar的算法确定路径的类。
*@作者Dorji Gyaltsen
*/
公共类AStarPathFinder{
/**已搜索的节点集(ClosedNode)
*尚待搜索(OpenNode)*/
private ArrayList closedNode=new ArrayList();
私有OpenList openNode=新OpenList();
/**正在搜索的地图的实例*/
私人地图;
/**搜索的最大搜索深度*/
专用搜索距离;
/**映射中节点的总集*/
专用节点[][]个节点;
/**对角线运动余量*/
私有布尔对角运动;
/**确定首先搜索哪个节点的启发式方法*/
私人无流变启发式;
/**
*构造函数初始化所有变量
*/
公共AStarPathFinder(Map newMap、int newMaxSearchDistance、,
布尔新对角移动,闭包(新启发式){
map=newMap;
maxSearchDistance=newMaxSearchDistance;
对角线运动=新的对角线运动;
启发式=新启发式;
/**节点定义和初始化**/
节点=新节点[map.getWidth()][map.getHeight()];
对于(int x=0;x您的问题很可能来自您的jar文件不完整的事实。根据您的评论,您似乎只将项目的一部分导出到jar文件中。您应该尝试使用Eclipse扩展生成jar文件。该网站包含您需要的所有文档
此外,您显示的类不包含任何main
方法。为了仅使用jar文件运行应用程序,您需要在项目中的某个位置使用该方法。您遇到了什么样的错误?请从您的问题中删除所有这些不相关的代码,并将其替换为实际遇到的错误。@MarkoTopolnik my compiler没有提供关于错误的任何细节,但它说这个文件中有一些错误。你想让我们对你的问题进行一些暗中探索吗?没有,但我想知道如何更改编译器设置来解决问题。我很抱歉这么天真。谢谢你的善意回应。好吧,我在其他主类中有main方法。我将看一看你给出的链接。现在我遇到的问题是“图像不可用,无法读取输入”,这里是我在主类中的图像读取代码:image[Map.GRASS]=ImageIO.read(新文件(“res/GRASS.png”))请comment@Dorji出现这个新问题的原因是,当您使用jar文件时,对该文件的引用不起作用。您应该在jar文件中搜索如何使用图像资源,您很可能会找到问题的答案。因此可能会有一些问题。如果没有问题与您的实际问题相对应m、 你应该问另一个问题,因为这个问题与jar的创建有关(我们不希望一个问题中有超过一个问题)。谢谢你的友好回答。我会回答这个问题。我非常感谢你的时间和努力。祝你愉快