Example usage for java.lang Class forName

List of usage examples for java.lang Class forName

Introduction

In this page you can find the example usage for java.lang Class forName.

Prototype

@CallerSensitive
public static Class<?> forName(String className) throws ClassNotFoundException 

Source Link

Document

Returns the Class object associated with the class or interface with the given string name.

Usage

From source file:Main.java

public static void main(String args[]) throws Exception {
    Class c = Class.forName("java.awt.Dimension");

    Field fields[] = c.getFields();
    for (int i = 0; i < fields.length; i++) {
        System.out.println(fields[i].hashCode());
    }/*  w  ww . j  a va 2 s.c  o  m*/
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String driverName = "org.gjt.mm.mysql.Driver";
    Class.forName(driverName);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Class c = Class.forName("MyClass");
    Method m = c.getDeclaredMethod("say", new Class[] { String.class, String.class });
    Object i = c.newInstance();//from  ww  w . j  a  va 2  s . com
    Object r = m.invoke(i, new Object[] { "Hello", "World" });
}

From source file:Main.java

public static void main(String[] args) throws Exception {

    Class cls = Class.forName("java.awt.Label");
    System.out.println("Methods =");

    Method m[] = cls.getMethods();
    for (int i = 0; i < m.length; i++) {
        System.out.println(m[i]);
    }/*ww w . j  a va  2s  .  c om*/
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    String myDB = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=C:/data.MDB";
    Connection conn = DriverManager.getConnection(myDB, "", "");
}

From source file:Main.java

public static void main(String[] args) throws Exception {

    Class cls = Class.forName("java.lang.String");
    System.out.println("Fields =");

    // returns the array of Field objects representing the public fields
    Field f[] = cls.getFields();//from  w  w w . ja  v  a2s  . co  m
    for (int i = 0; i < f.length; i++) {
        System.out.println(f[i]);
    }

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    String myDB = "jdbc:odbc:Driver={Microsoft Excel Driver (*.xls)};DBQ=c:/data.xls;"
            + "DriverID=22;READONLY=false";
    Connection con = DriverManager.getConnection(myDB, "", "");
}

From source file:Main.java

public static void main(String[] args) throws Exception {

    Class cls = Class.forName("Main");

    // returns the name of the class
    System.out.println("Class = " + cls.getName());

    // returns the ProtectionDomain of this class.
    ProtectionDomain p = cls.getProtectionDomain();
    System.out.println(p);//from   ww w.  jav a 2 s  .  co  m

}

From source file:Main.java

public static void main(String[] unused) {
    try {/*from   w  ww  .ja  v  a  2  s  . c  o m*/
        String n = "java.lang.Deprecated";
        Class c = Class.forName(n);
        System.out.println(c.isAnnotation());

    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

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

    Constructor[] constructors = c.getDeclaredConstructors();
    for (int i = 0; i < constructors.length; i++) {
        System.out.println(constructors[i].getDeclaringClass());
    }/*from  ww w . ja  va2 s  .c o m*/

}