问题
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;
}
}
回答1:
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.
回答2:
.DS_Store (Desktop Services Store) is a hidden file created by Apple Inc.'s Mac OS X operating system to store custom attributes of a folder such as the position of icons or the choice of a background image.
来源:https://stackoverflow.com/questions/10385696/getting-a-strange-behavior-when-creating-a-runnable-jar-file-on-eclipse-for-my-c