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 cls = Class.forName("java.lang.String");
    System.out.println("Panel Constructors =");

    /* returns the array of Constructor objects representing the public 
    constructors of this class */
    Constructor c[] = cls.getConstructors();
    for (int i = 0; i < c.length; i++) {
        System.out.println(c[i]);
    }//from w  ww. j a  v a  2s  .com

}

From source file:Main.java

public static void main(String[] unused) {
    try {/*  w w  w.  j  ava  2  s  . c om*/
        String n = "java.lang.Deprecated";
        Class c = Class.forName(n);
        Class d = Class.forName("java.util.Date");
        System.out.println(d.isAnnotationPresent(c));

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

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Class.forName("oracle.jdbc.driver.OracleDriver");
    Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:databaseName",
            "username", "password");
    if (connection != null) {
        System.out.println("Connected");
    } else {/*from   w w  w .j av  a  2  s .c om*/
        System.out.println("Failed to make connection!");
    }
}

From source file:Main.java

public static void main(String[] unused) {
    try {/* w ww . j a  v a2  s.  co  m*/
        String n = "java.lang.Deprecated";
        Class c = Class.forName(n);
        Class d = Class.forName("java.util.Date");
        System.out.println(d.getAnnotation(c));

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

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Class.forName("org.postgresql.Driver");
    Connection connection = DriverManager.getConnection("jdbc:postgresql://127.0.0.1:5432/testdb", "userName",
            "password");
    if (connection != null) {
        System.out.println("Connected");
    } else {//  ww w. j av  a  2  s. c  o  m
        System.out.println("Failed to make connection!");
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Object object = new String();

    try {// w w  w .j  a  v  a  2 s  .  co m
        Class cls = Class.forName("java.lang.String");
    } catch (ClassNotFoundException e) {
    }
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    Class cls = Class.forName("constructor2");
    Class partypes[] = new Class[2];
    partypes[0] = Integer.TYPE;//w ww. ja  va  2s .c om
    partypes[1] = Integer.TYPE;
    Constructor ct = cls.getConstructor(partypes);
    Object arglist[] = new Object[2];
    arglist[0] = new Integer(37);
    arglist[1] = new Integer(47);
    Object retobj = ct.newInstance(arglist);

}

From source file:Main.java

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

    Class.forName("com.mysql.jdbc.Driver");

    System.out.println("MySQL JDBC Driver Registered!");
    Connection connection = null;

    connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/dbName", "root", "password");

    if (connection != null) {
        System.out.println("database now!");
    } else {/*  ww w. j a va 2s  . c  om*/
        System.out.println("Failed to make connection!");
    }
}

From source file:Spy.java

public static void main(String... args) throws Exception {
    Class<?> c = Class.forName("Spy");
    Field[] flds = c.getDeclaredFields();
    for (Field f : flds) {
        System.out.println(f.isSynthetic());

    }/*from  ww  w.  j  a  v  a2 s  . c  o m*/
}

From source file:Spy.java

public static void main(String... args) throws Exception {
    Class<?> c = Class.forName("Spy");
    Field[] flds = c.getDeclaredFields();
    for (Field f : flds) {
        System.out.println(f.isEnumConstant());

    }/*from   www.j av a 2  s  . com*/
}