Mehdi
Mehdi

Reputation: 107

Give an arguments to main

When i give a name of Class in args to main , an argument is : "myPackage.Algo.PersonneA". I have an exception and i don't know why ?

Exception in thread "main" java.lang.ClassNotFoundException: myPackage.Algo.PersonneA

This is my main :

 public static void main(String[] args) {
        Algo rf = (Algo) Class.forName(args[0]).newInstance();

    }

and this is a class what i want instanciate in main.

package myPackage;
public class Algo {
     public static class PersonneA extends ClassA {
        public Algorithm create(int r) {
            return new AlgorithmDist(new DistributedDocument(), r);
        }
    }

    public static class PersonneB extends ClassA {
        public Algorithm create(int r) {
            return new AlgorithmSimul(new SimulationDocument(), r);
        }
    }
}

Upvotes: 1

Views: 189

Answers (2)

amit
amit

Reputation: 178421

Two things:

(1) for inner class, you need to pass to forName() the string:

"myPackage.Algo$PersonneA"
               ^
           instead of .

The inner class is seperated from the enclosing class with a $ and not with a ..
So change the argument to main() to the above string instead the one you provided.

(2) PersonneA is subclass of ClassA and not of Algo - so after fixing issue 1 you will still get a ClassCastException, since an object of type PersonneA is not of type Algo

Upvotes: 5

John Snow
John Snow

Reputation: 5334

your main should look like this:

public static void main(String[] args) {
        Algo rf = (Algo) Class.forName(args[0]).newInstance();

    }

you wrote: static public void main(String[] args) which is wrong syntax :)

Upvotes: 1

Related Questions