I now have teo classes the first one contains this

import java.awt.*;
import javax.swing.*;

class fm1 extends JPanel
{
	public fm1()
	{
		this.setLayout(new GridLayout(3,0));
		Dimension D1 = new Dimension (300, 300);

		JLabel fruit1 = new JLabel();
		fruit1.setPreferredSize(D1);		
		fruit1.setOpaque(true);
		this.add(fruit1);

		JLabel fruit2 = new JLabel();
 		fruit2.setPreferredSize(D1);		
		fruit2.setOpaque(true);
		this.add(fruit2);

		JLabel fruit3 = new JLabel();
		fruit3.setPreferredSize(D1);		
		fruit3.setOpaque(true);
		this.add(fruit1);
	}
}

This is the Standalone Class and it compiles fine

I then have this one

import java.awt.*;
import javax.swing.*;

class FruitWnd
{
    public static void main(String[] args) 
    {
	// Create and set up the JFrame
        JFrame myWindow = new JFrame("Fruit Machine");
        
        // Set the default close operation of the JFrame object to dispose
       	myWindow.setDefaultCloseOperation(3);
        
  		// Instance the standalone class containing the components
  		fm1 FruitMachine = new fm1();
      	
      	// Add intermediate container to top level container
        myWindow.add(FruitMachine,BorderLayout.CENTER);
        
		// Display the window.
        myWindow.pack();
        myWindow.setVisible(true);
	    
    }// end of main method
    
}// end of class

When i Try to compile it it says that it cant find the fm1 class symbol. however on a very similar code it will compile it and i cant see the difference in order for me to solve it. can anyone help?

Recommended Answers

All 13 Replies

Are both classes in the same folder?

Are both classes in the same folder?

the two java files are in same folder, the second class hasnt been compiled so there isnt two .class files

Are your class files in a different folder than your source files? Do the file names match the class names?

yes and yes (im assuming by source files you mean the .java files) the two files are fm1.java and FruitWnd.java

If it helps heres the programs i based them on

fm1.java is based on TilePanelA.java which is

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

class TilePanelA extends JPanel implements MouseListener
{
	public TilePanelA() 
	{
		this.setLayout(new GridLayout(3,0));
		Dimension D1 = new Dimension (230, 443);
				
		JLabel L1 = new JLabel();
		L1.setPreferredSize(D1);		
		L1.setBackground(Color.WHITE);
		L1.setOpaque(true);
		this.add(L1);
				
		JLabel L2 = new JLabel();  
		L2.setPreferredSize(D1);
		L2.setBackground(Color.WHITE);
		L2.setOpaque(true);
		this.add(L2);
				
		JLabel L3 = new JLabel(); 
		L3.setPreferredSize(D1); 
		L3.setBackground(Color.WHITE);
		L3.setOpaque(true);
		this.add(L3);
		
		JLabel L4 = new JLabel();  
		L4.setPreferredSize(D1);
		L4.setBackground(Color.WHITE);
		L4.setOpaque(true);
		this.add(L4);
		
		JLabel L5 = new JLabel();  
		L5.setPreferredSize(D1);
		L5.setBackground(Color.WHITE);
		L5.setOpaque(true);
		this.add(L5);
		
		JLabel L6 = new JLabel();  
		L6.setPreferredSize(D1);
		L6.setBackground(Color.WHITE);
		L6.setOpaque(true);
		this.add(L6);
		
		JLabel L7 = new JLabel();  
		L7.setPreferredSize(D1);
		L7.setBackground(Color.WHITE);
		L7.setOpaque(true);
		this.add(L7);
		
		JLabel L8 = new JLabel();  
		L8.setPreferredSize(D1);
		L8.setBackground(Color.WHITE);
		L8.setOpaque(true);
		this.add(L8);
		
		JLabel L9 = new JLabel();
		L9.setPreferredSize(D1);
		L9.setBackground(Color.WHITE);
		L9.setOpaque(true);
		this.add(L9);
	} // end of constructor

	public void mouseEntered (MouseEvent me)
	{
	}
	public void mouseExited (MouseEvent me)
	{
	}
	public void mousePressed (MouseEvent me)
	{
	}
	public void mouseReleased (MouseEvent me)
	{
	}
	public void mouseClicked (MouseEvent me)
	{
		Color labelColor = me.getComponent().getBackground();
		if( labelColor == Color.WHITE)
		{
			me.getComponent().setBackground(Color.BLACK);	
		}
		else
		{
			me.getComponent().setBackground(Color.WHITE);
		}
	}
} // end of class

the Main difference between the two is the bits about the action listener and the number of panels

FruitWnd.java is based on tileExercise.java which is:

import java.awt.*;
import javax.swing.*;

class TileExercise
{
    public static void main(String[] args) 
    {
	// Create and set up the JFrame
        JFrame myWindow = new JFrame("Tile Exercise");
        
        // Set the default close operation of the JFrame object to dispose
       	myWindow.setDefaultCloseOperation(3);
        
  		// Instance the standalone class containing the components
  		TilePanelA tpa = new TilePanelA();
      	
      	// Add intermediate container to top level container
        myWindow.add(tpa,BorderLayout.CENTER);
        
		// Display the window.
        myWindow.pack();
        myWindow.setVisible(true);
	    
    }// end of main method
    
}// end of class

TilePanelA and tileExercise both compile correctly

Well I run them at my computer and there is no syntax error. Your class cannot find the fm1

well i knew that i want to know why it cant and how to make it so it can be found

Well, not knowing your exact directory structures and where your output class files are located (you said they were in different directories), I would recommend compiling all of those java files at the same time with javac *.java . It's just a simple classpath problem that you are encountering. There is nothing wrong with the code itself.

Thnx for that command it compiled the FruitWnd class, however when i tried to run the code it didnt work i got this error in the cmd output

C:\Users\Chris\Documents\Java\ICA2\Source and classes>java FruitWnd
Exception in thread "main" java.lang.NoClassDefFoundError: FruitWnd
Caused by: java.lang.ClassNotFoundException: FruitWnd
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
Could not find the main class: FruitWnd. Program will exit.

whats causing the error

note that FruitWnd.java is almost identical to TileExercise.java which does compile and run

Are you running it from the directory that contains that class file?

the .java files and the .class files are both in the same directory which is C:\Users\Chris\Documents\Java\ICA2\Source and classes>

unless you mean the fm1 class being in the fm1.java file instead of the Fruitwnd.java file

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.