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.
Related
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.
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
My directory structure is as follows:
/WorkingDirectory
MyCollection.java
/au/edu/au
/UserInterface
UserInterface.java
/Collection
Album.java
CDAlbum.java
DVDAlbum.java
CollectionFactory.java
Where Album.java is an interface implemented by CDAlbum.java and DVDAlbum.java. Each .java file has the appropriate
package au.edu.uow.UserInterface;
or
package au.edu.uow.Collection;
line.
In UserInterface.java I declare an ArrayList of type Album, which gives me compile errors of
.\au\edu\uow\UserInterface\UserInterface.java:9: error: cannot find symbol
private ArrayList<Album> myCollection;
^
symbol: class Album
location: class UserInterface
I am compiling from MyCollection.java, which has import statements for both packages.
what is causing this error? I tried adding
import au.edu.uow.Collection
to UserInterface.java, but to no avail.
You need either:
import au.edu.uow.Collection.*;
or:
import au.edu.uow.Collection.Album;
Using wildcard imports is more convenient but some consider this a bad practice. This is a matter of style; decide for yourself.
It is also considered good style for package names to be in all-lower-case, although this is not enforced by the compiler.
So, you folder structure looks like /au/edu/au & your trying to import from au.edu.uow
Apart from what JimN has suggested, I think you'll find that Java will complain that te package doesn't exist or the files in /au/edu/au are in the wrong package
Either change the directory structure or package structure to match each other
company xyz created a package
com.xyz.utils.
There are two classes declared in two separate files. They have some variables as package private. so that a variable X in class A can be used in class B of the same package.
package com.xyz.utils;
public class A{
int a=10;
}
package com.xyz.utils;
public class B{
int b = (new A()).a;
}
Those two files are compiled into a jar and sent to customer.
The customer add the jar to the project he is building and he writes code like below
package com.xyz.utils;
public class customer_class
{
int Y = (new A()).a;
}
Is that above code is correct?
My quetsion is. how can we make variables which are declared as package private to be not visible to others when they use the package.
The answer is "no" - you can't stop them from doing that.
It can not be done in general. I think, you can seal the package 'com.xyz.utils' in in the jar manifest, to tell the user that: do not define their classes in the sealed package as a best practice. But you can not restrict the user of your library from doing it.
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.