为什么我的Eclipse可执行Jar文件无法双击运行?

3
我的Runnable Jar文件双击无法运行,我使用的是64位的Windows和Eclipse。编译时没有出现任何错误。我不确定是我的代码问题还是其他原因。
以下是代码。
import java.awt.BorderLayout;

public class Cal {
private JFrame frame;
private JTextField baseTextField;
private JTextField heightTextField;
private JTextField areaTextField;
private JTextField tBaseTextField;
private JTextField tHeightTextField;
private JTextField tAreaTextField;
private JTextField radiusTextField;
private JTextField cAreaTextField;
private JTextField diaTextField;
private JTextField circTextField;
private JTextField b1TextField;
private JTextField b2TextField;
private JTextField trapTextField;
private JTextField hTextField;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Cal window = new Cal();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public Cal() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    frame.setIconImage(Toolkit.getDefaultToolkit().getImage(
            Cal.class.getResource("/images/Triforce.jpg")));
    frame.setBounds(100, 100, 667, 595);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
    frame.getContentPane().add(tabbedPane, BorderLayout.CENTER);

    Panel panel_1 = new Panel();
    panel_1.setBackground(Color.LIGHT_GRAY);
    tabbedPane.addTab("Triangle", null, panel_1, null);
    panel_1.setLayout(null);

    JLabel lblInputHeight_1 = new JLabel("Input Height");
    lblInputHeight_1.setForeground(new Color(0, 0, 204));
    lblInputHeight_1.setBounds(10, 79, 70, 30);
    panel_1.add(lblInputHeight_1);

    JLabel lblInputBase_1 = new JLabel("Input Base");
    lblInputBase_1.setForeground(new Color(0, 0, 255));
    lblInputBase_1.setBounds(10, 319, 200, 30);
    panel_1.add(lblInputBase_1);

    tBaseTextField = new JTextField();
    tBaseTextField.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            calculateT();
        }
    });
    tBaseTextField.setBounds(10, 120, 200, 74);
    panel_1.add(tBaseTextField);
    tBaseTextField.setColumns(10);

    tHeightTextField = new JTextField();
    tHeightTextField.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            calculateT();
        }
    });
    tHeightTextField.setBounds(10, 360, 200, 74);
    panel_1.add(tHeightTextField);
    tHeightTextField.setColumns(10);

    JLabel lblArea_1 = new JLabel("Area:");
    lblArea_1.setForeground(new Color(0, 0, 255));
    lblArea_1.setBounds(289, 178, 194, 39);
    panel_1.add(lblArea_1);

    tAreaTextField = new JTextField();
    tAreaTextField.setBounds(289, 228, 194, 74);
    panel_1.add(tAreaTextField);
    tAreaTextField.setColumns(10);
    tAreaTextField.setEditable(false);

    JPanel panel = new JPanel();
    panel.setBackground(Color.LIGHT_GRAY);
    tabbedPane.addTab("Rectangle", null, panel, null);
    panel.setLayout(null);

    JLabel lblInputBase = new JLabel("Input Base");
    lblInputBase.setForeground(new Color(0, 0, 255));
    lblInputBase.setBounds(0, 91, 166, 39);
    panel.add(lblInputBase);

    baseTextField = new JTextField();
    baseTextField.setBounds(0, 131, 166, 44);
    panel.add(baseTextField);
    baseTextField.setColumns(20);
    baseTextField.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            calculateR();
        }
    });

    JLabel lblInputHeight = new JLabel("Input Height");
    lblInputHeight.setForeground(new Color(0, 0, 255));
    lblInputHeight.setBounds(0, 294, 166, 20);
    panel.add(lblInputHeight);

    heightTextField = new JTextField();
    heightTextField.setBounds(0, 325, 166, 44);
    panel.add(heightTextField);
    heightTextField.setColumns(20);
    heightTextField.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            calculateR();
        }
    });

    JLabel lblArea = new JLabel("Area:");
    lblArea.setForeground(new Color(0, 0, 255));
    lblArea.setBounds(279, 131, 161, 68);
    panel.add(lblArea);

    areaTextField = new JTextField();
    areaTextField.setBounds(274, 210, 166, 44);
    panel.add(areaTextField);
    areaTextField.setColumns(20);
    areaTextField.setEditable(false);

    Panel panel_2 = new Panel();
    panel_2.setBackground(Color.LIGHT_GRAY);
    tabbedPane.addTab("Circle", null, panel_2, null);
    panel_2.setLayout(null);

    JLabel lblInputRadius = new JLabel("Input Radius");
    lblInputRadius.setBounds(10, 131, 85, 107);
    panel_2.add(lblInputRadius);

    radiusTextField = new JTextField();
    radiusTextField.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            calculateC();
        }
    });
    radiusTextField.setBounds(105, 152, 144, 64);
    panel_2.add(radiusTextField);
    radiusTextField.setColumns(10);

    JLabel lblArea_2 = new JLabel("Area:");
    lblArea_2.setBounds(259, 155, 91, 61);
    panel_2.add(lblArea_2);

    cAreaTextField = new JTextField();
    cAreaTextField.setBounds(313, 154, 144, 61);
    panel_2.add(cAreaTextField);
    cAreaTextField.setColumns(10);

    Panel panel_3 = new Panel();
    panel_3.setBackground(Color.LIGHT_GRAY);
    tabbedPane.addTab("Circumference", null, panel_3, null);
    panel_3.setLayout(null);

    JLabel lblInputDiameter = new JLabel("Input Diameter");
    lblInputDiameter.setBounds(10, 146, 97, 61);
    panel_3.add(lblInputDiameter);

    diaTextField = new JTextField();
    diaTextField.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            calculateCirc();
        }
    });
    diaTextField.setBounds(117, 146, 126, 61);
    panel_3.add(diaTextField);
    diaTextField.setColumns(10);

    JLabel lblArea_3 = new JLabel("Circumference");
    lblArea_3.setBounds(253, 146, 86, 61);
    panel_3.add(lblArea_3);

    circTextField = new JTextField();
    circTextField.setBounds(367, 146, 126, 61);
    panel_3.add(circTextField);
    circTextField.setColumns(10);

    Panel panel_4 = new Panel();
    panel_4.setBackground(Color.LIGHT_GRAY);
    tabbedPane.addTab("Trapizoid", null, panel_4, null);
    panel_4.setLayout(null);

    JLabel lblBase = new JLabel("Input Base 1");
    lblBase.setBounds(0, 65, 72, 55);
    panel_4.add(lblBase);

    JLabel lblInputBase_2 = new JLabel("Input Base 2");
    lblInputBase_2.setBounds(0, 325, 72, 43);
    panel_4.add(lblInputBase_2);

    b1TextField = new JTextField();
    b1TextField.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            calculateTrap();
        }
    });
    b1TextField.setBounds(82, 65, 129, 55);
    panel_4.add(b1TextField);
    b1TextField.setColumns(10);

    b2TextField = new JTextField();
    b2TextField.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            calculateTrap();
        }
    });
    b2TextField.setBounds(82, 319, 129, 55);
    panel_4.add(b2TextField);
    b2TextField.setColumns(10);

    JLabel lblArea_4 = new JLabel("Area:");
    lblArea_4.setBounds(278, 179, 72, 55);
    panel_4.add(lblArea_4);

    trapTextField = new JTextField();
    trapTextField.setBounds(360, 179, 129, 55);
    panel_4.add(trapTextField);
    trapTextField.setColumns(10);

    JLabel lblInputHeight_2 = new JLabel("Input Height");
    lblInputHeight_2.setBounds(0, 179, 72, 55);
    panel_4.add(lblInputHeight_2);

    hTextField = new JTextField();
    hTextField.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            calculateTrap();
        }
    });
    hTextField.setBounds(82, 179, 129, 55);
    panel_4.add(hTextField);
    hTextField.setColumns(10);
}

private void calculateR() {
    try {
        double base = Double.parseDouble(baseTextField.getText());
        double height = Double.parseDouble(heightTextField.getText());
        double area = base * height;
        areaTextField.setText(String.format("%.4f", area));
    } catch (NumberFormatException e) {
        areaTextField.setText("NaN");
    }
}

private void calculateT() {
    try {
        double tBase = Double.parseDouble(tBaseTextField.getText());
        double tHeight = Double.parseDouble(tHeightTextField.getText());
        double preArea = tBase * tHeight;
        double Area = preArea / 2;
        tAreaTextField.setText(String.format("%.4f", Area));
    } catch (NumberFormatException e) {
        tAreaTextField.setText("NaN");
    }
}

private void calculateC() {
    try {
        double Radius = Double.parseDouble(radiusTextField.getText());
        double preArea = Radius * Radius;
        double Area = preArea * 3.14;
        cAreaTextField.setText(String.format("%.4f", Area));
    } catch (NumberFormatException e) {
        cAreaTextField.setText("NaN");
    }
}

private void calculateCirc() {
    try {
        double diameter = Double.parseDouble(diaTextField.getText());
        double Circumference = diameter * 3.14;
        circTextField.setText(String.format("%.4f", Circumference));
    } catch (NumberFormatException e) {
        circTextField.setText("NaN");
    }
}

private void calculateTrap() {
    try {
        double base1 = Double.parseDouble(b1TextField.getText());
        double base2 = Double.parseDouble(b2TextField.getText());
        double height = Double.parseDouble(hTextField.getText());
        double preArea = base1 + base2 / 2;
        double Area = preArea * height;
        trapTextField.setText(String.format("%.4f", Area));
    } catch (NumberFormatException e) {
        trapTextField.setText("NaN");
    }
}
}

你能否从命令行运行它? - jsedano
不行,我收到了错误信息,无法访问Calculator.jar文件。 - user2585424
@user2585424 你用什么命令在命令行中运行它? - Jeffrey
3个回答

3

您的代码没有问题,它将可以编译。

首先,请确保您的主类是Cal.java。这可以通过右键单击项目 属性 -> 运行/调试设置 -> 单击配置(或创建新的配置) -> 编辑 -> 选择一个类 来完成。

接下来,请确保将项目导出为Runnable JAR文件。这可以通过右键单击项目 导出... -> Runnable JAR文件(在Java文件夹下) -> 下一步 -> 选择与上述相同的配置 来完成。

您应该能够双击JAR文件,或在命令提示符/终端中使用以下命令:java -jar FileName.jar

这应该解决您的问题。


0

此外,您可能需要检查确保 JAR 文件与其他所有文件都在主文件夹中... 我自己 5 分钟前也遇到了这个问题。将其放入邮件文件夹中可解决我的问题... 如果您仍然需要将其保存到该位置,但仍需要使用资源文件夹进行保存的话,我不知道保存到资源文件夹有什么意义,但这对我有效。


0
你可以试试这个方法,它曾经帮助过我:
  1. 下载jarfix
  2. 运行你的 jarfix.exe 文件。
  3. 然后运行你的 jar 文件。
关于 jarfix:它将你的 jar 文件与 javaw.exe 进程关联起来。
希望能对你有所帮助!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接