制作一个jar清单。它来自哪里我自己制作?

时间:2014-12-01 03:49:39

标签: java jar manifest

我最近创建了一个简单的tic tac toe程序,我一直试图将它变成一个jar并使用cmd。我设法拿到了罐子,但是当我点击它时没有任何反应。经过更多的研究后,我认为这是一个明显的问题,因为我没有做任何事情,甚至可以找到一个。现在这个显而易见的事情让我很困惑我已经创建了我自己的清单几次只是一个txt,但是这没有用,我读到它会创建它自己的清单,但我找不到任何地方。有人可以澄清这个清单来自或如何创建一个。

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

清单文件:

Main-Class:Tic_Tac_Toe.TicTacToe

2 个答案:

答案 0 :(得分:1)

您使用的是IDE吗?如果是这样的话,大多数IDE都可以选择直接将Java程序导出为创建了Manifest的jar。您在META-INF / MANIFEST.MF文件中需要的是主路径和类路径中的任何依赖库。

在MANIFEST.MF文件中添加以下内容:

Main-Class: com.example.MainClass
Class-Path: lib/library.jar 

使用Main-Class中的main方法和Class-Path中的任何依赖库提供类的完整路径。

现在你可以尝试创建jar了。

答案 1 :(得分:1)

如果要将清单文件添加到jar,则必须使用jar -cfm命令。

例如,您有一个清单文件名Manifest.txt,您希望将该命令添加到您的jar tictactoe.jar中。

jar -cfm tictactoe.jar Manifest.txt *

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

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

同时检查jar文件的默认程序是否已正确配置。

<强>更新

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

Main-Class: TicTacToe 

如果您想要一个包中的类,那么在import语句上方的第一行添加包package tictactoe;。清单文件应该是。

Main-Class: tictactoe.TicTacToe

有关详情,请查看Setting an Application's Entry Point