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 8943669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:50:08+00:00 2026-06-15T11:50:08+00:00

I have many more classes in the project but for now please consider only

  • 0

I have many more classes in the project but for now please consider only A, B and C classes.

abstract public class A {...}

public class B extends A {...}

public class C extends A {...}

Then later I have a code, say in class D, like this

A a = new B();
//A a = new C();

//use a's methods

So my question now is how to easily configure in Eclipse building of two separate JARs. First one should have B.class included and C.class excluded and code as A a = new B(); The second one should have C.class included and B.class excluded and code as A a = new C();

I do not know many things about Ant and Maven. Do I need to use them in this case?

Maybe, something wrong with my design, if so, please let me know.

  • 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-06-15T11:50:10+00:00Added an answer on June 15, 2026 at 11:50 am

    What you want to is mainly a code loading problem, not an Ant problem.

    Just the two statements you presented for creating a new class instance:

    A a = new B(); 
    A a = new C();
    

    The constructors are called using static code. Ant can not change the code, therefore the only way with Ant I see is generating a Factory class with Ant as part of the build process, depending if A or C is included into the JAR

    But that would result in a project that can no longer be used directly in Eclipse as the original source code in the Eclipse project misses the factor class.

    A IMHO better approach is dynamic class loading (may be combined with reflection). You can automatically search for classes that extend A or you add a configuration file/info to the JAR specifying which class to create (e.g. properties file).
    Place the properties file in the src folder with the Java file and load it via this.getClass().getResource("myclass.config");

    The config file can contain the class name that should used for creating a new instance.
    The following code snipped assumes that B and C both have a public constructor that does not take any argument:

    String classNameToLoad = ... // loaded from config file example "mypackage.B"
    final Class<?> c = Class.forName(classNameToLoad);
    final Class<? extends A> ac = c.asSubclass(A.class);
    final Constructor<? extends A> a_ctor =  ac.getConstructor();
    final A a = a_ctor.newInstance();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this (I'm only showing three records, there are many, many more) Array
I have 3 classes (simplified for this question): public class Page { public Guid
I've got a PHP project with many classes, and a PDO Wrapper class that
I have a generic model: public abstract class MyModel<T> where T: MyBase, new() {
I have many MySQL statements that are running under MySQL 4, but not MySQL
In my project structure I have the usual class per file separation and auto-loading
I have a project where I have to scrape many URLs from many pages.
Suppose I have the classes/models Projects (has many lists) Lists I want to allow
I have a class in the main project I don't want to change. class
I have the following situation: In project A, an object (say Obj1 of class

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.