Question

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#

Was it helpful?

Solution

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

OTHER TIPS

You cannot list all classes contained in a package according to Can you find all classes in a package using reflection?

If you want to list the source code of a class, I suggest including the source in your jar and displaying it somehow that way.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top