Help with a basic app (JButton, images into a jar file)

0 Alex Becker · July 23, 2014
Hello,
In order to create a very simple application (I'm a beginner), I followed Bucky's JButton tutorials in the first Java Tutorial series. It works fine and runs perfectly in Eclipse. If I export it into a .jar file and run it on the computer I coded it with, it also works great. The problems only start to pop up when I send that .jar file (alone and with the folder, nothing I send changes anything) to another computer. I look at the console and it says that it can't find the image. So my question is, how can I export a .jar file and include the picture I used in the code inside that .jar file. This is like the 3rd website I ask this question on and so far nothing has worked (it might also be because I'm majorly incompetent but hey it's worth another shot).

**Note: I replaced all irrelevant output text with "WORDS" for privacy reasons.**

Here is everything I've got...

My main "apples" class.
import javax.swing.JFrame;

public class apples {
public static void main(String[] args) {

Gui go = new Gui();
go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
go.setSize(1920,1080);
go.setVisible(true);

}

}


My second "Gui" class.
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

public class Gui extends JFrame {

private JButton custom;

public Gui () {
super("WORDS");
setLayout(new FlowLayout());

Icon b = new ImageIcon(getClass().getResource("b.png"));
custom = new JButton(null, b);
custom.setToolTipText("WORDS");
add(custom);

HandlerClass handler = new HandlerClass();
custom.addActionListener(handler);

}

private class HandlerClass implements ActionListener {
public void actionPerformed(ActionEvent event) {
JOptionPane.showMessageDialog(null, String.format("WORDS", event.getActionCommand()));
JOptionPane.showMessageDialog(null, String.format("WORDS", event.getActionCommand()));
JOptionPane.showMessageDialog(null, String.format("WORDS", event.getActionCommand()));
JOptionPane.showMessageDialog(null, String.format("WORDS", event.getActionCommand()));


}

}

}



Thank you,
Alex

Post a Reply

Replies

Oldest  Newest  Rating
0 Alex Becker · August 2, 2014
I know, but something did go wrong. And there's no apparent reason.

Here is the debug screen:
apples [Java Application]
apples at localhost:55091
Thread [main] (Suspended (exception ClassNotFoundException))
URLClassLoader$1.run() line: 202 [local variables unavailable]
AccessController.doPrivileged(PrivilegedExceptionAction<T>, AccessControlContext) line: not available [native method]
Launcher$AppClassLoader(URLClassLoader).findClass(String) line: 190
Launcher$AppClassLoader(ClassLoader).loadClass(String, boolean) line: 306
Launcher$AppClassLoader.loadClass(String, boolean) line: 301
Launcher$AppClassLoader(ClassLoader).loadClass(String) line: 247
Daemon Thread [Thread-1] (Running)
/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/bin/java (Jul 26, 2014 6:52:42 AM)
apples [Java Application]
apples at localhost:59991
Thread [main] (Suspended (exception ClassNotFoundException))
URLClassLoader$1.run() line: 202
AccessController.doPrivileged(PrivilegedExceptionAction<T>, AccessControlContext) line: not available [native method]
Launcher$AppClassLoader(URLClassLoader).findClass(String) line: 190
Launcher$AppClassLoader(ClassLoader).loadClass(String, boolean) line: 306
Launcher$AppClassLoader.loadClass(String, boolean) line: 301
Launcher$AppClassLoader(ClassLoader).loadClass(String) line: 247
Daemon Thread [Thread-1] (Running)
/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/bin/java (Aug 2, 2014 7:04:57 AM)
0 Alex Becker · July 25, 2014
Ok here are the screenshots of everything, try to forget you saw the words in quotes please.

http://imgur.com/a/pUBcJ

Hope this helps. And thank you, I would really love this to work.
-Alex
0 Alex Becker · July 25, 2014
Ok I followed everything you said and the program works, apart from the SwingUtilities.invokeLater(Gui::new);

Here are screenshots of both Apples and Gui. The error in Apples creates the errors in Gui.

http://imgur.com/a/4jTob
0 Alex Becker · July 25, 2014
Well now there are no more errors whatsoever, but when I run the program I get this error in the console:
Exception in thread "main" java.lang.NoClassDefFoundError: apples

Caused by: java.lang.ClassNotFoundException: apples
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
0 Alex Becker · July 25, 2014
Strait in Eclipse, in the IDE.
0 Alex Becker · July 26, 2014
I did but it still didn't work (same error). Here are screenshots explaining it all.

http://imgur.com/a/rVOqU

Ps: the final image (debug) is because I chose "Debug as java application" to see what would happen, I did do the "Run as" first.
  • 1

Java / Android Development

106,994 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator