精华内容
下载资源
问答
  • Java中的java.lang.Class API 详解

    千次阅读 2019-02-14 18:48:17
    且将新火试新茶,诗酒趁年华。 概述 Class是一个位于java.lang包下面的一个类,在Java中每个类实例都有对应的Class对象。...Class.forName(“className”):因为Class类没有公共的构造方法,所以存在一个静态的方法...

    且将新火试新茶,诗酒趁年华。

    https://raw.githubusercontent.com/longfeizheng/longfeizheng.github.io/master/images/java/java04.jpg

    概述

    Class是一个位于java.lang包下面的一个类,在Java中每个类实例都有对应的Class对象。类对象是由Java虚拟机(JVM)自动构造的。

    Class类的方法经常在反射时被调用。

    创建Class对象

    有三种方法可以创建Class对象

    1. Class.forName(“className”):因为Class类没有公共的构造方法,所以存在一个静态的方法返回Class对象,即Class.forName()用于创建Class对象。要创建的Class对象的类名在运行时确定,不存在则抛出ClassNotFoundException。(注意className为类的完整包名)
    Class.forName("java.lang.String")
    
    1. Myclass.class:当我们在类名后面跟上.class时,就会返回当前类的Class对象。它主要应用于原始数据类型,并且仅在我们知道类的名称时才使用。要创建的Class对象的类名在编译时确定
    Class c2 = String.class;
    

    请注意,此方法与类名一起使用,而不是与类实例一起使用

    A a = new A();   // Class A 的实例a
    Class c = A.class; // 正确用法
    Class c = a.class; //编译出错
    
    1. obj.getClass() : 此方法存在时Object类中,它返回此obj对象的运行时类。
    String str = new String("string");
    Class c3 = str.getClass();
    

    Class对象里面的方法

    String toString()

    此方法将Class对象转换为字符串。它返回字符串表示形式,即字符串“class”或“interface”,后跟空格,然后是类的含包名名称。如果Class对象表示基本类型,则此方法返回基本类型的名称,如果它表示void,则返回“void”

    描述:

    语法 :
    public String toString()
    参数 :
    NA
    返回 :
    a string representation of this class object.
    重写 :
    toString in class Object
    

    测试类:

    public class ToStringTest {
        public static void main(String[] args) throws ClassNotFoundException {
            Class c1 = Class.forName("java.lang.String");
            Class c2 = int.class;
            Class c3 = void.class;
            System.out.println("Class represented by c1: "+c1.toString());
            System.out.println("Class represented by c2: "+c2.toString());
            System.out.println("Class represented by c3: "+c3.toString());
        }
    }
    

    输出:

    Class represented by c1: class java.lang.String
    Class represented by c2: int
    Class represented by c3: void
    

    String toGenericString()

    返回描述此类的字符串,包括有关修饰符和类型参数的信息。

    描述:

    语法 :
    public String toGenericString()
    参数 :
    NA
    返回 :
    a string describe of this class object.
    

    测试类:

    public class ToGenericStringTest {
        public static void main(String[] args) throws ClassNotFoundException {
            Class c1 = Class.forName("java.lang.String");
            Class c2 = int.class;
            Class c3 = void.class;
            System.out.println("Class represented by c1: "+c1.toGenericString());
            System.out.println("Class represented by c2: "+c2.toGenericString());
            System.out.println("Class represented by c3: "+c3.toGenericString());
        }
    }
    

    输出:

    Class represented by c1: public final class java.lang.String
    Class represented by c2: int
    Class represented by c3: void
    

    Class<?> forName(String className)

    返回与给定字符串名称的类或接口相关联的 类对象。

    描述:

    语法 :
    public static Class<?> forName(String className) throws ClassNotFoundException
    参数 :
    className -含包名
    返回 :
    返回指定包名的class对象
    异常:
    ClassNotFoundException
    ......
    

    测试类:

    public class ToClassForNameTest {
        public static void main(String[] args) throws ClassNotFoundException {
            Class c1 = Class.forName("java.lang.String");
        }
    }
    

    输出:

    Class<?> forName(String className,boolean initialize, ClassLoader loader)

    使用给定的类加载器返回与给定字符串名称的类或接口相关联的类对象。如果参数loader为空,则通过引导类加载器加载该类。只有当initialize参数为true并且尚未被初始化时,该类才被初始化。

    描述:

    语法 :
    public static Class<?> forName(String className,boolean initialize, ClassLoader loader)
    throws ClassNotFoundException
    参数 :
    className -含包名
    initialize - 如果true该类将被初始化。
    loader  - 类加载器
    返回 :
    返回指定包名的class对象
    异常:
    ClassNotFoundException
    ......
    

    测试类:

    public class ToClassForNameTest {
        public static void main(String[] args) throws ClassNotFoundException {
            Class myClass = Class.forName("Test");
            ClassLoader loader = myClass.getClassLoader();
            Class c = Class.forName("java.lang.String",true,loader)
        }
    }
    

    输出:

    T newInstance()

    此方法创建此Class对象表示的类的新实例。通过具有空参数列表的新表达式创建类。如果尚未初始化,则初始化该类。

    描述:

    语法 :
    public T newInstance() throws InstantiationException,IllegalAccessException
    参数 :
    NA
    返回 :
    由此对象表示的类的新分配实例
    异常:
    IllegalAccessException
    ......
    

    测试类:

    public class NewInstanceTest {
        public static void main(String[] args) throws ClassNotFoundException {
          Class c1 = Class.forName("java.lang.String");
          Object instance = c1.newInstance();
          System.out.println("instance class : " + instance.getClass());
        }
    }
    

    输出:

    instance class : class java.lang.String
    

    boolean isInstance(Object obj)

    此方法确定指定的Object是否与此Class表示的对象分配兼容。它相当于java中的instanceof运算符

    描述:

    语法 :
    public boolean isInstance(Object obj)
    参数 :
    obj - the object to check
    返回 :
    true if obj is an instance of this class else return false
    异常:
    

    测试类:

    public class IsInstanceTest {
        public static void main(String[] args) throws ClassNotFoundException {
            Class c = Class.forName("java.lang.String");
            String url = "http://niocoder.com";
            int i = 10;
            boolean b1 = c.isInstance(url);
            boolean b2 = c.isInstance(i);
            System.out.println("is url instance of String : " + b1);
            System.out.println("is i instance of String : " + b2);
        }
    }
    

    输出:

    is url instance of String : true
    is i instance of String : false
    

    boolean isAssignableFrom(Class<?> cls)

    此方法确定此Class对象表示的类或接口是否与指定的Class参数表示的类或接口相同,或者是超类或超接口。

    描述:

    语法 :
    public boolean isAssignableFrom(Class<?> cls)
    参数 :
    cls - the Class object to be checked
    返回 :
    true if objects of the type cls can be assigned to objects of this class
    

    测试类:

    public class IsAssignableFrom extends Thread {
        public static void main(String[] args) throws Exception {
            Class myClass = Class.forName("com.niocoder.test.java.method.IsAssignableFrom");
            Class c1 = Class.forName("java.lang.Thread");
            Class c2 = Class.forName("java.lang.String");
    
            boolean b1 = c1.isAssignableFrom(myClass);
            boolean b2 = c2.isAssignableFrom(myClass);
    
            System.out.println("is Thread class Assignable from IsAssignableFrom : " + b1);
            System.out.println("is String class Assignable from Test : " + b2);
        }
    }
    
    

    输出:

    is Thread class Assignable from IsAssignableFrom : true
    is String class Assignable from Test : false
    

    boolean isInterface()

    此方法确定指定的Class对象是否表示接口类型

    描述:

    语法 :
    public boolean isInterface()
    参数 :
    NA
    返回 :
    return true if and only if this class represents an interface type else return false
    

    测试类:

    public class IsInterfaceTest {
        public static void main(String[] args) throws Exception {
            Class c1 = Class.forName("java.lang.String");
            Class c2 = Class.forName("java.lang.Runnable");
            boolean b1 = c1.isInterface();
            boolean b2 = c2.isInterface();
            System.out.println("is java.lang.String an interface : " + b1);
            System.out.println("is java.lang.Runnable an interface : " + b2);
        }
    }
    
    

    输出:

    is java.lang.String an interface : false
    is java.lang.Runnable an interface : true
    

    boolean isPrimitive()

    此方法确定指定的Class对象是否表示基本类型。即boolean , byte , char , short , int , long , float和double 。

    描述:

    语法 :
    public boolean isPrimitive()
    参数 :
    NA
    返回 :
    return true if and only if this class represents a primitive type else return false
    

    测试类:

    public class IsPrimitiveTest {
        public static void main(String[] args) {
            Class c1 = int.class;
            Class c2 = IsPrimitiveTest.class;
            boolean b1 = c1.isPrimitive();
            boolean b2 = c2.isPrimitive();
    
            System.out.println("is " + c1.toString() + " primitive : " + b1);
            System.out.println("is " + c2.toString() + " primitive : " + b2);
        }
    }
    
    

    输出:

    is int primitive : true
    is class com.niocoder.test.java.method.IsPrimitiveTest primitive : false
    

    boolean isArray()

    此方法确定指定的Class对象是否表示数组类。

    描述:

    语法 :
    public boolean isArray()
    参数 :
    NA
    返回 :
    return true if and only if this class represents an array type else return false
    

    测试类:

    public class IsArrayTest {
        public static void main(String[] args) {
            int a[] = new int[2];
            Class c1 = a.getClass();
            Class c2 = IsArrayTest.class;
    
            boolean b1 = c1.isArray();
            boolean b2 = c2.isArray();
    
            System.out.println("is "+c1.toString()+" an array : " + b1);
            System.out.println("is "+c2.toString()+" an array : " + b2);
        }
    }
    
    

    输出:

    is class [I an array : true
    is class com.niocoder.test.java.method.IsArrayTest an array : false
    

    boolean isAnonymousClass()

    当且仅当此类是匿名类时,此方法才返回true。匿名类与本地类类似,只是它们没有名称

    描述:

    语法 :
    public boolean isAnonymousClass()
    参数 :
    NA
    返回 :
    true if and only if this class is an anonymous class.false,otherwise.
    

    boolean isLocalClass()

    当且仅当此类是本地类时,此方法才返回true。本地类是在Java代码块中本地声明的类,而不是类的成员。

    描述:

    语法 :
    public boolean isLocalClass()
    参数 :
    NA
    返回 :
    true if and only if this class is a local class.false,otherwise.
    

    boolean isMemberClass()

    当且仅当此类是Member类时,此方法返回true。

    描述:

    语法 :
    public boolean isMemberClass()
    参数 :
    NA
    返回 :
    true if and only if this class is a Member class.false,otherwise.
    

    boolean isEnum()

    当且仅当此类在源代码中声明为枚举时,此方法才返回true

    描述:

    语法 :
    public boolean isEnum()
    参数 :
    NA
    返回 :
    true iff this class was declared as an enum in the source code.false,otherwise.
    

    boolean isAnnotation()

    此方法确定此Class对象是否表示注释类型。请注意,如果此方法返回true,则isInterface()方法也将返回true,因为所有注释类型也是接口

    描述:

    语法 :
    public boolean isAnnotation()
    参数 :
    NA
    返回 :
    return true if and only if this class represents an annotation type else return false
    

    测试类:

    public class IsTest {

    @interface B
    {
        // Annotation element definitions
    }
    
    //枚举
    enum Color {
        RED, GREEN, BLUE;
    }
    
    //  Member class
    class A {
    }
    
    public static void main(String[] args) {
        // 匿名类
        IsTest t1 = new IsTest() {
    
        };
    
        Class c1 = t1.getClass();
        Class c2 = IsTest.class;
        Class c3 = A.class;
        Class c4 = Color.class;
        Class c5 = B.class;
    
        boolean b1 = c1.isAnonymousClass();
        System.out.println("is " + c1.toString() + " an anonymous class : " + b1);
        boolean b2 = c2.isLocalClass();
        System.out.println("is " + c2.toString() + " a local class : " + b2);
        boolean b3 = c3.isMemberClass();
        System.out.println("is " + c3.toString() + " a member class : " + b3);
        boolean b4 = c4.isEnum();
        System.out.println("is " + c3.toString() + " a Enum class : " + b4);
        boolean b5 = c5.isAnnotation();
        System.out.println("is " + c3.toString() + " an annotation  : " + b5);
    }
    

    }

    输出:

    is class com.niocoder.test.java.method.IsTest$1 an anonymous class : true
    is class com.niocoder.test.java.method.IsTest a local class : false
    is class com.niocoder.test.java.method.IsTest$A a member class : true
    is class com.niocoder.test.java.method.IsTest$A a Enum class : true
    is class com.niocoder.test.java.method.IsTest$A an annotation  : true
    

    String getName()

    返回由 类对象表示的实体(类,接口,数组类,原始类型或空白)的名称(含包明),作为 String 。

    描述:

    语法 :
    public String getName()
    参数 :
    NA
    返回 :
    returns the name of the name of the entity represented by this object.
    

    String getSimpleName()

    此方法返回源代码中给出的基础类的名称(不含包名)。如果基础类是匿名类,则返回空字符串

    描述:

    语法 :
    public String getSimpleName()
    参数 :
    NA
    返回 :
    the simple name of the underlying class
    

    测试类:

    public class GetNameTest {
        public static void main(String[] args)
        {
            Class c = GetNameTest.class;
    
            System.out.print("Class Name associated with c : ");
            System.out.println(c.getName());
            System.out.println(c.getSimpleName());
        }
    }
    
    

    输出:

    Class Name associated with c : com.niocoder.test.java.method.GetNameTest
    GetNameTest
    

    ClassLoader getClassLoader()

    此方法返回此类的类加载器。如果类加载器是bootstrap类加载器,那么此方法返回null,因为引导类加载器是用C,C ++等本机语言实现的。如果此对象表示基本类型或void,则返回null

    描述:

    语法 :
    public ClassLoader getClassLoader()
    参数 :
    NA
    返回 :
    the class loader that loaded the class or interface represented by this object
    represented by this object.
    

    TypeVariable<Class>[ ] getTypeParameters()

    此方法返回一个TypeVariable对象数组,该对象表示由此GenericDeclaration对象表示的泛型声明声明的类型变量,按声明顺序

    描述:

    语法 :
    public TypeVariable<Class<T>>[] getTypeParameters()
    参数 :
    NA
    返回 :
    an array of TypeVariable objects that represent the type variables declared
    by this generic declaration represented by this object.
    

    测试类:

    public class GetClassLoaderTest {
        public static void main(String[] args) throws Exception {
            {
                Class myClass = Class.forName("com.niocoder.test.java.method.GetClassLoaderTest");
                Class c1 = Class.forName("java.lang.String");
                Class c2 = int.class;
                System.out.print("GetClassLoaderTest class loader : ");
                System.out.println(myClass.getClassLoader());
                System.out.print("String class loader : ");
                System.out.println(c1.getClassLoader());
    
                System.out.print("primitive int loader : ");
                System.out.println(c2.getClassLoader());
            }
    
            {
                Class c = Class.forName("java.util.Set");
                TypeVariable[] tv = c.getTypeParameters();
                System.out.println("TypeVariables in "+c.getName()+" class : ");
                for (TypeVariable typeVariable : tv)
                {
                    System.out.println(typeVariable);
                }
            }
    
        }
    }
    
    

    输出:

    GetClassLoaderTest class loader : sun.misc.Launcher$AppClassLoader@18b4aac2
    String class loader : null
    primitive int loader : null
    TypeVariables in java.util.Set class :
    E
    

    Class<? super T> getSuperclass()

    此方法返回表示此Class所表示的实体(类,接口,基本类型或void)的超类的Class。如果此Class表示Object类,接口,基本类型或void,则返回null。如果此对象表示数组类,则返回表示Object类的Class对象。

    描述:

    语法 :
    public Class<? super T> getSuperclass()
    参数 :
    NA
    返回 :
    the superclass of the class represented by this object
    

    Type getGenericSuperclass()

    此方法返回表示此Class所表示的实体(类,接口,基本类型或void)的直接超类的Type。如果此Class表示Object类,接口,基本类型或void,则返回null。如果此对象表示数组类,则返回表示Object类的Class对象。

    描述:

    语法 :
    public Type getGenericSuperclass()
    参数 :
    NA
    返回 :
    athe superclass of the class represented by this object
    

    Class<?>[] getInterfaces()

    此方法返回由此对象表示的类或接口实现的接口。如果此对象表示不实现接口的类或接口,则该方法返回长度为0的数组。如果此对象表示基本类型或void,则该方法返回长度为0的数组。

    描述:

    语法 :
    public Class<?>[] getInterfaces()
    参数 :
    NA
    返回 :
    an array of interfaces implemented by this class.
    

    Package getPackage()

    此方法返回此类的包。

    描述:

    语法 :
    public Package getPackage()
    参数 :
    NA
    返回 :
    the package of the class,
    or null if no package information is available
    from the archive or codebase.
    

    测试类:

    public class GetSuperClassTest {
        public static void main(String[] args) throws Exception {
            System.out.println("getSuperclass-----------------------------------------");
            {
                Class myClass = GetSuperClassTest.class;
                Class c1 = Class.forName("com.niocoder.test.java.method.A");
                Class c2 = Class.forName("com.niocoder.test.java.method.B");
                Class c3 = Class.forName("java.lang.Object");
                System.out.print("GetSuperClassTest superclass : ");
                System.out.println(myClass.getSuperclass());
                System.out.print("A superclass : ");
                System.out.println(c1.getSuperclass());
                System.out.print("B superclass : ");
                System.out.println(c2.getSuperclass());
                System.out.print("Object superclass : ");
                System.out.println(c3.getSuperclass());
            }
    
            System.out.println("getGenericSuperclass-----------------------------------------");
            {
                Class myClass = GetSuperClassTest.class;
                Class c1 = Class.forName("java.util.ArrayList");
                Class c3 = Class.forName("java.lang.Object");
                System.out.print("GetSuperClassTest superclass : ");
                System.out.println(myClass.getGenericSuperclass());
                System.out.print("ArrayList superclass : ");
                System.out.println(c1.getGenericSuperclass());
                System.out.print("Object superclass : ");
                System.out.println(c3.getGenericSuperclass());
            }
    
            System.out.println("getInterfaces-----------------------------------------");
            {
                Class c1 = Class.forName("com.niocoder.test.java.method.D");
                Class c2 = Class.forName("java.lang.String");
                Class c1Interfaces[] = c1.getInterfaces();
                Class c2Interfaces[] = c2.getInterfaces();
                System.out.println("interfaces implemented by D class : ");
                for (Class class1 : c1Interfaces) {
                    System.out.println(class1);
                }
    
                System.out.println("interfaces implemented by String class : ");
                for (Class class1 : c2Interfaces) {
                    System.out.println(class1);
                }
            }
    
            System.out.println("getPackage-----------------------------------------");
            {
                Class c1 = Class.forName("java.lang.String");
                Class c2 = Class.forName("java.util.ArrayList");
                System.out.println(c1.getPackage());
                System.out.println(c2.getPackage());
            }
        }
    }
    
    class A {
    
    }
    
    class B extends A {
    
    }
    
    interface C {
    
    }
    
    interface D extends C {
    
    }
    
    

    输出:

    getSuperclass-----------------------------------------
    GetSuperClassTest superclass : class java.lang.Object
    A superclass : class java.lang.Object
    B superclass : class com.niocoder.test.java.method.A
    Object superclass : null
    getGenericSuperclass-----------------------------------------
    GetSuperClassTest superclass : class java.lang.Object
    ArrayList superclass : java.util.AbstractList<E>
    Object superclass : null
    getInterfaces-----------------------------------------
    interfaces implemented by D class :
    interface com.niocoder.test.java.method.C
    interfaces implemented by String class :
    interface java.io.Serializable
    interface java.lang.Comparable
    interface java.lang.CharSequence
    getPackage-----------------------------------------
    Disconnected from the target VM, address: '127.0.0.1:59273', transport: 'socket'
    package java.lang, Java Platform API Specification, version 1.8
    package java.util, Java Platform API Specification, version 1.8
    

    Field[] getFields()

    此方法返回一个Field对象数组,该对象反映此Class对象表示的类(及其所有超类)或接口(及其所有超类)的所有可访问公共字段

    描述:

    语法 :
    public Field[] getFields()  throws SecurityException
    参数 :
    NA
    返回 :
    the array of Field objects representing the public fields
    and  array of length 0 if the class or interface has no accessible public fields
    or if this Class object represents a primitive type or void.
    

    测试类:

    public class GetFieldsTest {
        public static void main(String[] args) throws Exception {
            Class c1 = Class.forName("java.lang.Integer");
            Field F[] = c1.getFields();
    
            System.out.println("Below are the fields of Integer class : ");
            for (Field field : F) {
                System.out.println(field);
            }
        }
    }
    
    
    

    输出:

    Below are the fields of Integer class :
    public static final int java.lang.Integer.MIN_VALUE
    public static final int java.lang.Integer.MAX_VALUE
    public static final java.lang.Class java.lang.Integer.TYPE
    public static final int java.lang.Integer.SIZE
    public static final int java.lang.Integer.BYTES
    

    Method[] getMethods()

    此方法返回一个Method对象数组,这些对象反映了类或接口的所有可访问的公共方法,以及从此Class对象表示的超类和超级接口继承的方法。

    描述:

    语法 :
    public Method[] getMethods() throws SecurityException
    参数 :
    NA
    返回 :
    the array of Method objects representing the public methods
    and  array of length 0 if the class or interface has no accessible public method
    or if this Class object represents a primitive type or void.
    

    测试类:

    public class GetMethodsTest {
        public static void main(String[] args) throws Exception {
            Class c1 = Class.forName("java.lang.Object");
            Method M[] = c1.getMethods();
    
            System.out.println("Below are the methods of Object class : ");
            for (Method method : M) {
                System.out.println(method);
            }
        }
    }
    
    
    

    输出:

    Below are the methods of Object class :
    public final void java.lang.Object.wait() throws java.lang.InterruptedException
    public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
    public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
    public boolean java.lang.Object.equals(java.lang.Object)
    public java.lang.String java.lang.Object.toString()
    public native int java.lang.Object.hashCode()
    public final native java.lang.Class java.lang.Object.getClass()
    public final native void java.lang.Object.notify()
    public final native void java.lang.Object.notifyAll()
    

    Constructor<?>[] getConstructors()

    此方法返回一个Constructor对象数组,这些对象反映此Class对象所表示的类的所有公共构造函数

    描述:

    语法 :
    public Constructor<?>[] getConstructors() throws SecurityException
    参数 :
    NA
    返回 :
    the array of Constructor objects representing the public constructors of this class
    and  array of length 0 if the class or interface has no accessible public constructor
    or if this Class object represents a primitive type or void.
    

    测试类:

    public class GetConstructorsTest {
        public static void main(String[] args) throws Exception {
            Class c1 = Class.forName("java.lang.Boolean");
            Constructor C[] = c1.getConstructors();
    
            System.out.println("Below are the constructors of Boolean class :");
            for (Constructor constructor : C) {
                System.out.println(constructor);
            }
        }
    }
    

    输出:

    public java.lang.Boolean(boolean)
    public java.lang.Boolean(java.lang.String)
    

    Field getField(String fieldName)

    此方法返回一个Field对象,该对象反映此Class对象所表示的类或接口的指定公共成员字段

    描述:

    语法 :
    public Field getField(String fieldName) throws NoSuchFieldException,SecurityException
    参数 :
    fieldName -  the field name
    返回 :
    the Field object of this class specified by name
    

    Method getMethod(String methodName,Class… parameterTypes)

    此方法返回一个Method对象,该对象反映此Class对象所表示的类或接口的指定公共成员方法

    描述:

    语法 :
    public Method getMethod(String methodName,Class... parameterTypes) throws
    NoSuchFieldException,SecurityException
    参数 :
    methodName -  the method name
    parameterTypes - the list of parameters
    返回 :
    the method object of this class specified by name
    

    Constructor<?> getConstructor(Class<?>… parameterTypes)

    此方法返回一个Constructor对象,该对象反映此Class对象所表示的类的指定公共构造函数.parameterTypes参数是一个Class对象的数组,用于按声明的顺序标识构造函数的形式参数类型

    描述:

    语法 :
    public Constructor<?> getConstructor(Class<?>... parameterTypes)
    throws NoSuchMethodException,SecurityException
    参数 :
    parameterTypes - the list of parameters
    返回 :
    the Constructor object of the public constructor that matches the specified parameterTypes
    

    测试类:

    public class GetFieldsNameTest {
        public static void main(String[] args) throws Exception {
            System.out.println("getField--------------------------------------------");
            {
                Class c1 = Class.forName("java.lang.Integer");
    
                Field f = c1.getField("MIN_VALUE");
    
                System.out.println("public field in Integer class with MIN_VALUE name :");
                System.out.println(f);
            }
            System.out.println("getMethod--------------------------------------------");
            {
                Class c1 = Class.forName("java.lang.Integer");
                Class c2 = Class.forName("java.lang.String");
                Method m = c1.getMethod("parseInt", c2);
    
                System.out.println("method in Integer class specified by parseInt : ");
                System.out.println(m);
            }
            System.out.println("getConstructor--------------------------------------------");
            {
                Class c1 = Class.forName("java.lang.Integer");
                Class c2 = Class.forName("java.lang.String");
                Constructor c = c1.getConstructor(c2);
    
                System.out.println("Constructor in Integer class & String parameterType:");
                System.out.println(c);
            }
        }
    }
    

    输出:

    getField--------------------------------------------
    public field in Integer class with MIN_VALUE name :
    public static final int java.lang.Integer.MIN_VALUE
    getMethod--------------------------------------------
    method in Integer class specified by parseInt :
    public static int java.lang.Integer.parseInt(java.lang.String) throws java.lang.NumberFormatException
    getConstructor--------------------------------------------
    Constructor in Integer class & String parameterType:
    public java.lang.Integer(java.lang.String) throws java.lang.NumberFormatException
    

    T cast(Object obj)

    此方法用于将对象强制转换为此Class对象表示的类或接口。

    描述:

    语法 :
    public T cast(Object obj)
    参数 :
    obj - the object to be cast
    返回 :
    the object after casting, or null if obj is null
    

    Class<? extends U> asSubclass(Class clazz)

    此方法用于转换此Class对象以表示由指定的类对象表示的类的子类。它始终返回对此类对象的引用

    描述:

    语法 :
    public <U> Class<? extends U> asSubclass(Class<U> class)
    参数 :
    clazz - the superclass object to be cast
    返回 :
    this Class object, cast to represent a subclass of the specified class object.
    

    测试类:

    public class CastTest {
        public static void main(String[] args) {
            {
                A1 a = new A1();
                System.out.println(a.getClass());
                B1 b = new B1();
                a = A1.class.cast(b);
                System.out.println(a.getClass());
            }
    
            {
                A1 a = new A1();
                Class superClass = a.getClass();
                B1 b = new B1();
                Class subClass = b.getClass();
                Class cast = subClass.asSubclass(superClass);
                System.out.println(cast);
            }
    
        }
    }
    
    class A1 {
    
    }
    
    class B1 extends A1 {
    
    }
    
    

    输出:

    class com.niocoder.test.java.method.A1
    class com.niocoder.test.java.method.B1
    class com.niocoder.test.java.method.B1
    

    参考链接


    Java.lang.Class class in Java | Set 1

    展开全文
  • class jdk.internal.loader.ClassLoadersAppClassLoadercannotbecasttoclassjava.net.URLClassLoader(jdk.internal.loader.ClassLoadersAppClassLoader cannot be cast to class java.net.URLClassLoader (jdk....

    在码云上下载的另一个springboot项目,启动报错:class jdk.internal.loader.ClassLoaders$AppClassLoader cannot be cast to class java.net.URLClassLoader (jdk.internal.loader.ClassLoaders$AppClassLoader and java.net.URLClassLoader are in module java.base of loader 'bootstrap')

    解决方案:

    • 将pom.xml中的热部署依赖去掉(或注释掉)
    <!--热启动-->
    <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-devtools</artifactId>
       <optional>true</optional>
       <scope>true</scope>
    </dependency>
    
    • 将SpringBoot版本升到2.x版本:
      修改前:
    <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.9.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
    

    修改后:

     <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.3.RELEASE</version>
            <relativePath/>
        </parent>
    

    总结:将项目中的热部署依赖注释,并将springboot由原来的1.5.9升级到2.1.3后,该问题解决。

    原文链接:

    展开全文
  • Unable to process Jar entry [module-info.class] from Jar的问题解决背景解决办法 背景 在使用Eclipse进行Spring Boot应用开发时,如果项目启动是Run As->Run On Server方式运行就会报类似于Unable to process ...

    Unable to process Jar entry [module-info.class] from Jar的问题解决

    背景

    在使用Eclipse进行Spring Boot应用开发时,如果项目启动是Run As->Run On Server方式运行就会报类似于Unable to process Jar entry [module-info.class] from Jar的错误,详细错误类似如下:

    严重: Unable to process Jar entry [module-info.class] from Jar [file:/E:/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/chapter-03-2/WEB-INF/lib/jackson-core-2.10.1.jar] for annotations
    org.apache.tomcat.util.bcel.classfile.ClassFormatException: Invalid byte tag in constant pool: 19
    	at org.apache.tomcat.util.bcel.classfile.Constant.readConstant(Constant.java:97)
    	at org.apache.tomcat.util.bcel.classfile.ConstantPool.<init>(ConstantPool.java:54)
    	at org.apache.tomcat.util.bcel.classfile.ClassParser.readConstantPool(ClassParser.java:174)
    	at org.apache.tomcat.util.bcel.classfile.ClassParser.parse(ClassParser.java:83)
    	at org.apache.catalina.startup.ContextConfig.processAnnotationsStream(ContextConfig.java:2053)
    	at org.apache.catalina.startup.ContextConfig.processAnnotationsJar(ContextConfig.java:2000)
    	at org.apache.catalina.startup.ContextConfig.processAnnotationsUrl(ContextConfig.java:1970)
    	at org.apache.catalina.startup.ContextConfig.processAnnotations(ContextConfig.java:1923)
    	at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1163)
    	at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:775)
    	at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:299)
    	at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:94)
    	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5087)
    	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1419)
    	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1409)
    	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    	at java.lang.Thread.run(Thread.java:748)
    
    二月 27, 2020 9:34:31 上午 org.apache.catalina.startup.ContextConfig processAnnotationsJar
    严重: Unable to process Jar entry [module-info.class] from Jar [file:/E:/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/chapter-03-2/WEB-INF/lib/jackson-datatype-jsr310-2.10.1.jar] for annotations
    org.apache.tomcat.util.bcel.classfile.ClassFormatException: Invalid byte tag in constant pool: 19
    	at org.apache.tomcat.util.bcel.classfile.Constant.readConstant(Constant.java:97)
    	at org.apache.tomcat.util.bcel.classfile.ConstantPool.<init>(ConstantPool.java:54)
    	at org.apache.tomcat.util.bcel.classfile.ClassParser.readConstantPool(ClassParser.java:174)
    	at org.apache.tomcat.util.bcel.classfile.ClassParser.parse(ClassParser.java:83)
    	at org.apache.catalina.startup.ContextConfig.processAnnotationsStream(ContextConfig.java:2053)
    	at org.apache.catalina.startup.ContextConfig.processAnnotationsJar(ContextConfig.java:2000)
    	at org.apache.catalina.startup.ContextConfig.processAnnotationsUrl(ContextConfig.java:1970)
    	at org.apache.catalina.startup.ContextConfig.processAnnotations(ContextConfig.java:1923)
    	at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1163)
    	at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:775)
    	at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:299)
    	at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:94)
    	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5087)
    	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1419)
    	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1409)
    	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    	at java.lang.Thread.run(Thread.java:748)
    
    二月 27, 2020 9:34:32 上午 org.apache.catalina.startup.ContextConfig processAnnotationsJar
    严重: Unable to process Jar entry [module-info.class] from Jar [file:/E:/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/chapter-03-2/WEB-INF/lib/jackson-module-parameter-names-2.10.1.jar] for annotations
    org.apache.tomcat.util.bcel.classfile.ClassFormatException: Invalid byte tag in constant pool: 19
    	at org.apache.tomcat.util.bcel.classfile.Constant.readConstant(Constant.java:97)
    	at org.apache.tomcat.util.bcel.classfile.ConstantPool.<init>(ConstantPool.java:54)
    	at org.apache.tomcat.util.bcel.classfile.ClassParser.readConstantPool(ClassParser.java:174)
    	at org.apache.tomcat.util.bcel.classfile.ClassParser.parse(ClassParser.java:83)
    	at org.apache.catalina.startup.ContextConfig.processAnnotationsStream(ContextConfig.java:2053)
    	at org.apache.catalina.startup.ContextConfig.processAnnotationsJar(ContextConfig.java:2000)
    	at org.apache.catalina.startup.ContextConfig.processAnnotationsUrl(ContextConfig.java:1970)
    	at org.apache.catalina.startup.ContextConfig.processAnnotations(ContextConfig.java:1923)
    	at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1163)
    	at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:775)
    	at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:299)
    	at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:94)
    	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5087)
    	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1419)
    	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1409)
    	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    	at java.lang.Thread.run(Thread.java:748)
    
    二月 27, 2020 9:34:32 上午 org.apache.catalina.startup.ContextConfig processAnnotationsJar
    严重: Unable to process Jar entry [module-info.class] from Jar [file:/E:/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/chapter-03-2/WEB-INF/lib/jackson-annotations-2.10.1.jar] for annotations
    org.apache.tomcat.util.bcel.classfile.ClassFormatException: Invalid byte tag in constant pool: 19
    	at org.apache.tomcat.util.bcel.classfile.Constant.readConstant(Constant.java:97)
    	at org.apache.tomcat.util.bcel.classfile.ConstantPool.<init>(ConstantPool.java:54)
    	at org.apache.tomcat.util.bcel.classfile.ClassParser.readConstantPool(ClassParser.java:174)
    	at org.apache.tomcat.util.bcel.classfile.ClassParser.parse(ClassParser.java:83)
    	at org.apache.catalina.startup.ContextConfig.processAnnotationsStream(ContextConfig.java:2053)
    	at org.apache.catalina.startup.ContextConfig.processAnnotationsJar(ContextConfig.java:2000)
    	at org.apache.catalina.startup.ContextConfig.processAnnotationsUrl(ContextConfig.java:1970)
    	at org.apache.catalina.startup.ContextConfig.processAnnotations(ContextConfig.java:1923)
    	at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1163)
    	at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:775)
    	at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:299)
    	at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:94)
    	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5087)
    	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1419)
    	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1409)
    	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    	at java.lang.Thread.run(Thread.java:748)
    
    二月 27, 2020 9:34:32 上午 org.apache.catalina.startup.ContextConfig processAnnotationsJar
    严重: Unable to process Jar entry [module-info.class] from Jar [file:/E:/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/chapter-03-2/WEB-INF/lib/jackson-databind-2.10.1.jar] for annotations
    org.apache.tomcat.util.bcel.classfile.ClassFormatException: Invalid byte tag in constant pool: 19
    	at org.apache.tomcat.util.bcel.classfile.Constant.readConstant(Constant.java:97)
    	at org.apache.tomcat.util.bcel.classfile.ConstantPool.<init>(ConstantPool.java:54)
    	at org.apache.tomcat.util.bcel.classfile.ClassParser.readConstantPool(ClassParser.java:174)
    	at org.apache.tomcat.util.bcel.classfile.ClassParser.parse(ClassParser.java:83)
    	at org.apache.catalina.startup.ContextConfig.processAnnotationsStream(ContextConfig.java:2053)
    	at org.apache.catalina.startup.ContextConfig.processAnnotationsJar(ContextConfig.java:2000)
    	at org.apache.catalina.startup.ContextConfig.processAnnotationsUrl(ContextConfig.java:1970)
    	at org.apache.catalina.startup.ContextConfig.processAnnotations(ContextConfig.java:1923)
    	at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1163)
    	at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:775)
    	at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:299)
    	at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:94)
    	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5087)
    	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1419)
    	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1409)
    	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    	at java.lang.Thread.run(Thread.java:748)
    
    二月 27, 2020 9:34:33 上午 org.apache.catalina.startup.ContextConfig processAnnotationsJar
    严重: Unable to process Jar entry [module-info.class] from Jar [file:/E:/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/chapter-03-2/WEB-INF/lib/jackson-datatype-jdk8-2.10.1.jar] for annotations
    org.apache.tomcat.util.bcel.classfile.ClassFormatException: Invalid byte tag in constant pool: 19
    	at org.apache.tomcat.util.bcel.classfile.Constant.readConstant(Constant.java:97)
    	at org.apache.tomcat.util.bcel.classfile.ConstantPool.<init>(ConstantPool.java:54)
    	at org.apache.tomcat.util.bcel.classfile.ClassParser.readConstantPool(ClassParser.java:174)
    	at org.apache.tomcat.util.bcel.classfile.ClassParser.parse(ClassParser.java:83)
    	at org.apache.catalina.startup.ContextConfig.processAnnotationsStream(ContextConfig.java:2053)
    	at org.apache.catalina.startup.ContextConfig.processAnnotationsJar(ContextConfig.java:2000)
    	at org.apache.catalina.startup.ContextConfig.processAnnotationsUrl(ContextConfig.java:1970)
    	at org.apache.catalina.startup.ContextConfig.processAnnotations(ContextConfig.java:1923)
    	at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1163)
    	at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:775)
    	at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:299)
    	at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:94)
    	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5087)
    	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1419)
    	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1409)
    	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    	at java.lang.Thread.run(Thread.java:748)
    
    二月 27, 2020 9:34:37 上午 org.apache.jasper.servlet.TldScanner scanJars
    信息: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
    二月 27, 2020 9:34:37 上午 org.apache.catalina.core.ApplicationContext log
    信息: 2 Spring WebApplicationInitializers detected on classpath
    

    Unable to process Jar entry [module-info.class] from Jar[file:/E:/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/信息后面可能跟的jar文件不同。

    解决办法

    出现这种问题,是因为我们启动工程的方式不对造成的,Spring Boot应用要在工程右键菜单中选择Run As->Spring Boot App的方式启动,启动后的日志如下:

    
      .   ____          _            __ _ _
     /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
    ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
     \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
      '  |____| .__|_| |_|_| |_\__, | / / / /
     =========|_|==============|___/=/_/_/_/
     :: Spring Boot ::             (v2.3.0.M2)
    
    2020-02-27 09:43:51.984  INFO 17252 --- [           main] com.cloud.skyme.Chapter032Application    : Starting Chapter032Application on Sc-201506092204 with PID 17252 (E:\workspace\chapter-03-2\target\classes started by Administrator in E:\workspace\chapter-03-2)
    2020-02-27 09:43:51.987  INFO 17252 --- [           main] com.cloud.skyme.Chapter032Application    : No active profile set, falling back to default profiles: default
    2020-02-27 09:43:52.615  INFO 17252 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
    2020-02-27 09:43:52.622  INFO 17252 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
    2020-02-27 09:43:52.622  INFO 17252 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.31]
    2020-02-27 09:43:52.702  INFO 17252 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
    2020-02-27 09:43:52.702  INFO 17252 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 685 ms
    2020-02-27 09:43:52.808  INFO 17252 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
    2020-02-27 09:43:52.908  INFO 17252 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
    2020-02-27 09:43:52.912  INFO 17252 --- [           main] com.cloud.skyme.Chapter032Application    : Started Chapter032Application in 1.149 seconds (JVM running for 1.818)
    
    

    这样基本就正常了。

    展开全文
  • 准备上线备份文件,发现一个其中类多了$1.class和 $2.class,不要慌,一样备份下来,编译后也会有,一样的上传覆盖即可,至于为什么会有这样的现象,那是什么,怎么产生的 推荐网址:...

    准备上线备份文件,发现一个其中类多了$1.class和 $2.class,不要慌,一样备份下来,编译后也会有,一样的上传覆盖即可,至于为什么会有这样的现象,那是什么,怎么产生的
    推荐网址:
    https://blog.csdn.net/w_y_loong/article/details/53580101
    https://blog.csdn.net/lydong_/article/details/79812494

    讲述的非常清晰,感谢博主的分享

    展开全文
  • 将pom.xml文件中的相应以依赖删除, 将本地maven仓库中下载的依赖删除, 右键->Maven->Reimport,重新导入 光标在红色线标注的位置,alt+enter,重新导入依赖。 ...
  • 运行项目报错如下,提示找不到OnUnhandledKeyEventListener。... I/zygote64: Rejecting re-init on previously-failed class java.lang.Class<android.support.v4.view.ViewCompat$OnUnhandle...
  • .java编译成.class 与 .class反编译成.java

    万次阅读 多人点赞 2018-06-07 14:42:47
    了解下这两个文件类型:.java文件是给人看的,.class是给计算机看的。程序员编写的是.java文件,经编译后生成.class文件给计算执行。相信这两句话很形象的说明了这两个文件的关系了。1,首先反编译器就是jd-gui这个...
  • 更换 androidx 后报错 需要修改依赖中的版本 , 查看可以看到 implementation 'androidx.appcompat:appcompat:1.3.0-alpha02' 修改之后就没有这个错误了
  • 测试类自动注入失败:@RunWith(SpringRunner.class)详解

    万次阅读 多人点赞 2019-08-23 10:29:58
    @RunWith(SpringRunner.class) @SpringBootTest public class ProjectFontControllerTest { } 使用此注解需依赖: <dependency> <groupId>org.springframework.boot</groupId> ...
  • java.class.path详解

    千次阅读 2019-06-24 12:39:55
    java中System.properties的一个属性“java.class.path”(系统类加载器加载字节码class的路径)的获取及详解: 获取 package com.yoyo.yotils.yoyo.system; import org.apache.commons.lang3.StringUtils; ...
  • 关于@RunWith(SpringRunner.class)的作用

    千次阅读 2020-12-21 20:24:22
    查了好多文章说@RunWith(SpringRunner.class)注解是一个测试启动器,可以加载Springboot测试注解。本人好奇@RunWith(SpringRunner.class)的作用,于是在IDEA中把这个注解去掉后发现Bean也可以通过@Autowired注解进行...
  • 如何查看编译过的.Class文件

    万次阅读 2019-01-08 16:13:56
    Class文件是经过JDK中javac工具编译后的一组以8位字节为基础单位的二进制流,各个数据项目严格按照顺序紧凑地排列在Class文件之中,中间没有添加任何分隔符. 如何查看编译后的Class文件?推荐两种不需要安装任何额外...
  • 关于.getClass()和.class的区别

    千次阅读 多人点赞 2017-10-19 20:35:47
    一直在想.class和.getClass()的区别,思索良久,有点思绪,然后有网上搜了搜,找到了如下的一篇文章,与大家分享。 原来为就是涉及到Java的反射~~~~~ Java反射学习 所谓反射,可以理解为在运行时期获取...
  • Class.forName()与xxx.class的区别

    千次阅读 2018-05-30 15:54:19
    对于Class.forName(),这个方法是Class类的一个static成员。Class对象就和其他对象一样,我们可以获取并操作他的引用。forName()是取得Class对象引用的一种方法。他是用一个包含目标类的文本名的String作为输入参数...
  • 在项目中添加 .gitignore 文件 添加这些内容,基本上就够用了,如果有什么特殊的要求,可以再追加一些内容 ... 再次提交,成功, 之后文件进行修改,编译,生成新的 .class 文件,也不会提示将.class文件进行上传操作
  • @EntityListeners 源码 /** * Specifies the callback listener classes to be used for an * entity or mapped superclass. This annotation may be ... * to an entity class or mapped superclass. * * @s...
  • 在 CSS 中,类选择器以一个点号显示: ....classA.classB指的是一个元素,同时满足.classA和classB <div class="classA classB"></div> .classA .classB指的是classB内嵌于classA <div...
  • 在MyEclipse中运行一个小应用程序或者在编辑一些程序时,出现java.lang.UnsupportedClassVersionError Bad version number in .class file异常,的解决办法!
  • 项目之前启动都没这个问题,突然启动时就有这个错误了: 看报错信息有提示从jackson-annotations报出来的。 细想,自己最近也没有动这个包的依赖呀 然后在网上看了资料,原因时:依赖包依赖有jdk1.9的语法信息,...
  • StudentClazzDecompression.class); JSONArray stuClsArr = getJSONArrayByList(stuClzList); Map, Object> params = new HashMap, Object>(); params.put("schCode", request.getParameter("schCode")); ...
  • 对于以上三个语法结构的区分,需要先理解Class类 所有对象的类以及调用了静态方法的类都需要在对象创建之前在JVM虚拟机中加载,加载内容被称为“类对象”,每个类的类对象是唯一且是不可变的。而在对象创建的时候,...
  • 当一个类文件编译之后有很多类名字中有$符, 比如TestFrame.class, TestFrame$1.class, TestFrame$2.class, TestFrame$MyJob.class $后面跟数字的类就是匿名类编译出来的结果.TestFrame$MyJob.class则是内部类MyJob...
  • 16-Nov-2019 11:23:32.812 SEVERE [RMI TCP Connection(3)-127.0.0.1] org.apache.catalina.startup.ContextConfig.processAnnotationsJar Unable to process Jar entry [module-info.class] from Jar [jar:f...
  • 又是一上午就解决这个破问题,小弟我原本使用tomcat7版本在eclipse上,现在改成myEclipse了 配置我原本的tomcat后只要带上项目的调试就报找不到文件,就是db2jcc.jar或者db2jcc4.jar(具体看你报错的项目路径里写的...
  • Java--getClass()和.Class的区别

    千次阅读 多人点赞 2018-04-26 17:14:05
    传统编程:在编译阶段决定使用的类型反射:动态获得类型每个类都会产生一个对应的Class对象,也就是保存在.class文件。所有类都是在对其第一次使用时,动态加载到JVM的,当程序创建一个对类的静态成员的引用时,就会...
  • Tomcat启动时输出以下错误: 24 -May- 2015 ....class ....classfile ....class ....classfile .ClassParser ...(ClassParser ...导致该错误的原因是.class文件加密了,而解密的时候没有能够正确解密,导致了该问题。
  • https://www.cnblogs.com/lzmrex/articles/9236977.html
  • 启动tomcat,转到class.class的解决办法

    千次阅读 2017-07-11 14:53:51
    在debug模式窗口中,点run菜单栏,在下拉的选项卡中,在底部我们可以看见Remove all breakpoints的选项,只要我点击它,就会把我们当前调试的项目中的所有断点撤销。 ... all breakpoints就可以删除所有的断点了...
  • .class类文件的打开方法

    万次阅读 2018-04-09 14:26:24
    开源工具classpy的地址: ...gradle下载地址: ...注意:使用classpy前电脑必须已安装并配置好gradle环境变量 配置gradle环境变量: 1、官网上下载安装版本,gradle-x.x-bin.zip是...这样理解class类文件结构就非常方便啦~
  • 织梦dedesql.class.php文件漏洞解决方法

    千次阅读 2018-04-02 11:56:58
    阿里云后台提示织梦dedesql.class.php文件变量覆盖漏洞会导致SQL注入,可被攻击者构造恶意SQL语句,重置管理员密码,写入webshell等,进而获取服务器权限。下面告诉大家怎么修复这个漏洞:首先说一下dedecms不安全的...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 13,297,298
精华内容 5,318,919
关键字:

.class