【问题标题】:Eclipse - Program Breaks when ExportedEclipse - 导出时程序中断
【发布时间】:2015-03-30 15:28:35
【问题描述】:

最近几天我一直在做一个 Java 项目,我决定将它导出到一个 jar 中进行测试。该程序在 Eclipse 中运行良好,但在 jar 文件中被破坏。此外,所有资源都位于项目内。另请注意,这不是整个项目,如果需要,请索取其他代码(很多)。

编辑:

程序现在确实在左侧显示图像,在底部显示按钮,并在顶部显示欢迎。仍然没有显示左侧文本文件的内容。

这是它在 Eclipse 中的样子:

这是 jar 文件中的样子(所有按钮都不起作用):

这是该类的代码:

package counter.main;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import javax.sound.sampled.Clip;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;

public class HomeFrame {

static JPanel panel;
public static JPanel p2;
private static JButton play = new JButton("Play");
File patch = new File(Main.class.getResource("/counter/res/ResourceCounterPatchNotes.txt").getFile());
private static JButton twitter;
private static JButton mute;
private static JButton info;
private static JButton themeChooser;
private static JLabel stoneLogs;

//private static JLabel text;
public static JLabel greet = new JLabel("", SwingConstants.CENTER);

static JFrame frame = new JFrame("Resource Counter - Home (by Grayson S)"); {
    frame.setSize(800, 520);
    frame.setLocationRelativeTo(null);
    frame.setResizable(false);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.repaint();
    frame.revalidate();

    createView();
}

private void createView() {
    setIcon();

    panel = new JPanel();
    frame.getContentPane().add(panel);
    play.setPreferredSize(new Dimension(200, 70));
    p2 = new JPanel();
    frame.getContentPane().add(p2);
    p2.setLayout(new GridBagLayout());  

    play.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                Thread.sleep(500);   
            } catch(InterruptedException ex) {
                Thread.currentThread().interrupt();
            }
            SelectionFrame.frame1.setVisible(true);
            frame.setVisible(false);
        }

    });

    p2.setLayout(new GridBagLayout());
    p2.setBackground(Color.BLACK); //sets the background color.
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.gridheight = GridBagConstraints.REMAINDER;
    gbc.weightx = 1;
    gbc.anchor = GridBagConstraints.CENTER;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.insets = new Insets(0, 8, 0, 0);

    JTextArea ta = new JTextArea(10, 20);
    ta.setForeground(Color.WHITE);
    ta.setBackground(Color.BLACK);
    ta.setFont(new Font("Lucida Sans", Font.PLAIN, 12));
    //ta.setMargin(new Insets(12, 12, 12, 12));  
    p2.add(ta, gbc);
    //gbc.fill = GridBagConstraints.VERTICAL;
    play.setFont(new Font("Segoe UI", Font.BOLD, 16));

    gbc = new GridBagConstraints();
    gbc.gridx = 1;
    gbc.gridy = 0;
    gbc.weighty = 1;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.insets = new Insets(8, 8, 0, 5);
    gbc.anchor = GridBagConstraints.NORTH;
    p2.add(info = new JButton("Info"), gbc);
    info.setMargin(new Insets(12, 12, 12, 12)); 
    gbc.gridy = 1;
    p2.add(themeChooser = new JButton("Theme Chooser"), gbc);
    themeChooser.setMargin(new Insets(12, 12, 12, 12));
    gbc.gridy = 2;
    p2.add(twitter = new JButton("Grayson's Twitter"), gbc);
    twitter.setMargin(new Insets(12, 12, 12, 12));
    gbc.gridy = 3;
    p2.add(mute = new JButton("Mute Music"), gbc);
    mute.setMargin(new Insets(12, 12, 12, 12));
    gbc.gridy = 4;
    gbc.anchor = GridBagConstraints.CENTER;
    p2.add(stoneLogs = new JLabel(""), gbc);
    gbc.gridy = 5;

    Image img = new ImageIcon(this.getClass().getResource("/counter/res/Pick2.png")).getImage();
    stoneLogs.setIcon(new ImageIcon(img));

    themeChooser.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            ThemeFrame.frame.setLocationRelativeTo(null);
            ThemeFrame.frame.setVisible(true);
        }

    });

    info.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            InfoFrame.frame.setLocationRelativeTo(null);
            InfoFrame.frame.setVisible(true);
        }

    });

    mute.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (Main.clip.isRunning()) {
                Main.clip.stop();
                mute.setText("Play Music");
            } else {
                Main.clip.loop(Clip.LOOP_CONTINUOUSLY);
                Main.clip.start();
                mute.setText("Mute Music");
            }
            {
            }
        }

    });

    twitter.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            try {

                String URL = "https://twitter.com/Graysull";
            java.awt.Desktop.getDesktop().browse(java.net.URI.create(URL));

            } catch (IOException e1) {
                e1.printStackTrace();
        }

    }});

    gbc.gridx = 0;
    gbc.anchor = GridBagConstraints.SOUTH;
    gbc.weighty = 1;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    frame.add(play, BorderLayout.SOUTH);

    frame.getContentPane().setBackground(Color.GRAY);

    try {
        ta.read(new FileReader(patch), null);
        ta.setEditable(false);
    } catch (FileNotFoundException e1) {
        e1.printStackTrace();
    } catch (IOException e1) {
        e1.printStackTrace();
    }


    greet.setFont(new Font( "Dialog", Font.BOLD, 20));
    frame.getContentPane().add(greet, BorderLayout.NORTH);
}


public void setIcon() {
    frame.setIconImage(Toolkit.getDefaultToolkit().getImage(Main.class.getResource("/counter/res/Pick.png")));
}
}

我该如何解决这个问题?感谢您的帮助。

【问题讨论】:

  • 你能给你的项目布局一个目录树吗?
  • 您可以尝试在/counter/res 的开头不使用/ 吗?
  • 我一定会试试的。我会报告结果。
  • 好的,我刚试过,没有运气。此外,当我这样做时,Eclipse 中也会出现错误。
  • jar 的扩展名更改为zip。打开它,看看你所有的资源是否都在那里

标签: java eclipse jar export


【解决方案1】:

如果它在 Eclipse 中可以正常工作,但在导出到 jar 时却不行,则可能是所需的库有问题。您可以尝试更改导出到 jar 的设置。

看看这篇文章:

stackoverflow.com/questions/6371674/

【讨论】:

  • 我尝试更改导出中的库设置,但没有成功。另外,我用WinRar打开jar,发现有26个类类似于Main$1.class,所以我删除了它们,但现在导出的Jar根本无法运行。也许 jar 试图运行 Main$1.class 类型的类?
  • 您是否也尝试过从 cmd 运行它?好的,但是如果您再次导出到 jar,那么它应该再次正常。但是如果你还没有这样做,请尝试从 cmd 运行,因为那样你会看到一条错误消息。
  • 我从 CMD 运行它,CMD 中没有显示错误。还是坏了。类名中的 $ 符号是什么意思?当我再次导出时,它们仍然存在。
  • 另外,如果有帮助,程序运行时音乐仍然会播放,因此它正在获取该资源。
  • 好的,我刚刚修改了一些代码,发现我实际上没有一个资源,所以修复了一些。但是,它没有显示文本文件的内容。
猜你喜欢
  • 2012-07-06
  • 2012-06-07
  • 2011-01-14
  • 1970-01-01
  • 2019-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多