Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Topics

Recent Status Updates

View All Updates

Photo
- - - - -

Problem in Creating jar file of a java project

timer setpreferredsize

  • Please log in to reply
6 replies to this topic

#1 BlueSkull

BlueSkull

    CC Newcomer

  • Just Joined
  • PipPip
  • 15 posts

Posted 16 December 2009 - 11:08 PM

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.

/*

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

	}



}

  • 0

#2 patilsneha

patilsneha

    CC Lurker

  • Just Joined
  • Pip
  • 4 posts

Posted 17 December 2009 - 02:51 AM

hi,
for this try to use Eclipse software. I think it is much easier to create jar file in eclipses.
  • 0

#3 BlueSkull

BlueSkull

    CC Newcomer

  • Just Joined
  • PipPip
  • 15 posts

Posted 17 December 2009 - 11:06 PM

Hi
Thanks for suggesting eclipse IDE.
I have open the Memory.java file in it from file-> open file option.. But how to import all the other files I mentioned earlier in IDE.
When I click on Run it shows ant build and then shows error Build failed: reason: unable to find an ant file to run.

Also tell me the procedure to create jar file in eclipse.
thanks in advance.
  • 0

#4 patilsneha

patilsneha

    CC Lurker

  • Just Joined
  • Pip
  • 4 posts

Posted 18 December 2009 - 12:48 AM

hi
follow the steps given below hope it will help
1. from FILE menu select new project (Memory.java)after right click on it select new class (Memory).
2. save the project and now run it.
3. In the File menu select EXPORT=> JAVA => JAR FILE => NEXT =.select your program and also give export destination for jar file =>next.
4. if you wish to edit the text in jar file or see the text then click ob the save the description of jar file in workspace and give the location
e.g./Memory.java/memory.jardesc
then you will get a jar file in text and also executable file in the given location.
  • 0

#5 BlueSkull

BlueSkull

    CC Newcomer

  • Just Joined
  • PipPip
  • 15 posts

Posted 18 December 2009 - 08:18 AM

Thanks Now I know how to compile .java files and make .jar files but still there some problem........

when I compile the code using javac command line and then run with java the GUI is like........
Image at the link (flickr.com/photos/aduait/4195435574/)

and when I make jar file according to above steps the GUI is like..............
Image at url
(flickr.com/photos/aduait/4195435576/)

I think its because program calls apple.gif,orange.gif and many other .gif files and .wav files too.

How I should I include then to make .jar file..
  • 0

#6 patilsneha

patilsneha

    CC Lurker

  • Just Joined
  • Pip
  • 4 posts

Posted 18 December 2009 - 09:47 PM

hello,

i am sorry but i cant say anything about it right now. I have to read to answer this question this. as I come to know i will reply you...
thanks
  • 0

#7 BlueSkull

BlueSkull

    CC Newcomer

  • Just Joined
  • PipPip
  • 15 posts

Posted 19 December 2009 - 02:16 AM

In starting 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.
  • 0





Also tagged with one or more of these keywords: timer, setpreferredsize

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download