Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading