java - 制作一个 jar list 。它从哪里来,我自己做吗?

标签 java jar manifest

我最近创建了一个简单的 tic tac toe 程序,我一直在尝试将其放入 jar 中,并使用 cmd。我设法拿到了 jar ,但是当我点击它时什么也没有发生。经过更多研究后,我认为这是一个明显的问题,因为我没有对此做任何事情,甚至可以找到一个问题。现在这个 list 的事情真的让我很困惑,我已经多次创建了自己的 list 作为一个txt,但这不起作用,我读到它会创建它自己的 list ,但我在任何地方都找不到它。有人可以澄清一下这个 list 来自哪里或者如何创建一个。

Java代码:

import javax.swing.JFrame;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.*;
import javax.swing.ImageIcon;
import javax.swing.SwingUtilities;
import javax.swing.JButton;

import javax.swing.JLabel;
public class TicTacToe extends JFrame {

// GUI constants
private static final int WIDTH = 400; // display width
private static final int HEIGHT = 300; // display height

// TicTacToe layout constants
private static final int NUM_ROWS = 3; // number of tic-tac-toe rows
private static final int NUM_COLS = 3; // number of tic-tac-toe columns
private static final int TOTAL_CELLS = NUM_ROWS * NUM_COLS;
private static final int MAX_MOVES = NUM_ROWS * NUM_COLS; // max number of moves

// TicTacToe image, message and default cell value constants
private static final ImageIcon X_IMAGE = new ImageIcon("images.jpg", ""); // image for X
private static final ImageIcon O_IMAGE = new ImageIcon("O.jpg", ""); // image for O
private static final String GAME_ENDS_IN_TIE = "nobody wins cause blame john wells"; // tie message
private static final String NON_PLAYER_CELL_VALUE = "-"; // "-" is a non player cell

// Private TicTacToe members
private JButton[] cells;
private CellButtonHandler[] cellHandlers;
private String gameWinner;
private int xGoesFirst;
// private int[] press = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
// private String xMess = "Cross for you laymens out there";
// private String oMess = "Naught for you laymens out there";
private String pOne;
private String pTwo;
// private int pOneWins;
// private int pTwoWins;
private int count = 0;

/**
 * Default Constructor
 */
public TicTacToe() {
    // JFrame frame = new JFrame();

    // SWING OPERATIONS
    Container pane = getContentPane();
    pane.setLayout(new GridLayout(NUM_ROWS, NUM_COLS));
    cells = new JButton[TOTAL_CELLS];
    cellHandlers = new CellButtonHandler[TOTAL_CELLS];
    for (int i = 0; i < TOTAL_CELLS; i++) {
        cells[i] = new JButton(NON_PLAYER_CELL_VALUE);
        cellHandlers[i] = new CellButtonHandler();
        cells[i].addActionListener(cellHandlers[i]);
        pane.add(cells[i]);
    }

    setTitle("Tic Tac Toe");
    setSize(WIDTH, HEIGHT);
    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    // END SWING OPERATIONS

    // CLASS OPERATIONS
    this.setGameWinner(GAME_ENDS_IN_TIE);
    this.xGoesFirst = 1;
    this.pOne = "X";
    this.pTwo = "O";
    // this.pOneWins = 0;
    // this.pTwoWins = 0;
    // END CLASS OPERATIONS

} // public TicTacToe()

/**
 * setGameWinner
 * 
 * @param who - the game winner as a String.
 */
private void setGameWinner(String who) {
    this.gameWinner = who;
} // private void setGameWinner(String who)

/**
 * getGameWinner
 * 
 * @return the game winner
 */
public String getGameWinner() {
    return this.gameWinner;
} // public String getGameWinner()

/**
 * Entry point of the program.
 * 
 * @param args - <code>String[]</code> of runtime arguments
 */
public static void main(String[] args) {
    // Swing is not thread safe, use SwingUtilities#invokeLater
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            new TicTacToe();
        }
    });
} // public static void main(String[] args)

/**
 * CellButtonHandler
 */
private class CellButtonHandler implements ActionListener {

    /**
     * actionPerformed
     */
    @Override
    public void actionPerformed(ActionEvent e) {
        JButton pressed = (JButton) e.getSource();

        pressed.setText(TicTacToe.this.getCurrentPlayer());
        // pressed.setIcon(TicTacToe.this.getCurrentPlayerIcon());

        TicTacToe.this.count++;

        if (TicTacToe.this.gameOverWin() || TicTacToe.this.gameOverTie()) {
            System.out.println(TicTacToe.this.getGameWinner());
            winScreen();
        }

        for (MouseListener ml : pressed.getMouseListeners()) {
            pressed.removeMouseListener(ml);
        }
    } // public void actionPerformed(ActionEvent e)

} // private class CellButtonHandler implements ActionListener

/**
 * private String getCurrentPlayer()
 */
private String getCurrentPlayer() {
    this.xGoesFirst = this.xGoesFirst * -1;

    if (this.xGoesFirst == -1) {
        return pOne;
    }

    return pTwo;
} //    private String getCurrentPlayer()

/**
 * getCurrentPlayerIcon
 */
private ImageIcon getCurrentPlayerIcon() {
    this.xGoesFirst = this.xGoesFirst * -1;

    if (this.xGoesFirst == -1) {
        return X_IMAGE;
    }

    return O_IMAGE;
} // private ImageIcon getCurrentPlayerIcon()

/**
 * Checks if the game ended in a win.
 * 
 * @return true if someone has won the game.
 */
private boolean gameOverWin() {
    if (rowChecker() || colomnChecker() || diagChecker()) {
        return true;
    }

    return false;
} //    private boolean gameOverWin()

/**
 * Checks if the game ended in a tie.
 * 
 * @return true if there are no more moves to be made.
 */
private boolean gameOverTie() {
    if (this.count >= MAX_MOVES) {
        return true;
    }

    return false;
} // private boolean gameOverTie()

/**
 * Checks the rows for a win.
 * 
 * @return true if one of the rows contains three X's or three O's.
 */
public boolean rowChecker() {
    int row = 0; // row variable
    int col = 0; // column variable
    String mark = ""; // string to hold the first
                        // button in a row's text value

    while (row != NUM_ROWS) {
        col = row * NUM_ROWS;
        mark = this.getCellText(col);

        if (mark.equals(NON_PLAYER_CELL_VALUE)) {
            row = row + 1;
            continue;
        }

        if (this.cellsAreEqual(mark, col + 1)
                && this.cellsAreEqual(mark, col + 2)) {

            this.setGameWinner("Row Winner: " + mark);
            return true;
        }

        row = row + 1;
    }

    // no win across the rows so we return false
    return false;
} // public boolean rowChecker()

/**
 * Checks the diagonals for a win.
 * 
 * @return true if one of the diagonals contains three X's or three O's.
 */
public boolean diagChecker() {
    int leftToRight = 0; // start at the top left box
    int rightToLeft = 0; // start at the top right box
    int step = 0; // the number of cells to step over
    String mark = ""; // string to hold the buttons mark

    // first we'll start by checking the top-left to
    // bottom-right diagonal
    leftToRight = 0;
    step = NUM_COLS + 1;
    mark = this.getCellText(leftToRight);

    if (!mark.equals(NON_PLAYER_CELL_VALUE)) {
        if (this.cellsAreEqual(mark, step)
                && this.cellsAreEqual(mark, (step * 2))) {

            this.setGameWinner("Diagonal Winner: " + mark);
            return true;
        }
    }

    // next we'll check the top-right to bottom-left diagonal
    rightToLeft = NUM_COLS - 1;
    step = NUM_COLS - 1;
    mark = this.getCellText(rightToLeft);

    if (!mark.equals(NON_PLAYER_CELL_VALUE)) {
        if (this.cellsAreEqual(mark, rightToLeft + step)
                && this.cellsAreEqual(mark, rightToLeft + (step * 2))) {

            this.setGameWinner("Diagonal Winner: " + mark);
            return true;
        }
    }

    // no win on the diagonals so we return false
    return false;
} // public boolean diagChecker()

/**
 * colomnChecker
 */
public boolean colomnChecker() {
    int row = 0; // row variable
    int col = 0; // column variable
    String mark = ""; // string to hold the buttons mark

    while (col != NUM_COLS) {
        row = col;
        mark = getCellText(row);

        if (mark.equals(NON_PLAYER_CELL_VALUE)) {
            col = col + 1;
            continue;
        }

        if (this.cellsAreEqual(mark, row + 3)
                && this.cellsAreEqual(mark, row + 6)) {

            this.setGameWinner("Column Winner: " + mark);
            return true;
        }

        col = col + 1;
    }

    // no win down the columns so we return false
    return false;
} // public boolean colomnChecker()

/**
 * getCellText
 */
private String getCellText(int which) {
    return this.cells[which].getText();
} // private String getCellText(int which)

/**
 * cellsAreEqual
 */
private boolean cellsAreEqual(String mark, int index) {
    return mark.equals(this.getCellText(index));
} // private boolean cellsAreEqual(String mark, int index)

// private class restart implements ActionListener
public boolean winScreen(){
     Container pane = getContentPane();
     pane.removeAll();
     pane.repaint();
     JLabel label=new JLabel(TicTacToe.this.getGameWinner()+" won");
     label.setHorizontalAlignment(JLabel.CENTER);
     label.setVerticalAlignment(JLabel.BOTTOM);
     JLabel back=new JLabel(new ImageIcon("moniz_475.JPEG"));
     pane.add(back);
     pane.add(label); 

     return true;
}

} // public class TicTacToe extends JFrame

list 文件:

主类:Tic_Tac_Toe.TicTacToe

最佳答案

如果您想将 list 文件添加到 jar 中,则必须使用 jar -cfm 命令。

例如,您有一个名为 Manifest.txt 的 list 文件,您希望将其添加到命令将是的 jar tictactoe.jar 中。

jar -cfm tictactoe.jar Manifest.txt *

如果您想提及主类,您必须在 list 文件中添加一个条目,如下所示。

Main-Class: <fully qualified name for the class>

还要检查 jar 文件的默认程序是否正确配置。

更新

您的 Java 类不在包中,因此您的 list 文件应该在包中。

Main-Class: TicTacToe 

如果您希望该类位于包中,请在导入语句上方的第一行中添加包package tictactoe;。 list 文件中应该是。

Main-Class: tictactoe.TicTacToe

欲了解更多信息,请查看Setting an Application's Entry Point

关于java - 制作一个 jar list 。它从哪里来,我自己做吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27221428/

相关文章:

java - 无法将 JSON 对象提交到 JSP

java - 如何在 Scala 中实现真正的单例

shell 中的 java 类路径

eclipse - 错误: Cannot run program "jar": CreateProcess error=2,系统找不到指定的文件

android - 遵循Mapbox初学者教程会返回 “Manifest merger failed”错误

java - 与 text/xml 相比,我如何使用 application/xml?

java - 无法在 wro4j-maven-plugin (NPE) 中使用 ConfigurableWroManagerFactory

java - 如何在新进程中执行Jar文件并关闭原来的Jar?

c++ - 使用Visual Studios C++ list 文件

winapi - 应用程序 list - 是否需要 UTF-8 BOM?