Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 804367
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:54:27+00:00 2026-05-14T23:54:27+00:00

Is it possible to load a class in Java and ‘fake’ the package name/canonical

  • 0

Is it possible to load a class in Java and ‘fake’ the package name/canonical name of a class? I tried doing this, the obvious way, but I get a “class name doesn’t match” message in a ClassDefNotFoundException.

The reason I’m doing this is I’m trying to load an API that was written in the default package so that I can use it directly without using reflection. The code will compile against the class in a folder structure representing the package and a package name import. ie:

./com/DefaultPackageClass.class
// ...
import com.DefaultPackageClass;
import java.util.Vector;
// ...

My current code is as follows:

public Class loadClass(String name) throws ClassNotFoundException {
    if(!CLASS_NAME.equals(name))
            return super.loadClass(name);

    try {
        URL myUrl = new URL(fileUrl);
        URLConnection connection = myUrl.openConnection();
        InputStream input = connection.getInputStream();
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        int data = input.read();

        while(data != -1){
            buffer.write(data);
            data = input.read();
        }

        input.close();

        byte[] classData = buffer.toByteArray();

        return defineClass(CLASS_NAME,
                classData, 0, classData.length);

    } catch (MalformedURLException e) {
        throw new UndeclaredThrowableException(e);
    } catch (IOException e) {
        throw new UndeclaredThrowableException(e); 
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T23:54:27+00:00Added an answer on May 14, 2026 at 11:54 pm

    As Pete mentioned, this can be done using the ASM bytecode library. In fact, that library actually ships with a class specifically for handling these class name re-mappings (RemappingClassAdapter). Here is an example of a class loader using this class:

    public class MagicClassLoader extends ClassLoader {
    
        private final String defaultPackageName;
    
        public MagicClassLoader(String defaultPackageName) {
            super();
            this.defaultPackageName = defaultPackageName;
        }
    
        public MagicClassLoader(String defaultPackageName, ClassLoader parent) {
            super(parent);
            this.defaultPackageName = defaultPackageName;
        }
    
        @Override
        public Class<?> loadClass(String name) throws ClassNotFoundException {
            byte[] bytecode = ...; // I will leave this part up to you
            byte[] remappedBytecode;
    
            try {
                remappedBytecode = rewriteDefaultPackageClassNames(bytecode);
            } catch (IOException e) {
                throw new RuntimeException("Could not rewrite class " + name);
            }
    
            return defineClass(name, remappedBytecode, 0, remappedBytecode.length);
        }
    
        public byte[] rewriteDefaultPackageClassNames(byte[] bytecode) throws IOException {
            ClassReader classReader = new ClassReader(bytecode);
            ClassWriter classWriter = new ClassWriter(classReader, 0);
    
            Remapper remapper = new DefaultPackageClassNameRemapper();
            classReader.accept(
                    new RemappingClassAdapter(classWriter, remapper),
                    0
                );
    
            return classWriter.toByteArray();
        }
    
        class DefaultPackageClassNameRemapper extends Remapper {
    
            @Override
            public String map(String typeName) {
                boolean hasPackageName = typeName.indexOf('.') != -1;
                if (hasPackageName) {
                    return typeName;
                } else {
                    return defaultPackageName + "." + typeName;
                }
            }
    
        }
    
    }
    

    To illustrate, I created two classes, both of which belong to the default package:

    public class Customer {
    
    }
    

    and

    public class Order {
    
        private Customer customer;
    
        public Order(Customer customer) {
            this.customer = customer;
        }
    
        public Customer getCustomer() {
            return customer;
        }
    
        public void setCustomer(Customer customer) {
            this.customer = customer;
        }
    
    }
    

    This is the listing of Order before any re-mapping:

    > javap -private -c Order
    Compiled from "Order.java"
    public class Order extends java.lang.Object{
    private Customer customer;
    
    public Order(Customer);
      Code:
       0:   aload_0
       1:   invokespecial   #10; //Method java/lang/Object."":()V
       4:   aload_0
       5:   aload_1
       6:   putfield    #13; //Field customer:LCustomer;
       9:   return
    
    public Customer getCustomer();
      Code:
       0:   aload_0
       1:   getfield    #13; //Field customer:LCustomer;
       4:   areturn
    
    public void setCustomer(Customer);
      Code:
       0:   aload_0
       1:   aload_1
       2:   putfield    #13; //Field customer:LCustomer;
       5:   return
    
    }
    

    This is the listing of Order after remapping (using com.mycompany as the default package):

    > javap -private -c Order
    Compiled from "Order.java"
    public class com.mycompany.Order extends com.mycompany.java.lang.Object{
    private com.mycompany.Customer customer;
    
    public com.mycompany.Order(com.mycompany.Customer);
      Code:
       0:   aload_0
       1:   invokespecial   #30; //Method "com.mycompany.java/lang/Object"."":()V
       4:   aload_0
       5:   aload_1
       6:   putfield    #32; //Field customer:Lcom.mycompany.Customer;
       9:   return
    
    public com.mycompany.Customer getCustomer();
      Code:
       0:   aload_0
       1:   getfield    #32; //Field customer:Lcom.mycompany.Customer;
       4:   areturn
    
    public void setCustomer(com.mycompany.Customer);
      Code:
       0:   aload_0
       1:   aload_1
       2:   putfield    #32; //Field customer:Lcom.mycompany.Customer;
       5:   return
    
    }
    

    As you can see, the remapping has changed all Order references to com.mycompany.Order and all Customer references to com.mycompany.Customer.

    This class loader would have to load all classes that either:

    • belong to the default package, or
    • use other classes that belong to the default package.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.