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:movierecommend.MovieRecommend.java

public static void main(String[] args) throws ClassNotFoundException, SQLException {
    String url = "jdbc:sqlserver://localhost;databaseName=MovieDB;integratedSecurity=true";
    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
    Connection conn = DriverManager.getConnection(url);

    Statement stm = conn.createStatement();
    ResultSet rsRecnik = stm.executeQuery("SELECT Recnik FROM Recnik WHERE (ID_Zanra = 1)"); //citam recnik iz baze za odredjeni zanr
    String recnik[] = null;/*from  w  ww  .  j  a v a 2 s. c o  m*/

    while (rsRecnik.next()) {
        recnik = rsRecnik.getString("Recnik").split(","); //delim recnik na reci

    }

    ResultSet rsFilmovi = stm.executeQuery(
            "SELECT TOP (200) Naziv_Filma, LemmaPlots, " + "ID_Filma FROM Film WHERE (ID_Zanra = 1)");
    List<Film> listaFilmova = new ArrayList<>();
    Film f = null;
    int rb = 0;
    while (rsFilmovi.next()) {
        f = new Film(rb, Integer.parseInt(rsFilmovi.getString("ID_Filma")), rsFilmovi.getString("Naziv_Filma"),
                rsFilmovi.getString("LemmaPlots"));
        listaFilmova.add(f);
        rb++;

    }
    //kreiranje vektorskog modela
    M = MatrixUtils.createRealMatrix(recnik.length, listaFilmova.size());
    System.out.println("Prva tezinska matrica");

    for (int i = 0; i < recnik.length; i++) {
        String recBaza = recnik[i];
        for (Film film : listaFilmova) {
            for (String lemmaRec : film.getPlotLema()) {
                if (recBaza.equals(lemmaRec)) {
                    M.setEntry(i, film.getRb(), M.getEntry(i, film.getRb()) + 1);
                }
            }
        }
    }
    //racunanje tf-idf
    System.out.println("td-idf");
    M = LSA.calculateTfIdf(M);
    System.out.println("SVD");
    //SVD
    SingularValueDecomposition svd = new SingularValueDecomposition(M);
    RealMatrix V = svd.getV();
    RealMatrix Vk = V.getSubMatrix(0, V.getRowDimension() - 1, 0, brojDimenzija - 1); //dimenzija je poslednji argument
    //kosinusna slicnost
    System.out.println("Cosin simmilarity");
    CallableStatement stmTop = conn.prepareCall("{call Dodaj_TopList(?,?,?)}");

    for (int j = 0; j < listaFilmova.size(); j++) {
        Film fl = listaFilmova.get(j);
        List<Film> lFilmova1 = new ArrayList<>();
        lFilmova1.add(listaFilmova.get(j));
        double sim = 0.0;
        for (int k = 0; k < listaFilmova.size(); k++) {
            // System.out.println(listaFilmova.size());                
            sim = LSA.cosinSim(j, k, Vk.transpose());
            listaFilmova.get(k).setSimilarity(sim);
            lFilmova1.add(listaFilmova.get(k));
        }
        Collections.sort(lFilmova1);
        for (int k = 2; k < 13; k++) {
            stmTop.setString(1, fl.getID() + "");
            stmTop.setString(2, lFilmova1.get(k).getID() + "");
            stmTop.setString(3, lFilmova1.get(k).getSimilarity() + "");
            stmTop.execute();
        }

    }

    stm.close();
    rsRecnik.close();
    rsFilmovi.close();
    conn.close();

}

From source file:hr.fer.spocc.Main.java

public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException,
        InvocationTargetException, ClassNotFoundException {
    if (args.length < 1)
        args = new String[1];
    args[0] = "src/test/resources/minusLang-01.source";
    MethodUtils.invokeExactStaticMethod(Class.forName("hr.fer.spocc.lexer._GeneratedMain"), "main",
            new Object[] { args });
}

From source file:com.hangum.tadpole.engine.procedure.OracleProcedure.java

/**
 * @param args//w ww .j a  va  2 s. c om
 */
public static void main(String[] args) {

    String strQuery = "CREATE OR REPLACE PROCEDURE procOneINOUTParameter(genericParam IN OUT VARCHAR2) "
            + " IS " + "      BEGIN "
            + "        genericParam := 'Hello World INOUT parameter ' || genericParam; " + "      END;";

    Connection conn = null;
    Statement stmt = null;
    try {
        Class.forName("oracle.jdbc.driver.OracleDriver");
        conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.32.128:1521:XE", "HR", "tadpole");

        stmt = conn.createStatement();
        int code = stmt.executeUpdate(strQuery);
        System.out.println("[result]" + code);

    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        try {
            if (stmt != null)
                stmt.close();
            if (conn != null)
                conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

From source file:Employee.java

public static void main(String[] args) throws Exception {
    Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();

    Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@myserver:1521:ORCL", "yourName", "mypwd");

    Statement stmt = conn.createStatement();

    Map map = conn.getTypeMap();//  w ww .ja  va  2  s .  c  o  m
    map.put("EMP_DATA", Class.forName("Employee"));
    conn.setTypeMap(map);

    ResultSet rs = stmt.executeQuery("SELECT * from Emp");

    Employee employee;

    while (rs.next()) {
        int empId = rs.getInt("EmpId");
        employee = (Employee) rs.getObject("Emp_Info");

        System.out.print("Employee Id: " + empId + ", SSN: " + employee.SSN);
        System.out.print(", Name: " + employee.FirstName + " " + employee.LastName);
        System.out.println(
                ", Yearly Salary: $" + employee.Salary + " Monthly Salary: " + employee.calcMonthlySalary());
    }
    conn.close();
}

From source file:org.usergrid.benchmark.boostrap.Command.java

/**
 * @param args/*  ww  w  .ja v a  2s .c  o  m*/
 */
public static void main(String[] args) {

    if ((args == null) || (args.length < 1)) {
        System.out.println("No command specified");
        return;
    }

    String command = args[0];

    Class<?> clazz = null;

    try {
        clazz = Class.forName(command);
    } catch (ClassNotFoundException e) {
    }

    if (clazz == null) {
        try {
            clazz = Class.forName("org.usergrid.benchmark.commands." + command);
        } catch (ClassNotFoundException e) {
        }
    }

    if (clazz == null) {
        try {
            clazz = Class.forName("org.usergrid.benchmark.commands." + StringUtils.capitalize(command));
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    if (clazz == null) {
        System.out.println("Unable to find command");
        return;
    }

    args = Arrays.copyOfRange(args, 1, args.length);

    try {
        if (ToolBase.class.isAssignableFrom(clazz)) {
            ToolBase tool = (ToolBase) clazz.newInstance();
            tool.startTool(args);
        } else {
            MethodUtils.invokeStaticMethod(clazz, "main", (Object) args);
        }
    } catch (NoSuchMethodException e) {
        System.out.println("Unable to invoke command");
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        System.out.println("Unable to invoke command");
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        System.out.println("Error while invoking command");
        e.printStackTrace();
    } catch (InstantiationException e) {
        System.out.println("Error while instantiating tool object");
        e.printStackTrace();
    }
}

From source file:cn.lhfei.hadoop.ch04.PooledStreamCompressor.java

/**
 * use case: //from www.ja va 2 s  .com
 * 
 * @param args
 */
public static void main(String[] args) {
    String codecClassname = args[0];
    Class<?> codecClass = null;
    CompressionOutputStream out = null;
    Compressor compressor = null;
    try {
        codecClass = Class.forName(codecClassname);
        Configuration conf = new Configuration();
        CompressionCodec codec = (CompressionCodec) ReflectionUtils.newInstance(codecClass, conf);
        compressor = CodecPool.getCompressor(codec);

        out = codec.createOutputStream(System.out, compressor);

        IOUtils.copyBytes(System.in, out, 4096, false);

        out.finish();

    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        CodecPool.returnCompressor(compressor);
    }
}

From source file:sim.app.sugarscape.SugarscapeWithUIHigh.java

public static void main(String[] args) {
    ParameterDatabase parameters = null;
    for (int x = 0; x < args.length - 1; x++)
        if (args[x].equals("-file")) {
            try {
                InputStream is = Class.forName(sim.app.sugarscape.SugarscapeWithUIHigh.class.getCanonicalName())
                        .getClassLoader().getResourceAsStream(args[x + 1]);
                parameters = new ParameterDatabase(is);
            } catch (Exception ex) {
                ex.printStackTrace(System.err);
            }/*w ww  .jav a2s.  com*/
            break;
        }
    if (parameters == null) {
        throw new RuntimeException(
                "No parameter file was provided.  You need to include it with the arguments:\n\n\t-file myparameters.conf");
    }
    SugarscapeWithUIHigh t = new SugarscapeWithUIHigh(parameters);
    Console c = new Console(t);
    t.console = c;
    c.setVisible(true);
}

From source file:PropertyTable.java

/** This main method allows the class to be demonstrated standalone */
public static void main(String[] args) {
    // Specify the name of the class as a command-line argument
    Class beanClass = null;//  w w w .  j av a  2 s.  c o m
    try {
        // Use reflection to get the Class from the classname
        beanClass = Class.forName("javax.swing.JLabel");
    } catch (Exception e) { // Report errors
        System.out.println("Can't find specified class: " + e.getMessage());
        System.out.println("Usage: java TableDemo <JavaBean class name>");
        System.exit(0);
    }

    // Create a table to display the properties of the specified class
    JTable table = new PropertyTable(beanClass);

    // Then put the table in a scrolling window, put the scrolling
    // window into a frame, and pop it all up on to the screen
    JScrollPane scrollpane = new JScrollPane(table);
    JFrame frame = new JFrame("Properties of JavaBean: ");
    frame.getContentPane().add(scrollpane);
    frame.setSize(500, 400);
    frame.setVisible(true);
}

From source file:de.andrena.tools.macker.plugin.CommandLineFile.java

public static void main(String[] args) throws Exception {
    if (args.length == 0 || args.length > 2) {
        System.err.println("Usage: CommandLineFile <main class> [command line arguments file]");
        System.exit(1);/* w w w  .  j  a va2  s.  c  o m*/
    }

    String className = args[0];
    Class<?> clazz = Class.forName(className);
    Method main = clazz.getMethod("main", new Class[] { String[].class });

    List<String> lines = new ArrayList<String>();
    if (args.length == 2) {
        Reader in = new InputStreamReader(new FileInputStream(args[1]), "UTF-8");
        try {
            lines = IOUtils.readLines(in);
        } finally {
            in.close();
        }
    }

    try {
        main.invoke(null, new Object[] { lines.toArray(new String[lines.size()]) });
    } catch (InvocationTargetException ex) {
        Throwable cause = ex.getTargetException();
        if (cause instanceof Error) {
            throw (Error) cause;
        }
        throw (Exception) cause;
    }
}

From source file:ReflectionTest.java

public static void main(String[] args) {
    // read class name from command line args or user input
    String name;//w  w  w  . ja  v  a2 s. com
    if (args.length > 0)
        name = args[0];
    else {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter class name (e.g. java.util.Date): ");
        name = in.next();
    }

    try {
        // print class name and superclass name (if != Object)
        Class cl = Class.forName(name);
        Class supercl = cl.getSuperclass();
        String modifiers = Modifier.toString(cl.getModifiers());
        if (modifiers.length() > 0)
            System.out.print(modifiers + " ");
        System.out.print("class " + name);
        if (supercl != null && supercl != Object.class)
            System.out.print(" extends " + supercl.getName());

        System.out.print("\n{\n");
        printConstructors(cl);
        System.out.println();
        printMethods(cl);
        System.out.println();
        printFields(cl);
        System.out.println("}");
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
    System.exit(0);
}