hi Guys,

Just started java and so far not too bad but i tried to sort out packages today and got this error. Stuck on it for hours. I'm not sure if im importing or setting up the pacakages properly but that where I'm hoping you can help... Im dealing with swing. il post both classes. Hopefully you can see what I have done wrong. P.S. The classpath and jdk are set properly(I think....)

CLASS 1

package myJava.SimpleFrame.SimpleFrameDriver;

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

public class SimpleFrameDriver 
{
	public static void main(String[] args)
	{
		SimpleFrame sFrame1 = new SimpleFrame();
		SimpleFrame sFrame2 = new SimpleFrame();
		
		sFrame1.showIt("SimpleFrame 1");
		sFrame2.showIt("SimpleFrame 2", 300,300);
	}
}

CLASS 2

package myJava.SimpleFrame.SimpleFrame;

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

public class SimpleFrame extends JFrame
{
	public SimpleFrame()
	{
		this.setSize(200,200);
		this.setLocation(200,200);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	//MAKE THE FRAME VISIBLE
	
	public void showIt()
	{
		this.setVisible(true);
	}
	
	//MAKES THE FRAME VISIBLE AND SETS THE TITLE TEXT.
	
	public void showIt(String title)
	{
		this.setTitle(title);
		this.setVisible(true);
	}
	
	
	public void showIt(String title,int x, int y)
	{
		this.setTitle(title);
		this.setLocation(x,y);
		this.setVisible(true);
	}
	
	//MAKES THE FRAME INVISIBLE.
	
	public void hideIt()
	{
		this.setVisible(false);
	}

}

and the error is:

SimpleFrameDriver.java:12:cannot find symbol
symbol: class SimpleFrame
loaction:class myJava.SimpleFrame.SimpleFrameDriver.SimpleFrameDriver
SimpleFrame sFrame2 = new SimpleFrame();

It's a problem with the package names - you seem to have the class name at the end of the package names, which is syntactically valid, but not a normal thing to do.
I would start by putting them both in the same package
package myJava.SimpleFrame;
and there's no need to import that package in either class

It's a problem with the package names - you seem to have the class name at the end of the package names, which is syntactically valid, but not a normal thing to do.
I would start by putting them both in the same package
package myJava.SimpleFrame;
and there's no need to import that package in either class

I am still getting an error at these lines:

SimpleFrame sFrame1 = new SimpleFrame();
        SimpleFrame sFrame2 = new SimpleFrame();

It doesn't seem to be able to find the SimpleFrame object to instantiate it.... AHH

P.S Huge thanks for your reply.

What is the exact error message now?
What is the exact folder structure in which you place your class files?

What is the exact error message now?
What is the exact folder structure in which you place your class files?

there are 4 error messages..... all pointing to the driver files where i instantiate the objects..

the files are in c:/myJava/SampleFrame/

Error Messages:


C:\myJava\SimpleFrameExam>javac SimpleFrameDriver.java
SimpleFrameDriver.java:8: cannot find symbol
symbol : class SimpleFrame
location: class myJava.SimpleFrameExam.SimpleFrameDriver
SimpleFrame sFrame1 = new SimpleFrame();
^
SimpleFrameDriver.java:8: cannot find symbol
symbol : class SimpleFrame
location: class myJava.SimpleFrameExam.SimpleFrameDriver
SimpleFrame sFrame1 = new SimpleFrame();
^
SimpleFrameDriver.java:9: cannot find symbol
symbol : class SimpleFrame
location: class myJava.SimpleFrameExam.SimpleFrameDriver
SimpleFrame sFrame2 = new SimpleFrame();
^
SimpleFrameDriver.java:9: cannot find symbol
symbol : class SimpleFrame
location: class myJava.SimpleFrameExam.SimpleFrameDriver
SimpleFrame sFrame2 = new SimpleFrame();
^
4 errors

C:\myJava\SimpleFrameExam>

Briefly, the rule is that each public class in a package must be in a file with the class name as its file name, in a directory path that corresponds to the package (Starting from anywhere in the system classpath).
eg if you have a class zzz in a package xxx.yyy

import xxx.yyy;
...
new zzz();

Java will look for a directory xxx in the classpath. In that dir it will look for a directory yyy, and in that directory it will look for zzz.class

You have to go thru all your classes and packages to ensure that this rule is always followed.

ps: a jar file is equivalent toa directory for this rule.

Briefly, the rule is that each public class in a package must be in a file with the class name as its file name, in a directory path that corresponds to the package (Starting from anywhere in the system classpath).
eg if you have a class zzz in a package xxx.yyy

import xxx.yyy;
...
new zzz();

Java will look for a directory xxx in the classpath. In that dir it will look for a directory yyy, and in that directory it will look for zzz.class

You have to go thru all your classes and packages to ensure that this rule is always followed.

ps: a jar file is equivalent toa directory for this rule.

so if i rename the dir to:
C://myJava/Frame

and then import that entire dir it should work? sorry im tryin to keep up

To get you going, I would suggest you get rid of all the package stuff and simply put all your classes in the same directory. It's often a problem for learners to get the whole package/directory thing right first time. You can learn about packages later.
But if you're redy to tackle it now, remeber
Your directory structure must exactly match you package structure
The whole directory structure must be in the classpath
You class files must be in the directory corresponding to their package

To get you going, I would suggest you get rid of all the package stuff and simply put all your classes in the same directory. It's often a problem for learners to get the whole package/directory thing right first time. You can learn about packages later.
But if you're redy to tackle it now, remeber
Your directory structure must exactly match you package structure
The whole directory structure must be in the classpath
You class files must be in the directory corresponding to their package

Ok thanks anyway for taking time :)

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.