4

I have been working on a Java project the last couple days and I decided to export it to a jar for a test run. The program works perfectly fine in Eclipse, yet is broken in a jar file. Also, all of the resources are located within the project. Also note that this is not the entire project, please request the other code if needed (its a lot).

EDIT:

Program now does show image on the left, button on the bottom, and welcome at the top. Still does not show the contents of a text file on the left.

Here is what it looks like in Eclipse:

enter image description here

Here is what is looks like in a jar file (none of the buttons work):

enter image description here

Here is the code for that class:

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")));
}
}

enter image description here

enter image description here

How do I fix this this? Help is appreciated.

Graysull
  • 109
  • 2
  • 8

1 Answers1

1

If it works fine in eclipse but not when you export to jar, it might be something wrong with the libraries required. You could try changing the settings for exporting to jar.

Have a look at this post:

stackoverflow.com/questions/6371674/

Johan
  • 475
  • 4
  • 17
  • I tried changing the library settings in the export, no luck. Also, I opened the jar with WinRar and I found that there were 26 classes that were like Main$1.class, so I deleted them but now the exported Jar won't run at all. Perhaps the jar was trying to run the Main$1.class type classes? – Graysull Jan 31 '15 at 18:28
  • Have you tried running it from the cmd too? Ok but if you export to jar again then it should be as normal again. But try run from cmd if you haven't done that, because then you would se an error message. – Johan Jan 31 '15 at 20:25
  • I ran it from CMD, no error showed in CMD. Still broken. And what does the $ sign mean in the class name? They are still there when I export it again. – Graysull Jan 31 '15 at 20:31
  • Also, if it helps, the music still plays when the program runs, so it is getting that resource. – Graysull Jan 31 '15 at 20:36
  • Ok I just modified some code and I found out that I actually didn't have one of the resources, so some of it is fixed. However, it is not showing contents of text files. – Graysull Jan 31 '15 at 20:57
  • I created a question just for the text file error, see: http://stackoverflow.com/questions/28256766/java-program-not-showing-contents-of-text-file-in-jar – Graysull Jan 31 '15 at 21:33
  • Saw that it was fixed, nice! – Johan Jan 31 '15 at 22:30