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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:16:12+00:00 2026-05-25T01:16:12+00:00

I have a need for a personal command-line tool that should take the name

  • 0

I have a need for a personal command-line tool that should take the name of a concrete subclass as a CL argument and instantiate the correct Java class at runtime:

public class MyCommandLineTool
{
    public static void main(String[] args)
    {
        // Read the name of the desired class to load
        String strClassName = getClassNameFromArgs(args);

        // Now create an instance of that class (if it exists/is valid/etc.)
        Shape oShape = someMagicalClassLoaderCall(strClassName);

        // Now call the subclass's overridden draw() method
        oShape.draw();
    }
}

public abstract class Shape
{
    // Some stuff

    public abstract void draw();
}

public class Circle extends Shape
{
    @Override
    public void draw()
    {
        // etc...
    }
}

Therefore, from the command line, you might run the program as follows:

java MyCommandLineTool -shape Circle

Or something like that (not worried about the syntax of the command line call right now).

I’ve read several introductory tutorials on ClassLoaders, but am choking on understanding when one needs to write their own class loader. All the tutorials seem to focus on how to write your class loader, but without explaining when it is appropriate to do so. And, because it seems fairly complicated to do this, I would prefer to not have to write my own if I don’t have to.

It seems to me that if I have a compiled Circle.class file, any JVM should have no problem reading that class file and constructing an instance of a Circle at runtime based on a string I pass it containing that class’s name.

Can anyone clarify whether or not I need to write my own loader for this specific example, why, and if not, what I need to implement in lieu of someMagicalClassLoaderCall(String).

Thanks!

  • 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-25T01:16:13+00:00Added an answer on May 25, 2026 at 1:16 am

    You’ll have to make sure that the actual class is on the classpath when you run your program

    Class clazz = Class.forName("fully.qualyfied.ClassName");
    Shape instance = (Shape)clazz.newInstance();
    instance.draw()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've always developed application for personal use on command line. Now I have to
I have need to produce LINE, BAR, and PIE charts in Rails. I have
I have need for a function pointer that takes two arguments and returns a
I have a need to create a transactional process using an external API that
I am trying to create an interactive command line program and it seems that
I have a project that will need to be able to export data for
I have a database for a web application that is home to some personal
I have an applet that allows users to sign documents using their personal certificates.
What does Pro have that's not in Standard (or vice-versa)? I need Visual Studio
I have three tabs.. Personal info,profile info and FinalStep... First of all i need

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.