Java package , cannot find symbol - java

I am trying to follow this basic guide:
http://www.tutorialspoint.com/java/java_packages.htm
When I compile the file MammalInt.java I have an error:
MammalInt.java:4: error: cannot find symbol
public class MammalInt implements Animal{
^
symbol: class Animal
1 error
Both the files Animal.java and MammalInt.java are in the same directory. I have already compiled Animal.java.
Please help me!

You should:
Make sure the filenames are correct (Animal.java and MammalInt.java - the same as the class names with .java file extension)
Make sure they are both in a directory called animals (same as the package name)
Make sure they both have package animals; at the top of the files
Compile tham at the same time with the command javac Animal.java MammalInt.java
That works.

Classes have two access modifier: public and default.If you are making a class default that will be access-able from all other packages but if you are not specifying any access modifier i.e if you are giving default modifier that means that class will be access-able from the package where this class belong to.

Add on top of both files this:
package animals;
it should fix the problem.

Related

How to add directory to class path in ubuntu?

Suppose i have a directory "Animal" (without quotes) and i have java classes in it.How to add that directory to class path in Linux ? I mean suppose i want to use class files in "Animal" directory from some other folder , then i need to add "Animal" to class path .How to do it ?
Also suppose Animal directory has several sub directories and each of the sub directories also have directory having java class files .Then how to add each of them to java package ?
edit : Suppose i have two folders in Ubuntu say Downloads and Documents .Now in Downloads i created a folder called "animal" having a class named Dog.java . Now in Documents folder i created main method in which i try to make an instance of "Dog" class .I want to do it by importing the package "animal" in Download folder. How to do it ?We know that if we want to use some pre-built package in java then we import that package to use classes inside that package .Similarly suppose i want to import my own package , what i need to do ? When i write import and compile then it says package "animal" does not exist .So what i do so that it doesn't gives compilation error .Can some one elaborate by giving an answer .
I did lot of work before asking this question and when i was unsuccessful i finally asked this question .
To work this out in detail, with some code- the folder structure is:
~ (user folder)
~/Documents
~/Documents/useanimal
~/Documents/useanimal/UseDog.java
~/Downloads
~/Downloads/animal
~/Downloads/animal/Dog.java
The code for the two classes is below. To compile:
cd ~/Downloads
javac animal/Dog.java
cd ~/Documents
javac -cp ~/Downloads useanimal/UseDog.java
To run:
cd ~
java -cp Documents:Downloads useanimal.UseDog
and it will output
UseDog:main
Dog created
Code:
animal/Dog.java
package animal;
public class Dog {
public Dog() {
System.out.println("Dog created");
}
}
useanimal/UseDog.java
package useanimal;
import animal.Dog;
public class UseDog {
public static void main(String[] args) {
System.out.println("UseDog:main");
Dog dog = new Dog();
}
}

Why it complains when I import a self-defined package like below?

I have build two classes named PackageTest.java (in the desktop dir) and Employee.java (in the desktop/com/wenhu/corejava dir).
In the Employee.java file, I wrote in the first line:
package com.wenhu.corejava;
Then in the PackageTest.java file, I wrote in the first line:
import com.wenhu.corejava.*;
However, the compiler complains:
PackageTest.java:8: error: cannot access Employee
Employee harry = new Employee("Harry", 50000, 1989, 10, 1);
^
bad class file: .\Employee.class
class file contains wrong class: com.wenhu.corejava.Employee
Please remove or make sure it appears in the correct subdirectory of the classpath.
1 error
Interestingly, if I wrote:
import com.wenhu.corejava.Employee;
The compiler is OK!
Could anyone tell me why this is happened? I though the wildcard * could represent the Employee Class...
Thanks a lot!
Simple:
bad class file: .\Employee.class
class file contains wrong class: com.wenhu.corejava.Employee
Your setup is somehow messed up. It seems that you have a class file for Employee in your current directory.
The package name within the class file must match the file system location!
package com.wenhu.corejava;
This statement in the Employee class means that your class file Employee.java must be located in the directory com/wenhu/corejava/. But in your case it is in the directory which your java compiler as root of all sources understand, i.e. the default package.
To resolve your problem, either remove the package declaration mentioned above, which is not recommended, or create the corresponding directory and move the source file Employee.java to it.

compiling java package classes. Cannot access Class. class file contains wrong class while working with packages

I was trying to compile a few files in a package in java. The package name is library. Please have a look at the following details.
This is my Directory Structure:
javalearning
---library
------ParentClass.java
------ChildClass.java
I tried to compile in the following way:
current directory: javalearning
javac library/ParentClass.java //this compilation works fine
javac library/ChildClass.java //error over here
The following is the ParentClass.java:
package library;
class Parentclass{
...
}
The following is the ChildClass.java:
package library;
class ChildClass extends ParentClass{
...
}
The error is as follows:
cannot access ParentClass
bad class file: .\library\ParentClass.class
Please remove or make sure it appears in the correct sub directory of the classpath
You've got a casing issue:
class Parentclass
That's not the same as the filename ParentClass.class, nor is it the same as the class you're trying to use in ChildClass: class ChildClass extends ParentClass.
Java classnames are case-sensitive, but Windows filenames aren't. If the class had been public, the compiler would have validated that the names matched - but for non-public classes, there's no requirement for that.
The fact that you've ended up with ParentClass.class suggests that at some point it was declared as ParentClass, but then you changed the declared name and when recompiling, Windows just overwrote the content of the current file rather than effectively creating Parentclass.class.
Make sure your declared class name exactly matches the filename. You may well want to delete all your class files before recompiling, just to get out of a confusing state.

How to mention the class path

How can I mention the path of a class as in following code?
Class cl=Class.forName("SomeClass");
This works well if the "SomeClass" is located in the same directory of the calling class. But how to check for a class from a different directory, I saw the syntax for that is like xxxx.yyyy.class, but could not make out what those 'x's and'y's stand for. please help.
Thanks in advance.
Use the fully-qualified class name. For instance, to do this with the Java SE String class, which is in the java.lang package:
Class clazz = Class.forName("java.lang.String");
Those xxx and yyy stands for package names. Packages are normally represented by directories on disk with the same name as the package. When you create a class file you can specify which package the class goes by stating package xxx.yyy at the top of the file.
Referring to "SomeClass" without a package name will try to load a class named SomeClass in the default package.
Use Class.forName although make sure you deal with a possible ClassNotFoundException exception. This is a runtime exception so it does not mean you need to catch it. This is how you will know if you path to the class is correct. The problem is that if it cannot find the class funny things can happen with your code.
Class.forName(com.my.package.ClassName)

Why does my Java class fail to compile when compiled from inside the package directory?

I have made a directory called "middle" and inside it another directory called "tier" and inside the "tier" directory are OrderManager.java which is an interface and OrderManagerImpl.java having its implementation.
The problem is when I try to compile OrderManagerImpl.java from outside the package middle.tier it compiles but when I do the same inside the package it gives me the following error:
OrderManagerImpl.java:6: cannot find symbol
symbol: class OrderManager
public class OrderManagerImpl extends java.rmi.server.UnicastRemoteObject implements OrderManager{
Why is it so?
Because the compiler expects to find your class inside the proper folder: ./middle/tier . When you try to compile inside the package, the compiler search for your class in ./middle/tier/middle/tier

Categories

Resources