Hello everyone, first of all I would like to say that I'm new to java and I'm getting this error while trying to compile a program from a book that I am using to learn the language.

Ok so the program is very basic just two classes here is my code:

class Dog {
  int size;
  String breed;
  String name;
  void bark() {
    System.out.println("Ruff! Ruff");
  }
}

and...

class DogTestDrive {
  public static void main(String[] args) {
    Dog d = new Dog();
    d.size = 40;
    d.bark();
  }
}

Out of those two classes the Dog class compiles fine but when I try to compile the second class(DogTestDrive), I'm getting this error:


c:\users\owner\desktop\DogTestDrive.java:3: cannot find symbol
symbol : class Dog
location: class DogTestDrive
Dog d = new Dog();
^
2 errors

Can anyone tell me what I'm doing wrong? This is the second time I've gotten this type of error even though I copy the code word for word. Thanks in advance.

in DogTestDrive class check whether you import Dog class or not ..

you should add line as

import package.Dog;

package is Directory name which contains Dog class..

and also check DogTestDrive AND Dog class are in the same directory or not...

in DogTestDrive class check whether you import Dog class or not ..

Not at all necessary. If both the classes Dog and DogTestDrive are in the same directory then it isn't required that the referring class requires to add an import statement. This is true even if the Dog class is not mentioned as public. If you do not mention a class as public it's access specifier falls to 'default' i.e package access but since none of the classes are within a package and rest within the same directory the access is permitted. Both OP and you can try it for your self.

Now if your class Dog is in a package let's say "foo.bar" and your DogTestDrive class too is declared within the same package then the case goes back to the first one as explained above. Then effectively both these classes are in the same directory and nothing needs to imported and in fact, as mentioned above the Dog class doesn't even need to public in that case. But if your DogTestDrive is in a different package or more specifically not in the same package as that of Dog (since it can be in no package at all) then there are 2 things required for the DogTestDrive class to compile successfully. Firstly it needs the Dog class to be public, because if it isn't then it simply isn't accessible to outside packages so the import statement even if it exists isn't gonna do you any good. Secondly, if the Dog class is public then the DogTestDrive class would require an import statement for the Dog class' package.

Remember if you are declaring a class in a package and then compiling it from the command-line or a shell in linux you need to use the -d flag while compiling to ensure that the class falls in the right directory structure.

Ahhh.... You write a post about something that seems to you as though it would be helpful, but then someone down votes you. So much for the trouble. :)

commented: ridicule someone's reply... +0

Ok guys so I had both classes in the same directory(in my desktop) and I set the Dog class to public. However when I try to compile the DogTestDrive class now I get two errors instead of one here is what I got:

C:\Program Files\bin>javac c:\users\owner\desktop\DogTestDrive.java
c:\users\owner\desktop\DogTestDrive.java:3: cannot find symbol
symbol  : class Dog
location: class DogTestDrive
    Dog d = new Dog();
    ^
c:\users\owner\desktop\DogTestDrive.java:3: cannot find symbol
symbol  : class Dog
location: class DogTestDrive
    Dog d = new Dog();
                ^
2 errors

Also if you notice I am compiling the class from a different directory(\program files\bin\) and setting a full directory to the file on my desktop. Could that be the problem? On top of that excuse my ignorance but I don't understand the whole business with packages, could someone explain to me how that works please?

If you are compiling from a different location then you'll have to specify the path where the class Dog is supposed to be found by using the -cp (classpath) option. By default it would only search in the default directory and the paths specified by classpath environment variable.

Read more about classpaths and packages.

Alright thanks a ton after I messed around with the classpath it finally worked!

C:\Users\ervikar\Desktop\apache-tomcat-5.5.29\webapps\APP>javac Simple.java
Simple.java:10: cannot find symbol
symbol : variable lengh
location: class java.lang.String[]
for(int i=0;i<args.lengh;i++)
^
1 error

Is there anybody can sort me out of this problem.
Please reply with possible solutions

I think you should show the full script, I don't think anyone would know how to solve you problem with only one line of code.

The error is very clear:
symbol : variable lengh
from the errors that you get. Learn how to read them and give it a try, before posting here

It says it cannot find the symbol lengh. Check your book or the tutorial you took the code from. It should say length

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.