0

The console shows a ArrayIndexOutOfBoundsException when I run this program, it could not find the argument in main(). I really don't know the reason, because I do have the argument String[] args in public static void main(String[] args):

package reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.time.chrono.JapaneseChronology;

public class ClassViewer {
    public static void view(String clazName) throws ClassNotFoundException{
        Class clz=Class.forName(clazName);
        Package p=clz.getPackage();
        System.out.printf("package %s;%n", clz.getName());

        int modifier=clz.getModifiers();//取得类型的修饰常数
        System.out.printf("%s %s %s {%n", 
                Modifier.toString(modifier),
                Modifier.isInterface(modifier) ? "interface" :"class",
                clz.getName()
                );

        //取得声明的数据成员代表对象
        Field[] fields =clz.getDeclaredFields();
        for(Field field:fields){
            //显示权限修饰
            System.out.printf("\t%s %s %s;%n", 
                    Modifier.toString(field.getModifiers()),
                    field.getType().getName(),
                    field.getName()
                    );
        }

        //取得声明的创建方法代表对象
        Constructor[] constructors=clz.getDeclaredConstructors();
        for(Constructor constructor:constructors){
            System.out.printf("\t%s %s();%n", 
                    Modifier.toString(constructor.getModifiers()),
                    constructor.getName()
                    );
        }

        //取得声明的方法成员代表对象
        Method[] methods=clz.getDeclaredMethods();
        for(Method method:methods){
            System.out.printf("\t%s %s %s;%n", 
                    Modifier.toString(method.getModifiers()),
                    method.getReturnType(),
                    method.getName()
                    );
        }
        System.out.println("}");

    }
public static void main(String[] args) {
        try {
            ClassViewer.view(args[0]);
        }catch(ArrayIndexOutOfBoundsException e){
            System.out.println("Array Index Out Of Bounds Exception ");
        }
        catch (ClassNotFoundException e) {
            System.out.println("can not find the class");
        }
    }
}
tkausl
  • 13,686
  • 2
  • 33
  • 50
qiaopan Ma
  • 35
  • 1
  • 1
  • 3
  • 2
    When you are running the code after compiling it, You have to pass argument which is called [command line argument](https://docs.oracle.com/javase/tutorial/essential/environment/cmdLineArgs.html). Suppose you want to pass a String ```Checking``` in your code then you have to run file as ```java ClassViewer Checking```. – Sanket Makani Aug 28 '16 at 04:25
  • Did you _pass_ a argument to the program? – tkausl Aug 28 '16 at 04:25
  • Make sure you pass arguments to `view`. For testing you could change ` ClassViewer.view(args[0]);` to pass an argument like ` ClassViewer.view(className);` where className is a name of a class. – c0der Aug 28 '16 at 06:24
  • Please translate the comments to English as well. As it is, they are useless for most readers who might want to help you. – Jongware Aug 28 '16 at 09:17
  • c0der is right , i was so careless to ignore that the arguement of the static method view(String className). so when i wanna run the method view . i should pass an argument of String and it means a name of a class. so if i pass a correct meaning name like"java.lang.reflect.Constructor" as the argument of the view , it would be ok .change"ClassViewer.view(args[0]);"like below "ClassViewer.view("java.lang.reflect.Constructor");" it would be ok – qiaopan Ma Aug 28 '16 at 12:57

1 Answers1

0

As has been noted in the comments appropriately, you must provide command-line arguments.

Here is example of that on Linux terminal (Mac also has terminal, and for Windows users there's powershell or CMD )

$ ls
ClassViewer.java
$ javac ClassViewer.java
$ java ClassViewer ClassViewer
package ClassViewer;
public class ClassViewer {
    public ClassViewer();
    public static void view;
    public static void main;
}

Note that apparently class has to be in the same directory

$ java ClassViewer /home/xieerqi/bin/java2/soundexClass                        
can not find the class
$ # This didn't work, so copy the soudnexClass.class into current directory
$ cp /home/xieerqi/bin/java2/soundexClass.class  .
$ java ClassViewer soundexClass                                                
package soundexClass;
public class soundexClass {
    public soundexClass();
    public static void getSoundex;
    public static void main;
}

This also can be done with IDEs (note , that each IDE has their own way):

Community
  • 1
  • 1
Sergiy Kolodyazhnyy
  • 938
  • 1
  • 13
  • 41