LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-16-2009, 11:59 PM   #1
BlueSkull
Member
 
Registered: Sep 2009
Posts: 73

Rep: Reputation: 17
Question Problem in Creating jar file of a java project


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

	}



}
 
Old 12-17-2009, 03:48 AM   #2
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 139Reputation: 139
Quote:
Originally Posted by BlueSkull View Post
Also there are some files in the folder Memory$1.lass , Memory$2.class....so on to $5.. what are these for
If I remember correctly, these are the .class files for any anonymous inner classes you define.

As for how to create .jar files, see the helpful tutorial at: http://java.sun.com/docs/books/tutor...jar/index.html.
 
Old 12-19-2009, 04:17 AM   #3
BlueSkull
Member
 
Registered: Sep 2009
Posts: 73

Original Poster
Rep: Reputation: 17
I was trying to create jar file using following commands in Terminal:

aduait@BlueSkull:~/Desktop/MemoryProject$jar cf Memory.jar Memory.class apple.gif lemon.gif Memory$4.class MemoryProject.jcp tada.wav backing.gif Memory Memory$5.class orange.gif uhoh.wav banana.gif Memory$1.class pear.gif cherry.gif Memory$2.class plum.gif grape.gif Memory$3.class Memory.java src_memoryproject.txt

after creating jar file I edit the MANIFEST.MF file and add the line Main-Class: Memory at last
saved it
and run the command

aduait@BlueSkull:~/Desktop/MemoryProject$ java -jar Memory.jar

but it shows the following errors

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.

Any suggestions.
 
Old 12-20-2009, 10:47 AM   #4
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 139Reputation: 139
Quote:
Originally Posted by BlueSkull View Post
I was trying to create jar file using following commands in Terminal:

aduait@BlueSkull:~/Desktop/MemoryProject$jar cf Memory.jar Memory.class apple.gif lemon.gif Memory$4.class MemoryProject.jcp tada.wav backing.gif Memory Memory$5.class orange.gif uhoh.wav banana.gif Memory$1.class pear.gif cherry.gif Memory$2.class plum.gif grape.gif Memory$3.class Memory.java src_memoryproject.txt
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:

Code:
$ jar cf Memory.jar Memory*.class apple.gif lemon.gif MemoryProject.jcp tada.wav backing.gif Memory orange.gif uhoh.wav banana.gif pear.gif cherry.gif plum.gif grape.gif Memory.java src_memoryproject.txt
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 ...).
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
creating jar file sajith Programming 2 05-16-2008 11:57 PM
package java program into .jar file rabbit2345 Programming 4 01-23-2008 07:41 AM
Where do I install a Java .jar file? cumbersome Slackware 1 04-22-2006 01:46 PM
How to run a java jar file. sabliny Programming 7 11-05-2005 01:59 PM
Fedora Core 1 Associating .jar files with java -jar command pymehta Fedora 0 01-13-2005 05:26 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:06 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration