在我的Chess项目的eclipse上创建一个可运行的jar文件时,得到一个奇怪的行为 [英] Getting a strange behavior when creating a runnable jar file on eclipse for my Chess project

查看:162
本文介绍了在我的Chess项目的eclipse上创建一个可运行的jar文件时,得到一个奇怪的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如主题所说,我已经创建了一个象棋游戏作为我的第一个2D游戏,但是当我尝试从它创建一个可运行的jar文件,我得到一个奇怪的行为,当我移动的棋子游戏变得混乱,没有什么作品。奇怪的是,在日食里面,一切都是完美的。当它创建可运行的jar文件时,eclipse会出现以下错误:

 资源与文件系统不同步:'/ Chess_Project /src/.DS_Store。 

我不记得在我的项目中有这样的文件,所以我猜这是一个隐藏的文件或一个系统文件。然后我试图导出它作为一个正常的Jar文件与eclipse并通过终端(顺便说一下,我是一个mac OSX狮子用户),但我失败了两种方式,得到 NullPointerException 还有一些错误。我已经阅读了一些关于清单文件,并尝试自己编译我的代码,然后以上面提到的每种方式导出。



我试图解决它一个星期左右,并在各种论坛上提出帮助,但没有运气。



感谢francis我意识到问题可能来自我的代码本身。 Andrew我的问题是为什么我可以在可运行的jar文件上得到一个奇怪的行为(这意味着事情不会像他们应该的那样),而当我在eclipse中运行它时,每件事情都可以正常运行?
这里是我的代码的主要类:

  import java.awt。*; 
import javax.swing.SwingUtilities;
import javax.swing。*;

public class MainWindowChess {

/ **
*
* /
public static void main(String [] args){
SwingUtilities.invokeLater(new Runnable(){

@Override
public void run(){
// TODO自动生成的方法存根
JFrame f = new JFrame(Chess);
SwingUtilities.isEventDispatchThread();
f.setLayout(new BorderLayout());
f.setSize(40 * 8,40 * 9 - 20 );
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setResizable(false);
Board b = new Board();
f.add(b);
f.setVisible(true);
System.out.println(check);

}
});

}
}

顺便说一句,人们回答这么快,我很高兴看到这么多人希望帮助别人。



这里是paintComponent方法:

  protected void paintComponent(Graphics g){
super.paintComponents(g); (int i = 0; i< 8; i ++){
for(int j = 0; j< 8 ; j ++){
g.drawImage(getRect(i,j),cors [i] [j] .getXCor(),cors [i] [j] .getYCor(),null); (int i = 0; i< bp.length; i ++){
bp [i] .drawPiece(g,bp [ i] .getImage(),bp [i] .getLocationX(),bp [i] .getLocationY());
}
for(int i = 0; i< wp.length; i ++){
wp [i] .drawPiece(g,wp [i] .getImage(),wp [ i] .getLocationX(),wp [i] .getLocationY());
}

boardDrawn = true;
} else if(boardDrawn == true){


if(tempPiece instanceof BlackPiece){
for(int i = 0; i< bp.length ; i ++){

//如果找到的是
if(tempPiece == bp [i]){

if(bp [i] .isMoveAvailable (bp [i] .getTypeID(),bp [i] .getLocationX(),bp [i] .getLocationY(),tempCor。
getXCor(),tempCor.getYCor())){
killTime = true;
//如果我们要绘制Piece的矩形没有被另外的一块
占用if(isRectBlocked(bp [i] .getTypeID(),oldCor.getXCor(),oldCor.getYCor( ),tempCor。
getXCor(),tempCor.getYCor())== false){

g.drawImage(getCompatitableRect(oldCor.getXCor(),oldCor.getYCor()), oldCor。
getXCor(),oldCor.getYCor(),null);
bp [i] .drawPiece(g,bp [i] .getImage(),tempCor.getXCor(),tempCor.getYCor());
bp [i] .setX(tempCor.getXCor());
bp [i] .setY(tempCor.getYCor());

} else if(canRectBeOccupied(bp [i],tempCor.getXCor(),tempCor.getYCor())
&& isRectBlocked(bp [i] .getTypeID() oldCor.getXCor(),oldCor.getYCor(),tempCor。
getXCor(),tempCor.getYCor())== false){

g.drawImage(getCompatitableRect(oldCor.getXCor (),oldCor.getYCor()),oldCor。
getXCor(),oldCor.getYCor(),null);
g.drawImage(getCompatitableRect(tempCor.getXCor(),tempCor.getYCor()),tempCor。
getXCor(),tempCor.getYCor(),null);
bp [i] .drawPiece(g,bp [i] .getImage(),tempCor.getXCor(),tempCor.getYCor());
bp [i] .setX(tempCor.getXCor());
bp [i] .setY(tempCor.getYCor());
$


如果(tempPiece instanceof WhitePiece){
for(int i = 0; i< wp.​​length; i ++){

//如果找到的是
if(tempPiece == wp [i]){
if(wp [i] .isMoveAvailable wp [i] .getTypeID(),wp [i] .getLocationX(),wp [i] .getLocationY(),tempCor。
getXCor(),tempCor.getYCor())){
killTime = true;

if(isRectBlocked(wp [i] .getTypeID(),oldCor.getXCor(),oldCor.getYCor(),tempCor。
getXCor(),tempCor.getYCor())= = false){
System.out.println();
g.drawImage(getCompatitableRect(oldCor.getXCor(),oldCor.getYCor()),oldCor。
getXCor(),oldCor.getYCor(),null);
wp [i] .drawPiece(g,wp [i] .getImage(),tempCor.getXCor(),tempCor.getYCor());
wp [i] .setX(tempCor.getXCor());
wp [i] .setY(tempCor.getYCor());

} else if(canRectBeOccupied(wp [i],tempCor.getXCor(),tempCor.getYCor())
& amp;& isRectBlocked(wp [i] .getTypeID() oldCor.getXCor(),oldCor.getYCor(),tempCor。
getXCor(),tempCor.getYCor())== false){

g.drawImage(getCompatitableRect(oldCor.getXCor (),oldCor.getYCor()),oldCor。
getXCor(),oldCor.getYCor(),null);
g.drawImage(getCompatitableRect(tempCor.getXCor(),tempCor.getYCor()),tempCor。
getXCor(),tempCor.getYCor(),null);
wp [i] .drawPiece(g,wp [i] .getImage(),tempCor.getXCor(),tempCor.getYCor());
wp [i] .setX(tempCor.getXCor());
wp [i] .setY(tempCor.getYCor());
}
}
}
}
}

killTime = false;
tempPiece = null;
}
}


解决方案

软件包/项目浏览器,右键单击您的项目并选择刷新。在导出中,您应该可以将其从Jar文件中排除(因为它没有业务)。



这个.DS_Store文件是一个Mac OS文件由操作系统自动创建,如果文件被删除,Eclipse将会抱怨,因此您需要确保刷新工作区。


As the topic says I have created a chess game as my first 2D game, but when ever I try to create a runnable jar file from it I get a strange behavior, and when I move the pieces the game becomes chaotic and nothing works. The weird thing is that inside the eclipse everything works perfectly. When it creates the runnable jar file eclipse gives the following error:

Resource is out of sync with the file system: '/Chess_Project/src/.DS_Store'.

I don't remember even having such file in my project so I guess it is a hidden file or a System file. Then I tried to export it as a normal Jar file with eclipse and after it through terminal (By the way I am a mac OSX lion user) ,but I failed at both ways and got NullPointerException and some more errors. I have read a little about manifest files and tried to compile my code by myself and then export in each of the ways which I mentioned above.

I have tried to solve it for a week or so and asked in various forums for help, but with no luck.

Thanks to francis I have realized the problem might come from my code itself. And andrew my question is "why do I get a strange behavior on the runnable jar file (which means things are not working like they should) while when I run it inside eclipse every thing works just fine?". here is the main class of my code:

import java.awt.*;
import javax.swing.SwingUtilities;
import javax.swing.*;

public class MainWindowChess {

    /**
    *
    */
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                JFrame f = new JFrame("Chess");
                SwingUtilities.isEventDispatchThread();
                f.setLayout(new BorderLayout());
                f.setSize(40 * 8, 40 * 9 - 20);
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.setResizable(false);
                Board b = new Board();
                f.add(b);
                f.setVisible(true);
                System.out.println("check");

            }
        });

    }
}

By the way it is amazing that people reply here so fast, I am delighted to see so many people who wish to help others.

Here is the paintComponent method:

protected void paintComponent(Graphics g) {
    super.paintComponents(g);

    if (boardDrawn == false) {
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                g.drawImage(getRect(i, j), cors[i][j].getXCor(), cors[i][j].getYCor(), null);
            }
        }

        for (int i = 0; i < bp.length; i++) {
            bp[i].drawPiece(g, bp[i].getImage(), bp[i].getLocationX(), bp[i].getLocationY());
        }
        for (int i = 0; i < wp.length; i++) {
            wp[i].drawPiece(g, wp[i].getImage(), wp[i].getLocationX(), wp[i].getLocationY());
        }

        boardDrawn = true;
    } else if (boardDrawn == true) {


        if (tempPiece instanceof BlackPiece) {
            for (int i = 0; i < bp.length; i++) {

                //if the piece was found
                if (tempPiece == bp[i]) {

                    if (bp[i].isMoveAvailable(bp[i].getTypeID(), bp[i].getLocationX(), bp[i].getLocationY(), tempCor.
                            getXCor(), tempCor.getYCor())) {
                        killTime = true;
                        //if the rectangle which we want to draw the Piece on is not occupied by another Piece
                        if (isRectBlocked(bp[i].getTypeID(), oldCor.getXCor(), oldCor.getYCor(), tempCor.
                                getXCor(), tempCor.getYCor()) == false) {

                            g.drawImage(getCompatitableRect(oldCor.getXCor(), oldCor.getYCor()), oldCor.
                                    getXCor(), oldCor.getYCor(), null);
                            bp[i].drawPiece(g, bp[i].getImage(), tempCor.getXCor(), tempCor.getYCor());
                            bp[i].setX(tempCor.getXCor());
                            bp[i].setY(tempCor.getYCor());

                        } else if (canRectBeOccupied(bp[i], tempCor.getXCor(), tempCor.getYCor())
                                && isRectBlocked(bp[i].getTypeID(), oldCor.getXCor(), oldCor.getYCor(), tempCor.
                                getXCor(), tempCor.getYCor()) == false) {

                            g.drawImage(getCompatitableRect(oldCor.getXCor(), oldCor.getYCor()), oldCor.
                                    getXCor(), oldCor.getYCor(), null);
                            g.drawImage(getCompatitableRect(tempCor.getXCor(), tempCor.getYCor()), tempCor.
                                    getXCor(), tempCor.getYCor(), null);
                            bp[i].drawPiece(g, bp[i].getImage(), tempCor.getXCor(), tempCor.getYCor());
                            bp[i].setX(tempCor.getXCor());
                            bp[i].setY(tempCor.getYCor());
                        }
                    }
                }
            }
        }
        if (tempPiece instanceof WhitePiece) {
            for (int i = 0; i < wp.length; i++) {

                //if the piece was found
                if (tempPiece == wp[i]) {
                    if (wp[i].isMoveAvailable(wp[i].getTypeID(), wp[i].getLocationX(), wp[i].getLocationY(), tempCor.
                            getXCor(), tempCor.getYCor())) {
                        killTime = true;

                        if (isRectBlocked(wp[i].getTypeID(), oldCor.getXCor(), oldCor.getYCor(), tempCor.
                                getXCor(), tempCor.getYCor()) == false) {
                            System.out.println("");
                            g.drawImage(getCompatitableRect(oldCor.getXCor(), oldCor.getYCor()), oldCor.
                                    getXCor(), oldCor.getYCor(), null);
                            wp[i].drawPiece(g, wp[i].getImage(), tempCor.getXCor(), tempCor.getYCor());
                            wp[i].setX(tempCor.getXCor());
                            wp[i].setY(tempCor.getYCor());

                        } else if (canRectBeOccupied(wp[i], tempCor.getXCor(), tempCor.getYCor())
                                && isRectBlocked(wp[i].getTypeID(), oldCor.getXCor(), oldCor.getYCor(), tempCor.
                                getXCor(), tempCor.getYCor()) == false) {

                            g.drawImage(getCompatitableRect(oldCor.getXCor(), oldCor.getYCor()), oldCor.
                                    getXCor(), oldCor.getYCor(), null);
                            g.drawImage(getCompatitableRect(tempCor.getXCor(), tempCor.getYCor()), tempCor.
                                    getXCor(), tempCor.getYCor(), null);
                            wp[i].drawPiece(g, wp[i].getImage(), tempCor.getXCor(), tempCor.getYCor());
                            wp[i].setX(tempCor.getXCor());
                            wp[i].setY(tempCor.getYCor());
                        }
                    }
                }
            }
        }

        killTime = false;
        tempPiece = null;
    }
}

解决方案

In the Package/Project Explorer, right click on your Project and select "Refresh". In the export then you should be able to exclude it from the Jar file (since it has no business being there).

This .DS_Store file is a Mac OS file that's automatically created by the OS, and Eclipse will complain if the file was deleted, so you need to make sure your workspace is refreshed.

这篇关于在我的Chess项目的eclipse上创建一个可运行的jar文件时,得到一个奇怪的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆