简体   繁体   中英

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 ?

You cannotreference Classes in a default package. Put every class into a package.

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`
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM