List of usage examples for java.lang Class newInstance
@CallerSensitive @Deprecated(since = "9") public T newInstance() throws InstantiationException, IllegalAccessException
From source file:ConstructorTroubleToo.java
public static void main(String... args) { try {//from ww w .j a va 2 s . com Class<?> c = Class.forName("ConstructorTroubleToo"); // Method propagetes any exception thrown by the constructor (including checked exceptions). if (args.length > 0 && args[0].equals("class")) { Object o = c.newInstance(); } else { Object o = c.getConstructor().newInstance(); } // production code should handle these exceptions more gracefully } catch (ClassNotFoundException x) { x.printStackTrace(); } catch (InstantiationException x) { x.printStackTrace(); } catch (IllegalAccessException x) { x.printStackTrace(); } catch (NoSuchMethodException x) { x.printStackTrace(); } catch (InvocationTargetException x) { x.printStackTrace(); err.format("%n%nCaught exception: %s%n", x.getCause()); } }
From source file:net.recommenders.plista.client.Client.java
/** * This method starts the server/*from ww w . j a va2s. c om*/ * * @param args * @throws Exception */ public static void main(String[] args) throws Exception { final Properties properties = new Properties(); String fileName = ""; String recommenderClass = null; String handlerClass = null; if (args.length < 3) { fileName = System.getProperty("propertyFile"); } else { fileName = args[0]; recommenderClass = args[1]; handlerClass = args[2]; } // load the team properties try { properties.load(new FileInputStream(fileName)); } catch (IOException e) { logger.error(e.getMessage()); } catch (Exception e) { logger.error(e.getMessage()); } Recommender recommender = null; recommenderClass = (recommenderClass != null ? recommenderClass : properties.getProperty("plista.recommender")); System.out.println(recommenderClass); lognum = Integer.parseInt(properties.getProperty("plista.lognum")); try { final Class<?> transformClass = Class.forName(recommenderClass); recommender = (Recommender) transformClass.newInstance(); } catch (Exception e) { logger.error(e.getMessage()); throw new IllegalArgumentException("No recommender specified or recommender not available."); } // configure log4j /*if (args.length >= 4 && args[3] != null) { PropertyConfigurator.configure(args[0]); } else { PropertyConfigurator.configure("log4j.properties"); }*/ // set up and start server AbstractHandler handler = null; handlerClass = (handlerClass != null ? handlerClass : properties.getProperty("plista.handler")); System.out.println(handlerClass); try { final Class<?> transformClass = Class.forName(handlerClass); handler = (AbstractHandler) transformClass.getConstructor(Properties.class, Recommender.class) .newInstance(properties, recommender); } catch (Exception e) { logger.error(e.getMessage()); e.printStackTrace(); throw new IllegalArgumentException("No handler specified or handler not available."); } final Server server = new Server(Integer.parseInt(properties.getProperty("plista.port", "8080"))); server.setHandler(handler); logger.debug("Serverport " + server.getConnectors()[0].getPort()); server.start(); server.join(); }
From source file:gis.proj.drivers.Snyder.java
public static void main(String... args) { Snyder snyder = new Snyder(); JCommander jc = new JCommander(snyder); try {//from w w w . j a v a2s .c o m jc.parse(args); } catch (Exception e) { jc.usage(); System.exit(-10); } String fFormat = "(forward) X: %18.9f, Y: %18.9f%n"; String iFormat = "(inverse) Lon: %18.9f, Lat: %18.9f%n%n"; double[][] xy, ll = null; java.util.regex.Pattern pq = java.util.regex.Pattern.compile("quit", java.util.regex.Pattern.CASE_INSENSITIVE); java.util.Scanner s = new java.util.Scanner(System.in); Projection proj = null; printLicenseInformation("Snyder"); try { System.out.println("Loading projection: " + snyder.projStr); Class<?> cls = Class.forName(snyder.projStr); proj = (Projection) cls.newInstance(); } catch (Exception ex) { ex.printStackTrace(); } double lon = 0.0, lat = 0.0, x = 0.0, y = 0.0; Datum d = new Datum(); Ellipsoid e = EllipsoidFactory.getInstance().getEllipsoid(snyder.ellpStr); System.out.println("\nEllipsoid: " + snyder.ellpStr + ", " + e.getName() + ", " + e.getId() + ", " + e.getDescription()); for (String prop : e.getPropertyNames()) { System.out.println("\t" + prop + "\t" + e.getProperty(prop)); } String cmdEntryLine = (snyder.inverse ? "\nx y " : "\nlon lat") + ": "; for (String dProp : proj.getDatumProperties()) { d.setUserOverrideProperty(dProp, 0.0); } System.out.print(cmdEntryLine); while (s.hasNext(pq) == false) { if (snyder.inverse == false) { lon = parseDatumVal(s.next()); lat = parseDatumVal(s.next()); } else { x = parseDatumVal(s.next()); y = parseDatumVal(s.next()); } for (String dp : d.getPropertyNames()) { System.out.print(dp + ": "); d.setUserOverrideProperty(dp, parseDatumVal(s.next())); } System.out.println(); if (snyder.inverse == false) { xy = proj.forward(new double[] { lon }, new double[] { lat }, e, d); System.out.printf(fFormat, xy[0][0], xy[1][0]); ll = proj.inverse(new double[] { xy[0][0] }, new double[] { xy[1][0] }, e, d); System.out.printf(iFormat, StrictMath.toDegrees(ll[0][0]), StrictMath.toDegrees(ll[1][0])); } else { ll = proj.inverse(new double[] { x }, new double[] { y }, e, d); System.out.printf(iFormat, StrictMath.toDegrees(ll[0][0]), StrictMath.toDegrees(ll[1][0])); xy = proj.forward(new double[] { ll[0][0] }, new double[] { ll[1][0] }, e, d); System.out.printf(fFormat, xy[0][0], xy[1][0]); } System.out.print(cmdEntryLine); } s.close(); }
From source file:ClassLoaderDemo0.java
public static void main(String[] argv) { System.out.println("ClassLoaderDemo starting"); ClassLoaderDemo0 loader = new ClassLoaderDemo0(); Class c = null; Object demo;//from w w w .j ava2 s . c o m try { /* Load the "Demo" class from memory */ System.out.println("About to load class Demo"); c = loader.loadClass("Demo", true); System.out.println("About to instantiate class Demo"); demo = c.newInstance(); System.out.println("Got Demo class loaded: " + demo); /* Now try to call a method */ Method mi = c.getMethod("test", null); mi.invoke(demo, null); } catch (InvocationTargetException e) { // The invoked method threw an exception. We get it // wrapped up inside another exception, hence the // extra call here: e.getTargetException().printStackTrace(); System.out.println("Could not run test method"); } catch (Exception e) { e.printStackTrace(); System.out.println("Could not run test method"); } }
From source file:com.fengduo.bee.commons.core.lang.ArrayUtils.java
public static void main(String[] args) { String[] s = { "zxc", null, "msunmss", "" }; s = ArrayUtils.replaceNullElement(s, new IHandle<String>() { @Override// w ww.j a v a 2 s. co m public boolean isNull(String obj) { return obj == null || StringUtils.isEmpty((String) obj); } @Override public String init(Class<String> clazz) { if (clazz.equals(String.class)) { return (String) new String("wu"); } try { return clazz.newInstance(); } catch (InstantiationException e) { System.out.println(e.getMessage()); } catch (IllegalAccessException e) { System.out.println(e.getMessage()); } System.out.println("replaceNullElement: init error"); return null; } }); System.out.println(StringUtils.join(s, ";")); String[] a = removeBlankElement(new String[] { "1", null, "2", "", "3" }); for (int i = 0, j = a.length; i < j; i++) { System.out.println(a[i]); } String[] t = null; System.out.println("Orignal:" + org.apache.commons.lang.ArrayUtils.toString(t, "NULL")); System.out.println("shift:" + org.apache.commons.lang.ArrayUtils.toString(shift(t, "a"), "NULL")); System.out.println("unshift:" + org.apache.commons.lang.ArrayUtils.toString(unshift(t, "a"), "NULL")); System.out.println("add(-1):" + org.apache.commons.lang.ArrayUtils.toString(add(t, -1, "a"), "NULL")); System.out.println("add(0):" + org.apache.commons.lang.ArrayUtils.toString(add(t, 0, "a"), "NULL")); System.out.println("add(length):" + org.apache.commons.lang.ArrayUtils.toString(add(t, 0, "a"), "NULL")); System.out.println("add(length+1):" + org.apache.commons.lang.ArrayUtils.toString(add(t, 1, "a"), "NULL")); t = new String[] { "1", null, "2", "", "3" }; System.out.println("Orignal:" + org.apache.commons.lang.ArrayUtils.toString(t, "NULL")); System.out.println("shift:" + org.apache.commons.lang.ArrayUtils.toString(shift(t, "a"), "NULL")); System.out.println("unshift:" + org.apache.commons.lang.ArrayUtils.toString(unshift(t, "a"), "NULL")); System.out.println("add(-1):" + org.apache.commons.lang.ArrayUtils.toString(add(t, -1, "a"), "NULL")); System.out.println("add(0):" + org.apache.commons.lang.ArrayUtils.toString(add(t, 0, "a"), "NULL")); System.out.println("add(length):" + org.apache.commons.lang.ArrayUtils.toString(add(t, 0, "a"), "NULL")); System.out.println("add(length+1):" + org.apache.commons.lang.ArrayUtils.toString(add(t, 1, "a"), "NULL")); }
From source file:com.mmj.app.common.core.lang.ArrayUtils.java
public static void main(String[] args) { String[] s = { "zxc", null, "", "" }; s = ArrayUtils.replaceNullElement(s, new IHandle<String>() { @Override/* www . jav a 2s . c o m*/ public boolean isNull(String obj) { return obj == null || StringUtils.isEmpty((String) obj); } @Override public String init(Class<String> clazz) { if (clazz.equals(String.class)) { return (String) new String("wu"); } try { return clazz.newInstance(); } catch (InstantiationException e) { System.out.println(e.getMessage()); } catch (IllegalAccessException e) { System.out.println(e.getMessage()); } System.out.println("replaceNullElement: init error"); return null; } }); System.out.println(StringUtils.join(s, ";")); String[] a = removeBlankElement(new String[] { "1", null, "2", "", "3" }); for (int i = 0, j = a.length; i < j; i++) { System.out.println(a[i]); } String[] t = null; System.out.println("Orignal:" + org.apache.commons.lang.ArrayUtils.toString(t, "NULL")); System.out.println("shift:" + org.apache.commons.lang.ArrayUtils.toString(shift(t, "a"), "NULL")); System.out.println("unshift:" + org.apache.commons.lang.ArrayUtils.toString(unshift(t, "a"), "NULL")); System.out.println("add(-1):" + org.apache.commons.lang.ArrayUtils.toString(add(t, -1, "a"), "NULL")); System.out.println("add(0):" + org.apache.commons.lang.ArrayUtils.toString(add(t, 0, "a"), "NULL")); System.out.println("add(length):" + org.apache.commons.lang.ArrayUtils.toString(add(t, 0, "a"), "NULL")); System.out.println("add(length+1):" + org.apache.commons.lang.ArrayUtils.toString(add(t, 1, "a"), "NULL")); t = new String[] { "1", null, "2", "", "3" }; System.out.println("Orignal:" + org.apache.commons.lang.ArrayUtils.toString(t, "NULL")); System.out.println("shift:" + org.apache.commons.lang.ArrayUtils.toString(shift(t, "a"), "NULL")); System.out.println("unshift:" + org.apache.commons.lang.ArrayUtils.toString(unshift(t, "a"), "NULL")); System.out.println("add(-1):" + org.apache.commons.lang.ArrayUtils.toString(add(t, -1, "a"), "NULL")); System.out.println("add(0):" + org.apache.commons.lang.ArrayUtils.toString(add(t, 0, "a"), "NULL")); System.out.println("add(length):" + org.apache.commons.lang.ArrayUtils.toString(add(t, 0, "a"), "NULL")); System.out.println("add(length+1):" + org.apache.commons.lang.ArrayUtils.toString(add(t, 1, "a"), "NULL")); }
From source file:com.zb.app.common.core.lang.ArrayUtils.java
public static void main(String[] args) { String[] s = { "zxc", null, "zuobian", "" }; s = ArrayUtils.replaceNullElement(s, new IHandle<String>() { @Override//from ww w .j a v a2 s . c om public boolean isNull(String obj) { return obj == null || StringUtils.isEmpty((String) obj); } @Override public String init(Class<String> clazz) { if (clazz.equals(String.class)) { return (String) new String("wu"); } try { return clazz.newInstance(); } catch (InstantiationException e) { System.out.println(e.getMessage()); } catch (IllegalAccessException e) { System.out.println(e.getMessage()); } System.out.println("replaceNullElement: init error"); return null; } }); System.out.println(StringUtils.join(s, ";")); String[] a = removeBlankElement(new String[] { "1", null, "2", "", "3" }); for (int i = 0, j = a.length; i < j; i++) { System.out.println(a[i]); } String[] t = null; System.out.println("Orignal:" + org.apache.commons.lang.ArrayUtils.toString(t, "NULL")); System.out.println("shift:" + org.apache.commons.lang.ArrayUtils.toString(shift(t, "a"), "NULL")); System.out.println("unshift:" + org.apache.commons.lang.ArrayUtils.toString(unshift(t, "a"), "NULL")); System.out.println("add(-1):" + org.apache.commons.lang.ArrayUtils.toString(add(t, -1, "a"), "NULL")); System.out.println("add(0):" + org.apache.commons.lang.ArrayUtils.toString(add(t, 0, "a"), "NULL")); System.out.println("add(length):" + org.apache.commons.lang.ArrayUtils.toString(add(t, 0, "a"), "NULL")); System.out.println("add(length+1):" + org.apache.commons.lang.ArrayUtils.toString(add(t, 1, "a"), "NULL")); t = new String[] { "1", null, "2", "", "3" }; System.out.println("Orignal:" + org.apache.commons.lang.ArrayUtils.toString(t, "NULL")); System.out.println("shift:" + org.apache.commons.lang.ArrayUtils.toString(shift(t, "a"), "NULL")); System.out.println("unshift:" + org.apache.commons.lang.ArrayUtils.toString(unshift(t, "a"), "NULL")); System.out.println("add(-1):" + org.apache.commons.lang.ArrayUtils.toString(add(t, -1, "a"), "NULL")); System.out.println("add(0):" + org.apache.commons.lang.ArrayUtils.toString(add(t, 0, "a"), "NULL")); System.out.println("add(length):" + org.apache.commons.lang.ArrayUtils.toString(add(t, 0, "a"), "NULL")); System.out.println("add(length+1):" + org.apache.commons.lang.ArrayUtils.toString(add(t, 1, "a"), "NULL")); }
From source file:com.mirth.connect.server.launcher.MirthLauncher.java
public static void main(String[] args) { try {// w w w . j a v a2 s. c o m try { uninstallPendingExtensions(); installPendingExtensions(); } catch (Exception e) { logger.error("Error uninstalling or installing pending extensions.", e); } Properties mirthProperties = new Properties(); String includeCustomLib = null; try { mirthProperties.load(new FileInputStream(new File(MIRTH_PROPERTIES_FILE))); includeCustomLib = mirthProperties.getProperty(PROPERTY_INCLUDE_CUSTOM_LIB); createAppdataDir(mirthProperties); } catch (Exception e) { logger.error("Error creating the appdata directory.", e); } ManifestFile mirthServerJar = new ManifestFile("server-lib/mirth-server.jar"); ManifestFile mirthClientCoreJar = new ManifestFile("server-lib/mirth-client-core.jar"); ManifestDirectory serverLibDir = new ManifestDirectory("server-lib"); serverLibDir.setExcludes(new String[] { "mirth-client-core.jar" }); List<ManifestEntry> manifestList = new ArrayList<ManifestEntry>(); manifestList.add(mirthServerJar); manifestList.add(mirthClientCoreJar); manifestList.add(serverLibDir); // We want to include custom-lib if the property isn't found, or if it equals "true" if (includeCustomLib == null || Boolean.valueOf(includeCustomLib)) { manifestList.add(new ManifestDirectory("custom-lib")); } ManifestEntry[] manifest = manifestList.toArray(new ManifestEntry[manifestList.size()]); // Get the current server version JarFile mirthClientCoreJarFile = new JarFile(mirthClientCoreJar.getName()); Properties versionProperties = new Properties(); versionProperties.load(mirthClientCoreJarFile .getInputStream(mirthClientCoreJarFile.getJarEntry("version.properties"))); String currentVersion = versionProperties.getProperty("mirth.version"); List<URL> classpathUrls = new ArrayList<URL>(); addManifestToClasspath(manifest, classpathUrls); addExtensionsToClasspath(classpathUrls, currentVersion); URLClassLoader classLoader = new URLClassLoader(classpathUrls.toArray(new URL[classpathUrls.size()])); Class<?> mirthClass = classLoader.loadClass("com.mirth.connect.server.Mirth"); Thread mirthThread = (Thread) mirthClass.newInstance(); mirthThread.setContextClassLoader(classLoader); mirthThread.start(); } catch (Exception e) { e.printStackTrace(); } }
From source file:WebLauncher.java
public static void main(String[] args) { DirectJNI.init();/*from w w w.j a v a2 s .c om*/ for (String className : DirectJNI._rPackageInterfacesHash.keySet()) { String shortClassName = className.substring(className.lastIndexOf('.') + 1); try { Class packageWebClass = DirectJNI._mappingClassLoader.loadClass(className + "Web"); if (packageWebClass.getDeclaredMethods().length > 0) { try { String url = "http://localhost:8080/" + "ws/" + shortClassName; Endpoint.publish(url, packageWebClass.newInstance()); System.out.println(shortClassName + "Web" + " has been successfully published to " + url); } catch (Exception ie) { ie.printStackTrace(); } } } catch (ClassNotFoundException e) { } } }
From source file:kellinwood.zipsigner.cmdline.Main.java
public static void main(String[] args) { try {// w w w .j a v a 2s .c o m Options options = new Options(); CommandLine cmdLine = null; Option helpOption = new Option("h", "help", false, "Display usage information"); Option modeOption = new Option("m", "keymode", false, "Keymode one of: auto, auto-testkey, auto-none, media, platform, shared, testkey, none"); modeOption.setArgs(1); Option keyOption = new Option("k", "key", false, "PCKS#8 encoded private key file"); keyOption.setArgs(1); Option pwOption = new Option("p", "keypass", false, "Private key password"); pwOption.setArgs(1); Option certOption = new Option("c", "cert", false, "X.509 public key certificate file"); certOption.setArgs(1); Option sbtOption = new Option("t", "template", false, "Signature block template file"); sbtOption.setArgs(1); Option keystoreOption = new Option("s", "keystore", false, "Keystore file"); keystoreOption.setArgs(1); Option aliasOption = new Option("a", "alias", false, "Alias for key/cert in the keystore"); aliasOption.setArgs(1); options.addOption(helpOption); options.addOption(modeOption); options.addOption(keyOption); options.addOption(certOption); options.addOption(sbtOption); options.addOption(pwOption); options.addOption(keystoreOption); options.addOption(aliasOption); Parser parser = new BasicParser(); try { cmdLine = parser.parse(options, args); } catch (MissingOptionException x) { System.out.println("One or more required options are missing: " + x.getMessage()); usage(options); } catch (ParseException x) { System.out.println(x.getClass().getName() + ": " + x.getMessage()); usage(options); } if (cmdLine.hasOption(helpOption.getOpt())) usage(options); Properties log4jProperties = new Properties(); log4jProperties.load(new FileReader("log4j.properties")); PropertyConfigurator.configure(log4jProperties); LoggerManager.setLoggerFactory(new Log4jLoggerFactory()); List<String> argList = cmdLine.getArgList(); if (argList.size() != 2) usage(options); ZipSigner signer = new ZipSigner(); signer.addAutoKeyObserver(new Observer() { @Override public void update(Observable observable, Object o) { System.out.println("Signing with key: " + o); } }); Class bcProviderClass = Class.forName("org.bouncycastle.jce.provider.BouncyCastleProvider"); Provider bcProvider = (Provider) bcProviderClass.newInstance(); KeyStoreFileManager.setProvider(bcProvider); signer.loadProvider("org.spongycastle.jce.provider.BouncyCastleProvider"); PrivateKey privateKey = null; if (cmdLine.hasOption(keyOption.getOpt())) { if (!cmdLine.hasOption(certOption.getOpt())) { System.out.println("Certificate file is required when specifying a private key"); usage(options); } String keypw = null; if (cmdLine.hasOption(pwOption.getOpt())) keypw = pwOption.getValue(); else { keypw = new String(readPassword("Key password")); if (keypw.equals("")) keypw = null; } URL privateKeyUrl = new File(keyOption.getValue()).toURI().toURL(); privateKey = signer.readPrivateKey(privateKeyUrl, keypw); } X509Certificate cert = null; if (cmdLine.hasOption(certOption.getOpt())) { if (!cmdLine.hasOption(keyOption.getOpt())) { System.out.println("Private key file is required when specifying a certificate"); usage(options); } URL certUrl = new File(certOption.getValue()).toURI().toURL(); cert = signer.readPublicKey(certUrl); } byte[] sigBlockTemplate = null; if (cmdLine.hasOption(sbtOption.getOpt())) { URL sbtUrl = new File(sbtOption.getValue()).toURI().toURL(); sigBlockTemplate = signer.readContentAsBytes(sbtUrl); } if (cmdLine.hasOption(keyOption.getOpt())) { signer.setKeys("custom", cert, privateKey, sigBlockTemplate); signer.signZip(argList.get(0), argList.get(1)); } else if (cmdLine.hasOption(modeOption.getOpt())) { signer.setKeymode(modeOption.getValue()); signer.signZip(argList.get(0), argList.get(1)); } else if (cmdLine.hasOption((keystoreOption.getOpt()))) { String alias = null; if (!cmdLine.hasOption(aliasOption.getOpt())) { KeyStore keyStore = KeyStoreFileManager.loadKeyStore(keystoreOption.getValue(), (char[]) null); for (Enumeration<String> e = keyStore.aliases(); e.hasMoreElements();) { alias = e.nextElement(); System.out.println("Signing with key: " + alias); break; } } else alias = aliasOption.getValue(); String keypw = null; if (cmdLine.hasOption(pwOption.getOpt())) keypw = pwOption.getValue(); else { keypw = new String(readPassword("Key password")); if (keypw.equals("")) keypw = null; } CustomKeySigner.signZip(signer, keystoreOption.getValue(), null, alias, keypw.toCharArray(), "SHA1withRSA", argList.get(0), argList.get(1)); } else { signer.setKeymode("auto-testkey"); signer.signZip(argList.get(0), argList.get(1)); } } catch (Throwable t) { t.printStackTrace(); } }