ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi...
I have the following code in java.
Its a game "Random guessing-Memory Game".
It loads different .gif files and .wav files in the same folder where the main class Memory.java is.
Now its running fine with java command used to run Memory class in terminal but when I create jar file its not running properly.
Also there are some files in the folder Memory$1.class , Memory$2.class....so on to $5.. what are these for
Actually I have downloaded the program source code with all the other files to see hw the game was working....
Please help how I should create jar file.
Code:
/*
* Memory.java
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import java.applet.*;
import java.util.Random;
public class Memory extends JFrame
{
static JLabel guessLabel = new JLabel();
static JLabel label0 = new JLabel();
static JLabel label1 = new JLabel();
static JLabel label2 = new JLabel();
static JLabel label3 = new JLabel();
static JLabel label4 = new JLabel();
static JLabel label5 = new JLabel();
static JLabel label6 = new JLabel();
static JLabel label7 = new JLabel();
static JLabel label8 = new JLabel();
static JLabel label9 = new JLabel();
static JLabel label10 = new JLabel();
static JLabel label11 = new JLabel();
static JLabel label12 = new JLabel();
static JLabel label13 = new JLabel();
static JLabel label14 = new JLabel();
static JLabel label15 = new JLabel();
static JLabel[] boxLabel = new JLabel[16];
static ImageIcon apple = new ImageIcon("apple.gif");
static ImageIcon banana = new ImageIcon("banana.gif");
static ImageIcon cherry = new ImageIcon("cherry.gif");
static ImageIcon grape = new ImageIcon("grape.gif");
static ImageIcon lemon = new ImageIcon("lemon.gif");
static ImageIcon orange = new ImageIcon("orange.gif");
static ImageIcon pear = new ImageIcon("pear.gif");
static ImageIcon plum = new ImageIcon("plum.gif");
static ImageIcon backing = new ImageIcon("backing.gif");
static ImageIcon[] choiceIcon = new ImageIcon[8];
static JButton newButton = new JButton();
static JButton exitButton = new JButton();
static Random myRandom = new Random();
static int choice;
static int index;
static int[] picked = new int[2];
static int[] behind = new int[16];
static int guesses;
static int remaining;
static AudioClip matchSound;
static AudioClip noMatchSound;
static Timer delayTimer;
public static void main(String args[])
{
// create frame
new Memory().show();
// get sounds
try
{
matchSound = Applet.newAudioClip(new URL("file:" + "tada.wav"));
noMatchSound = Applet.newAudioClip(new URL("file:" + "uhoh.wav"));
}
catch (Exception ex)
{
}
// start first game
newButton.doClick();
}
public Memory()
{
// frame constructor
setTitle("Memory Game");
// setResizable(false);
getContentPane().setBackground(Color.BLUE);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent evt)
{
exitForm(evt);
}
});
getContentPane().setLayout(new GridBagLayout());
// position controls
GridBagConstraints gridConstraints = new GridBagConstraints();
guessLabel.setText("Guesses: 0");
guessLabel.setForeground(Color.WHITE);
guessLabel.setFont(new Font("Arial", Font.BOLD, 18));
gridConstraints.gridx = 1;
gridConstraints.gridy = 0;
gridConstraints.gridwidth = 2;
gridConstraints.insets = new Insets(10, 10, 10, 10);
getContentPane().add(guessLabel, gridConstraints);
boxLabel[0] = label0;
boxLabel[1] = label1;
boxLabel[2] = label2;
boxLabel[3] = label3;
boxLabel[4] = label4;
boxLabel[5] = label5;
boxLabel[6] = label6;
boxLabel[7] = label7;
boxLabel[8] = label8;
boxLabel[9] = label9;
boxLabel[10] = label10;
boxLabel[11] = label11;
boxLabel[12] = label12;
boxLabel[13] = label13;
boxLabel[14] = label14;
boxLabel[15] = label15;
int x = 0;
int y = 1;
for (int i = 0; i < 16; i++)
{
gridConstraints = new GridBagConstraints();
boxLabel[i].setPreferredSize(new Dimension(70, 70));
boxLabel[i].setIcon(backing);
gridConstraints.gridx = x;
gridConstraints.gridy = y;
gridConstraints.insets = new Insets(5, 5, 5, 5);
getContentPane().add(boxLabel[i], gridConstraints);
boxLabel[i].addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
labelMouseClicked(e);
}
});
x++;
if (x > 3)
{
x = 0;
y += 1;
}
}
newButton.setText("New Game");
gridConstraints = new GridBagConstraints();
gridConstraints.gridx = 1;
gridConstraints.gridy = 6;
gridConstraints.gridwidth = 2;
gridConstraints.insets = new Insets(10, 10, 10, 10);
getContentPane().add(newButton, gridConstraints);
newButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
newButtonActionPerformed(e);
}
});
exitButton.setText("Exit");
gridConstraints = new GridBagConstraints();
gridConstraints.gridx = 1;
gridConstraints.gridy = 7;
gridConstraints.gridwidth = 2;
gridConstraints.insets = new Insets(0, 10, 10, 10);
getContentPane().add(exitButton, gridConstraints);
exitButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
exitButtonActionPerformed(e);
}
});
delayTimer = new Timer(1, new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
delayTimerActionPerformed(e);
}
});
pack();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
setBounds((int) (0.5 * (screenSize.width - getWidth())), (int) (0.5 * (screenSize.height - getHeight())), getWidth(), getHeight());
choiceIcon[0] = apple;
choiceIcon[1] = banana;
choiceIcon[2] = cherry;
choiceIcon[3] = grape;
choiceIcon[4] = lemon;
choiceIcon[5] = orange;
choiceIcon[6] = pear;
choiceIcon[7] = plum;
}
private void labelMouseClicked(MouseEvent e)
{
Component clickedComponent = e.getComponent();
for (index = 0; index < 16; index++)
{
if (clickedComponent == boxLabel[index])
{
break;
}
}
// If trying to pick same box, picking already selected box
// or trying pick when not playing, exit
if ((choice == 1 && index == picked[0]) || behind[index] == -1 || newButton.isEnabled())
{
return;
}
// Display selected picture
boxLabel[index].setIcon(choiceIcon[behind[index]]);
if (choice == 0)
{
picked[0] = index;
choice = 1;
return;
}
// use timer to process remaining code to allow
// label control to refresh
delayTimer.start();
}
private void newButtonActionPerformed(ActionEvent e)
{
guesses = 0;
remaining = 8;
guessLabel.setText("Guesses: 0");
// Randomly sort 16 integers using Shuffle routine
// Behind contains indexes (0 to 7) for hidden pictures
behind = sortIntegers(16);
for (int i = 0; i < 16; i++)
{
// reset image
boxLabel[i].setIcon(backing);
if (behind[i] > 7)
{
behind[i] = behind[i] - 8;
}
}
choice = 0;
newButton.setEnabled(false);
exitButton.setText("Stop");
}
private void exitButtonActionPerformed(ActionEvent e)
{
if (exitButton.getText().equals("Exit"))
{
exitForm(null);
}
else
{
exitButton.setText("Exit");
newButton.setEnabled(true);
}
}
private void exitForm(WindowEvent evt)
{
System.exit(0);
}
private void delayTimerActionPerformed(ActionEvent e)
{
// finish processing of display
delayTimer.stop();
guesses++;
guessLabel.setText("Guesses: " + String.valueOf(guesses));
picked[1] = index;
if (behind[picked[0]] == behind[picked[1]])
{
// If match, play sound
matchSound.play();
behind[picked[0]] = -1;
behind[picked[1]] = -1;
remaining--;
}
else
{
// If no match, blank picture, restore backs
noMatchSound.play();
// delay 1 second
long t = System.currentTimeMillis();
do {} while (System.currentTimeMillis() - t < 1000);
boxLabel[picked[0]].setIcon(backing);
boxLabel[picked[1]].setIcon(backing);
}
choice = 0;
if (remaining == 0)
{
exitButton.doClick();
newButton.requestFocus();
}
}
private static int[] sortIntegers(int n)
{
/*
* Returns n randomly sorted integers 0 -> n - 1
*/
int nArray[] = new int[n];
int temp, s;
Random myRandom = new Random();
// initialize array from 0 to n - 1
for (int i = 0; i < n; i++)
{
nArray[i] = i;
}
// i is number of items remaining in list
for (int i = n; i >= 1; i--)
{
s = myRandom.nextInt(i);
temp = nArray[s];
nArray[s] = nArray[i - 1];
nArray[i - 1] = temp;
}
return(nArray);
}
}
Exception in thread "main" java.lang.NoClassDefFoundError: Memory$1
at Memory.<init>(Memory.java:79)
at Memory.main(Memory.java:59)
Caused by: java.lang.ClassNotFoundException: Memory$1
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:319)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:264)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:332)
... 2 more"
Whats going wrong in creating jar file.
I'm not editing the MANIFEST.MF file properly or there is other problem.
I have done the above procedure many times and all the times I save save the changes in the MANIFEST.M file and end the line with return to end it.
If you open your Memory.jar, you'll probably notice that there are no Memory$*.class files therein - that's because when you type "Memory$1.jar", the shell interprets the "$1" part as a variable, which it then replaces with the value of the variable. In this case it happens to be empty (if no arguments were passed to the shell...).
The long-and-short is that the shell sees "Memory$1.jar" and turns it into "Memory.jar", which is what the program sees, which is valid so no error comes up.
To fix, the easiest way would probably be to replace all the Memory.class, Memory$1.class... names with Memory*.class:
This will also make sure that when you add more anonymous inner classes, they're included by default.
You can also enclose the arguments in single (not double) quotes (jar cf 'Memory$1.class' ...) or use a backslash before the dollar sign (jar cf Memory\$1.class ...).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.