Home > Enterprise >  How to use a single .class file in a java package?
How to use a single .class file in a java package?

Time:12-04

I want to write a code like this

package mypackage;
public class A extends B {
}

But all that I have is the B.class file which is compiled from a single B.java file with no package specified.

Could anyone help me out? Thanks!

I've tried putting B.class in my ./src and A.java in ./src/mypackage and run javac -cp src ./src/package/A.java but it wouldn't compile. It wouldn't compile neither if I put B.class in the same folder as A.java

CodePudding user response:

You can't. By your description, class B is in the default (unnamed) package. The Java language rules do not allow classes outside the default package to refer to classes in the default package.

The only way you can use this class is to put A in the default package as well. That is, move it one directory up, and remove the package statement.

Alternatively, you need to have the sources of class B (or decompile it), move it into a package and add a package statement, and recompile.

  •  Tags:  
  • java
  • Related