Interface Implementation error : cannot find symbol - java

I am implementing the following sample interface:
package test1;
public interface MotorVehicle {
void run();
int getFuel();
}
In the class
package test1;
import test1.MotorVehicle;
public class Car implements MotorVehicle
{
int fuel;
public void run(){
System.out.println("Running");
}
public int getFuel(){
return this.fuel;
}
}
When I try to compile the class file , I get the following error :
Car.java:4: error: cannot find symbol
public class Car implements MotorVehicle
^
symbol: class MotorVehicle
1 error
Compile Steps:
Step:1 javac MotorVehicle.java
Step:2 javac Car.java
Both my interface and the class are in the same directory , why does ut come up with cannot find symbol error?
Edit:
As suggested , have changed the package , and tried to run the same code again . Still getting an error.

The problem is that you're in the wrong folder when compiling.
From the console screenshot, it is clear that you are inside /test1. However, the package test1; statement expects a folder inside the current folder named test1. It can't find that folder/package, so you get an error.
The solution is to go up one folder, so you end up in /src, then compile using the path to the file, e.g. javac test1/Car.java. Explanation: You are in the folder /src, the package statement inside the classes says they are inside the folder test1 which is inside /src. Now every package/path can be resolved.
And you shouldn't import things that are in the same package.

First of all as your package name is test you must keep your class and the interface in a folder named test.
Second thing since they are in the same folder named test remove import test.MotorVehicle; from the class defination
Suppose if your folder test resides in g:/ such that g:/test/contains class and the interface.
Then try opening the command prompt in g:/
then type the following commands
for compiling
javac test/Car.java
and for executing
java test.Car
Though you may get Error: Main method not found in class test.Car
as your class does not contain main mathod

You are going in to exact path by the use of cd command.Because of that interface is not accessible as class will try to find out it from package from current/running location.
For make this compile you have to specify fully (again Fully) qualified name of package during compilation.
For Example
If you class is in a.b.test package compile it like this
javac a/b/test/Car.java

First compile MotorVehicle as it doesn't have any dependencies. Then set the classpath
Before issuing javac Car.java compile statements you need to set the Classpath
Windows
set CLASSPATH=%CLASSPATH%;<PATH_TO_COMPILED_BINARY>/
Unix
export CLASSPATH=$CLASSPATH:<PATH_TO_COMPILED_BINARY>/
<PATH_TO_COMPILED_BINARY> should not include the package test1
Example :
C:/sourcecode/test1
Then <PATH_TO_COMPILED_BINARY> should be C:/sourcecode
Update
Removing the import test1.MotorVehicle will also fix the issue.

After Compiling Motorvehicle.java. you have to create a folder test1 and transfer the MotorVehicle.class into the folder test1 then compile the next file Car.java. This will solve your error

Related

How to Properly Implement Packages in Java

I am learning how to use packages in Java, but I am running into trouble when trying to implement them. I have a simple class called Main which appears as follows:
public class Main
{
public static void main(String[]args)
{
System.out.println("Package Test...");
}
}
The directory of this class is: C:\Users\MyComputer\Desktop\Packages\Main.java
When I compile this class, I run into no trouble. However, when I add "package com.example.mypackage;" to the top of the .java file, compile the program, and try to run the program, I receive the following error: "Error: Could not find or load main class Main"
What can I do to solve this problem?
If the path of your class is C:\Users\MyComputer\Desktop\Packages\Main.java then your class is not in a package. In this instance "Packages" is your project folder, and it only contains the one java class.
If you want package com.example.mypackage; to work, then your path needs to be:
C:\Users\MyComputer\Desktop\Packages\com\example\mypackage\Main.java

Java - Use of nonpublic class from the current package makes compiling error

I've the following two source files
File World.java
package planets;
public class World {
public static void main(String[] args) {
Mars.land();
}
}
File Moon.java
package planets;
public class Moon {
public static void land() {
System.out.println("Hello Moon");
}
}
class Mars {
public static void land() {
System.out.println("Hello Mars");
}
}
As we can see, the Moon.java contains two classes: the public Moon class and the nonpublic Mars class.
The files are located inside planets directory, below is showed the directory tree
+current-dir:
+----+planets:
+----+World.java
+----+Moon.java
Now, if I try to compile from Windows command prompt (I'm inside current-dir folder) typing
javac planets\World.java
I receive this error message:
planets\World.java:5: error: cannot find symbol
Mars.land();
^
symbol: variable Mars
location: class World
1 error
It's very strange, because I know that the compiler searches for nonpublic classes inside all the source files of the current package.
Also Cay Horstmann's Core Java Vol 1, 10th ed. at pp. 192-193 says that:
[...]you can import nonpublic classes from the current package. These
classes may be defined in source files with different names. If you
import a class from the current package, the compiler searches all
source files of the current package to see which one defines the
class.
In addition I tried to write these files using Eclipse Oxygen and it compile without problems. But I know that Eclipse use a different compiler.
Why does javac compiler fail?
EDIT: I have not set CLASSPATH variable. So by default compiler looks inside current directory.
you need to type the following commands in order (inside your 'current-dir')
javac planets\Moon.java
javac -cp . planets\World.java
java -cp . planets.World

Must a class name be fully qualified when we already set -classpath to locate that file?

Example:
I have a class called ProgA
package test;
public class ProgA
{
public static void main(String[] args)
{
ProgB pb = new ProgB();
pb.callMe();
}
}
Now I have the ProgB like below:
package test2;
public class ProgB
{
public void callMe()
{
System.out.println("inside callme");
}
}
After compiling ProgB.java its class file is generated in the test2 package. Now when I try to compile ProgA.java using this command:
javac -cp C:\Users\MyName\Desktop\test2 ProgA.java
I get the error that it cannot find ProgB.
My question is why cant java look inside the class path to find ProgB.class file and compile my ProgA.java successfully? The code works fine when I specify the fully qualified class name of ProgB inside ProgA.java code and run with the classpath set to -classpath C:\Users\MyName\Desktop. Why to have the fully quilified name when I am already specifying the full class path to find ProgB. I am not clear with that concept of classpath and fully qualified class name. Please explain me. Thank you
First you would need to import the class. This is why it asks you to use a fully qualified class name. You cannot use a class that is not in the same package without importing it (or using the fully qualified class name).
import test2.ProgB;
Then while compiling, you should provide the class path till the root location, the compiler will look for the class using the package name as the path.
Your compile command should be.
javac -cp C:\Users\MyName\Desktop ProgA.java
In order to use a class from another package, you need to either use the fully qualified class name, or have an import statement. This is a .java source code requirement. It can't be fixed simply by fiddling with the compiler's classpath.
Without an import statement, unqualified names are assumed to belong to the current source file's package. If you're in a package test file, the identifier ProgB will match test.ProgB but not test2.ProgB. The compiler won't search other packages unless you tell it to.

Java & setting classpath & localization of packages

I've got structure of catalogs:
/home/etc./studies/JAVA/pack/Print.java
/home/etc./studies/JAVA/Lab2/zad1/pkg/A.java
/home/etc./studies/JAVA/Lab2/zad1/B.java
A is a class in package "pkg"
Print is a class in package "pack"
B imports the packages "pkg" and "pack".
When I tried to compile B.java, I get an error:
B.java:4: error: cannot access A
public class B extends A{
^
bad class file: /home/etc./studies/java/A.class
class file contains wrong class: pkg.A
Please remove or make sure it appears in the correct subdirectory of
the classpath.
Is it possible to include that packages without reorganization structure of files ?
It's clearly saying that:
class file contains wrong class name: pkg.A
that means probably you declared class name as pkg. An instead of A.
if you declare the package names as correct like this
home/etc/studies/JAVA/Lab2/zad1
home/etc/studies/JAVA/Lab2/zad1/pkg,
/home/etc/studies/JAVA/Lab2/zad1/B.java
You won't get the compilation error.
package home.etc.studies.JAVA.Lab2.zad1;
import home.etc.studies.JAVA.Lab2.zad1.pkg.A;
public class B extends A {
'enter code here`
}
You cannotreference Classes in a default package. Put every class into a package.

"class, interface, or enum expected" error when trying to create package

I'm trying to declare a package in a file as follows:
import java.util.*;
package rtg;
public class Generate
{
// ...
}
But I'm getting an error when I try to compile this:
Generate.java:3: class, interface, or enum expected package rtg;
Why am I getting this error?
it should be
package rtg;
import java.util.*;
public class Generate{
}
In java you first define package then imports and then class. See wiki here: Java_package and Oracle's tutorial here: Java Packages
Edit
Now to call Genereate class from a class in same folder that is rtg folder:
package rtg;
public class GUI{
Generate gen = new Generate();
}
Make sure all words are spelled correctly.
The pacakge declaration must be the first thing in a Java file (apart from comments). You can't put the imports above it.
All the examples are above is good but we have to compile this package making class by swich standard ... You have to give "-d" and destinations folder for making package in it. "c: \f1 >javac -d e: \f2 temp . Java" 'c,e'are drive, 'f1,f2' are folder, temp is class name.

Categories