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

  • Home
  • SEARCH
  • 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 6791687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:52:35+00:00 2026-05-26T17:52:35+00:00

Consider this package structure java.assignments Main.java java.assignments.lab1 Exe1.java java.assignments.lab2 Exe1.java Exe2.java Exe3.java java.assignments.lab3 Exe1.java

  • 0

Consider this package structure

java.assignments
    Main.java
    java.assignments.lab1
        Exe1.java
    java.assignments.lab2
        Exe1.java
        Exe2.java
        Exe3.java
    java.assignments.lab3
        Exe1.java
        Exe2.java
        Exe3.java
        Exe4.java
and so on.
  1. I want to write the main class in such a way that if I give the
    command like
    $ java java.assignments.Main exe
    java.assignments.lab2.Exe2
    The program should execute the main
    method stored in lab2.Exe2.java

  2. If I give $ java java.assignments.Main src
    java.assignments.lab2.Exe2
    The program should print the source code
    of Exe2.java

    I would prefer if the source code displayed included comments that I
    write in the program.

  3. If I give $ java java.assignments.Main list It should list all the
    packages and the classes contained in java.assignments

    If I give $ java java.assignments.Main list java.assignements.lab3
    It should list all the classes contained in java.assignments.lab3

Are these things possible ? and if yes how do I achieve this .

The stuff I have found out till now ,

I found out that I can dynamically load and execute classes #1# from this tutorial,
but how do I achieve #2# displaying Source code and listing packages #3#

  • 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-26T17:52:36+00:00Added an answer on May 26, 2026 at 5:52 pm

    I do not recommend having you start learning reflection; I think it is too difficult for a beginner, and even for some intermediate programmers, and also too involved. I mean, even if you do understand the concepts and so forth, reflection is a big topic and takes you rather far away from doing concrete things in programming.

    So I recommend this instead:

    Have all your “assignment” classes implement an interface that dictates they define a method which you’re then going to execute. For instance, define

    public interface MainClass
    {
      public void main(String[] args);
    }
    

    in a class called MainClass.java, and then have each of your assignments implement that interface:

    public class Lab2 implements MainClass
    

    I.e., just put “implements MainClass” after the class declaration.

    If you do not already know, you are going to need to handle the “arguments” string array passed to a java main method from the command line. That is where the “exe” and the name of the class the user wants you to execute are going to show up.

    So your “main main” method is going to test the first argument to see if it is equal to “exe”. If it is, then 1. prepend the package name (java.assignments) to your class name, instantiate the class using Class.forName(), cast it to Main, and execute the main method. That code will look something like:

    if (args[1].equalsIgnoreCase("exe"))   // it might be args[0], I can't remember
    {
      String className = "java.assignments." + args[2];
      Object o = Class.forName(className);
      Main   mainMethodClass = (Main)o;
      o.main();
    }
    

    Class.forName() instantiates an object of the given class name. All you need is a string that is the fully-qualified name.

    The line after Class.forName() above does a “cast” of the newly instantiated object to the Main interface. If your object doesn’t implement (or extend) Main, your code will fail on the line that does the cast. It tells the compiler to allow you to do things with it that are allowed on a class that implements Main.

    After that, the object is just like one you did a “new ” on, and you can invoke any method defined on it.

    I would say get that part running first for some different classes. If you’re still interested in displaying source and want to know where to put it, etc., then we can get into that in another lesson. I really think if you get this running, that will be enough to accomplish for one effort.

    rc

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a module that has several methods. Let's consider this : package MyPackage;
Consider this code (Java, specifically): public int doSomething() { doA(); try { doB(); }
I have a problem with the functions in the string_algo package. Consider this piece
Let's consider the following code in Java. package obj; final class First { public
Consider this simple class: package Foo; use Moose; has foo => ( is =>
Consider this problem: I have a program which should fetch (let's say) 100 records
Consider this code... using System.Threading; //... Timer someWork = new Timer( delegate(object state) {
Consider this case: dll = LoadDLL() dll->do() ... void do() { char *a =
Consider this Python program which uses PyGtk and Hippo Canvas to display a clickable
Consider this: public class TestClass { private String a; private String b; public TestClass()

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.