List of usage examples for java.net URLClassLoader URLClassLoader
public URLClassLoader(URL[] urls)
From source file:org.apache.axis2.jaxbri.CodeGenerationUtility.java
private static void scanEpisodeFile(File jar, SchemaCompiler sc) throws BadCommandLineException, IOException { URLClassLoader ucl = new URLClassLoader(new URL[] { jar.toURL() }); Enumeration<URL> resources = ucl.findResources("META-INF/sun-jaxb.episode"); while (resources.hasMoreElements()) { URL url = resources.nextElement(); sc.getOptions().addBindFile(new InputSource(url.toExternalForm())); }//from w ww .j a v a 2s . c o m }
From source file:org.kles.MainTEST.java
public void DBTest() throws MalformedURLException { ClassLoader loaderSpe = new URLClassLoader(createURLList(listSpe)); ClassLoader loaderStd = new URLClassLoader(createURLList(listStd)); MetaDataDef defSrc = getMetaDataDef(loaderSpe, "OCUSMA"); MetaDataDef defStd = getMetaDataDef(loaderStd, "OCUSMA"); // LinkedHashMap<String, LinkedHashMap<String, List<Diff>>> listDiff = DBComparator.compare(defSrc, defStd); }
From source file:com.spotify.docgenerator.DocgeneratorMojo.java
private URLClassLoader getPluginClassLoader() throws MalformedURLException { if (pluginClassLoader != null) { return pluginClassLoader; }/*from w w w. ja v a 2s . c om*/ final List<URL> jarUrls = Lists.newArrayList(); for (final String jarName : jarFiles) { jarUrls.add(new URL("file://" + jarName)); } final URLClassLoader loader = new URLClassLoader(jarUrls.toArray(new URL[jarUrls.size()])); pluginClassLoader = loader; return loader; }
From source file:com.legstar.protobuf.cobol.ProtoCobol.java
/** * From Google's org.waveprotocol.pst.PstFileDescriptor. * //from w w w. j a va 2 s . c o m * @param baseDir the base directory where compiled java binaries are * located * @param path the path to the java binary file relative to the base * directory * @return the java class loaded * @throws ProtoCobolException if class cannot be loaded */ protected Class<?> loadClass(File baseDir, String path) throws ProtoCobolException { try { ClassLoader classLoader = new URLClassLoader(new URL[] { baseDir.toURI().toURL() }); return classLoader.loadClass(getBinaryName(path)); } catch (Exception e) { throw new ProtoCobolException(e); } }
From source file:org.apache.hadoop.mapred.pipes.Submitter.java
@Override public int run(String[] args) throws Exception { CommandLineParser cli = new CommandLineParser(); if (args.length == 0) { cli.printUsage();/* w w w.ja v a2 s .c om*/ return 1; } cli.addOption("input", false, "input path to the maps", "path"); cli.addOption("output", false, "output path from the reduces", "path"); cli.addOption("jar", false, "job jar file", "path"); cli.addOption("inputformat", false, "java classname of InputFormat", "class"); //cli.addArgument("javareader", false, "is the RecordReader in Java"); cli.addOption("map", false, "java classname of Mapper", "class"); cli.addOption("partitioner", false, "java classname of Partitioner", "class"); cli.addOption("reduce", false, "java classname of Reducer", "class"); cli.addOption("writer", false, "java classname of OutputFormat", "class"); cli.addOption("program", false, "URI to application executable", "class"); cli.addOption("reduces", false, "number of reduces", "num"); cli.addOption("jobconf", false, "\"n1=v1,n2=v2,..\" (Deprecated) Optional. Add or override a JobConf property.", "key=val"); Parser parser = cli.createParser(); try { GenericOptionsParser genericParser = new GenericOptionsParser(getConf(), args); CommandLine results = parser.parse(cli.options, genericParser.getRemainingArgs()); JobConf job = new JobConf(getConf()); if (results.hasOption("input")) { FileInputFormat.setInputPaths(job, (String) results.getOptionValue("input")); } if (results.hasOption("output")) { FileOutputFormat.setOutputPath(job, new Path((String) results.getOptionValue("output"))); } if (results.hasOption("jar")) { job.setJar((String) results.getOptionValue("jar")); } if (results.hasOption("inputformat")) { setIsJavaRecordReader(job, true); job.setInputFormat(getClass(results, "inputformat", job, InputFormat.class)); } if (results.hasOption("javareader")) { setIsJavaRecordReader(job, true); } if (results.hasOption("map")) { setIsJavaMapper(job, true); job.setMapperClass(getClass(results, "map", job, Mapper.class)); } if (results.hasOption("partitioner")) { job.setPartitionerClass(getClass(results, "partitioner", job, Partitioner.class)); } if (results.hasOption("reduce")) { setIsJavaReducer(job, true); job.setReducerClass(getClass(results, "reduce", job, Reducer.class)); } if (results.hasOption("reduces")) { job.setNumReduceTasks(Integer.parseInt((String) results.getOptionValue("reduces"))); } if (results.hasOption("writer")) { setIsJavaRecordWriter(job, true); job.setOutputFormat(getClass(results, "writer", job, OutputFormat.class)); } if (results.hasOption("program")) { setExecutable(job, (String) results.getOptionValue("program")); } if (results.hasOption("jobconf")) { LOG.warn("-jobconf option is deprecated, please use -D instead."); String options = (String) results.getOptionValue("jobconf"); StringTokenizer tokenizer = new StringTokenizer(options, ","); while (tokenizer.hasMoreTokens()) { String keyVal = tokenizer.nextToken().trim(); String[] keyValSplit = keyVal.split("=", 2); job.set(keyValSplit[0], keyValSplit[1]); } } // if they gave us a jar file, include it into the class path String jarFile = job.getJar(); if (jarFile != null) { final URL[] urls = new URL[] { FileSystem.getLocal(job).pathToFile(new Path(jarFile)).toURL() }; //FindBugs complains that creating a URLClassLoader should be //in a doPrivileged() block. ClassLoader loader = AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() { public ClassLoader run() { return new URLClassLoader(urls); } }); job.setClassLoader(loader); } runJob(job); return 0; } catch (ParseException pe) { LOG.info("Error : " + pe); cli.printUsage(); return 1; } }
From source file:org.dkpro.tc.core.util.SaveModelUtils.java
public static List<ExternalResourceDescription> loadExternalResourceDescriptionOfFeatures(File tcModelLocation, UimaContext aContext) throws Exception { List<ExternalResourceDescription> erd = new ArrayList<>(); File classFile = new File(tcModelLocation + "/" + Constants.MODEL_FEATURE_CLASS_FOLDER); URLClassLoader urlClassLoader = new URLClassLoader(new URL[] { classFile.toURI().toURL() }); File file = new File(tcModelLocation, MODEL_FEATURE_EXTRACTOR_CONFIGURATION); for (String l : FileUtils.readLines(file)) { String[] split = l.split("\t"); String name = split[0];//w w w.j a v a2s. c om Object[] parameters = getParameters(split); Class<? extends Resource> feClass = urlClassLoader.loadClass(name).asSubclass(Resource.class); List<Object> idRemovedParameters = filterId(parameters); String id = getId(parameters); idRemovedParameters = addModelPathAsPrefixIfParameterIsExistingFile(idRemovedParameters, tcModelLocation.getAbsolutePath()); TcFeature feature = TcFeatureFactory.create(id, feClass, idRemovedParameters.toArray()); ExternalResourceDescription exRes = feature.getActualValue(); // Skip feature extractors that are not dependent on meta collectors if (!MetaDependent.class.isAssignableFrom(feClass)) { erd.add(exRes); continue; } Map<String, String> overrides = loadOverrides(tcModelLocation, META_COLLECTOR_OVERRIDE); configureOverrides(tcModelLocation, exRes, overrides); overrides = loadOverrides(tcModelLocation, META_EXTRACTOR_OVERRIDE); configureOverrides(tcModelLocation, exRes, overrides); erd.add(exRes); } urlClassLoader.close(); return erd; }
From source file:org.apache.hama.pipes.Submitter.java
@Override public int run(String[] args) throws Exception { CommandLineParser cli = new CommandLineParser(); if (args.length == 0) { cli.printUsage();/* w w w. jav a 2s.c o m*/ return 1; } LOG.debug("Hama pipes Submitter started!"); cli.addOption("input", false, "input path for bsp", "path"); cli.addOption("output", false, "output path from bsp", "path"); cli.addOption("jar", false, "job jar file", "path"); cli.addOption("inputformat", false, "java classname of InputFormat", "class"); // cli.addArgument("javareader", false, "is the RecordReader in Java"); cli.addOption("partitioner", false, "java classname of Partitioner", "class"); cli.addOption("outputformat", false, "java classname of OutputFormat", "class"); cli.addOption("cachefiles", false, "additional cache files to add", "space delimited paths"); cli.addOption("interpreter", false, "interpreter, like python or bash", "executable"); cli.addOption("jobname", false, "the jobname", "name"); cli.addOption("programArgs", false, "program arguments", "arguments"); cli.addOption("bspTasks", false, "how many bsp tasks to launch", "number"); cli.addOption("streaming", false, "if supplied, streaming is used instead of pipes", ""); cli.addOption("jobconf", false, "\"n1=v1,n2=v2,..\" (Deprecated) Optional. Add or override a JobConf property.", "key=val"); cli.addOption("program", false, "URI to application executable", "class"); Parser parser = cli.createParser(); try { // check generic arguments -conf GenericOptionsParser genericParser = new GenericOptionsParser(getConf(), args); // get other arguments CommandLine results = parser.parse(cli.options, genericParser.getRemainingArgs()); BSPJob job = new BSPJob(getConf()); if (results.hasOption("input")) { FileInputFormat.setInputPaths(job, results.getOptionValue("input")); } if (results.hasOption("output")) { FileOutputFormat.setOutputPath(job, new Path(results.getOptionValue("output"))); } if (results.hasOption("jar")) { job.setJar(results.getOptionValue("jar")); } if (results.hasOption("jobname")) { job.setJobName(results.getOptionValue("jobname")); } if (results.hasOption("inputformat")) { job.setInputFormat(getClass(results, "inputformat", conf, InputFormat.class)); } if (results.hasOption("partitioner")) { job.setPartitioner(getClass(results, "partitioner", conf, Partitioner.class)); } if (results.hasOption("outputformat")) { job.setOutputFormat(getClass(results, "outputformat", conf, OutputFormat.class)); } if (results.hasOption("streaming")) { LOG.info("Streaming enabled!"); job.set("hama.streaming.enabled", "true"); } if (results.hasOption("jobconf")) { LOG.warn("-jobconf option is deprecated, please use -D instead."); String options = results.getOptionValue("jobconf"); StringTokenizer tokenizer = new StringTokenizer(options, ","); while (tokenizer.hasMoreTokens()) { String keyVal = tokenizer.nextToken().trim(); String[] keyValSplit = keyVal.split("=", 2); job.set(keyValSplit[0], keyValSplit[1]); } } if (results.hasOption("bspTasks")) { int optionValue = Integer.parseInt(results.getOptionValue("bspTasks")); conf.setInt("bsp.local.tasks.maximum", optionValue); conf.setInt("bsp.peers.num", optionValue); } if (results.hasOption("program")) { String executablePath = results.getOptionValue("program"); setExecutable(job.getConfiguration(), executablePath); DistributedCache.addCacheFile(new Path(executablePath).toUri(), conf); } if (results.hasOption("interpreter")) { job.getConfiguration().set("hama.pipes.executable.interpretor", results.getOptionValue("interpreter")); } if (results.hasOption("programArgs")) { job.getConfiguration().set("hama.pipes.executable.args", Joiner.on(" ").join(results.getOptionValues("programArgs"))); // job.getConfiguration().set("hama.pipes.resolve.executable.args", // "true"); } if (results.hasOption("cachefiles")) { FileSystem fs = FileSystem.get(getConf()); String[] optionValues = results.getOptionValues("cachefiles"); for (String s : optionValues) { Path path = new Path(s); FileStatus[] globStatus = fs.globStatus(path); for (FileStatus f : globStatus) { if (!f.isDir()) { DistributedCache.addCacheFile(f.getPath().toUri(), job.getConfiguration()); } else { LOG.info("Ignoring directory " + f.getPath() + " while globbing."); } } } } // if they gave us a jar file, include it into the class path String jarFile = job.getJar(); if (jarFile != null) { @SuppressWarnings("deprecation") final URL[] urls = new URL[] { FileSystem.getLocal(conf).pathToFile(new Path(jarFile)).toURL() }; // FindBugs complains that creating a URLClassLoader should be // in a doPrivileged() block. ClassLoader loader = AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() { @Override public ClassLoader run() { return new URLClassLoader(urls); } }); conf.setClassLoader(loader); } runJob(job); return 0; } catch (ParseException pe) { LOG.info("Error : " + pe); cli.printUsage(); return 1; } }
From source file:io.fabric8.vertx.maven.plugin.mojos.AbstractRunMojo.java
/** * This will build the {@link URLClassLoader} object from the collection of classpath URLS * * @param classPathUrls - the classpath urls which will be used to build the {@link URLClassLoader} * @return an instance of {@link URLClassLoader} */// ww w. j a va 2 s. com protected ClassLoader buildClassLoader(Collection<URL> classPathUrls) { return new URLClassLoader(classPathUrls.toArray(new URL[classPathUrls.size()])); }
From source file:org.apache.sysml.utils.lite.BuildLiteExecution.java
@SuppressWarnings({ "rawtypes", "resource" }) public static void jmlcTests() { try {/*from ww w . ja v a2 s . c om*/ File jmlcTestDir = new File("target/test-classes"); if (!jmlcTestDir.exists()) { log.error("Test class directory could not be found"); return; } URL url = jmlcTestDir.toURI().toURL(); URL[] urls = new URL[] { url }; ClassLoader cl = new URLClassLoader(urls); Class clazz = cl.loadClass("org.apache.sysml.test.integration.functions.jmlc.FrameCastingTest"); Object obj = clazz.newInstance(); invokeMethod(clazz, obj, "setUpBase"); invokeMethod(clazz, obj, "setUp"); invokeMethod(clazz, obj, "testJMLCTransformDense"); invokeMethod(clazz, obj, "testJMLCTransformSparse"); invokeMethod(clazz, obj, "testJMLCTransformDenseReuse"); invokeMethod(clazz, obj, "testJMLCTransformSparseReuse"); invokeMethod(clazz, obj, "tearDown"); clazz = cl.loadClass("org.apache.sysml.test.integration.functions.jmlc.FrameDecodeTest"); obj = clazz.newInstance(); invokeMethod(clazz, obj, "setUpBase"); invokeMethod(clazz, obj, "setUp"); invokeMethod(clazz, obj, "testJMLCTransformDense"); invokeMethod(clazz, obj, "testJMLCTransformSparse"); invokeMethod(clazz, obj, "testJMLCTransformDenseReuse"); invokeMethod(clazz, obj, "testJMLCTransformSparseReuse"); invokeMethod(clazz, obj, "tearDown"); clazz = cl.loadClass("org.apache.sysml.test.integration.functions.jmlc.FrameEncodeTest"); obj = clazz.newInstance(); invokeMethod(clazz, obj, "setUpBase"); invokeMethod(clazz, obj, "setUp"); invokeMethod(clazz, obj, "testJMLCTransformDense"); invokeMethod(clazz, obj, "testJMLCTransformSparse"); invokeMethod(clazz, obj, "testJMLCTransformDenseReuse"); invokeMethod(clazz, obj, "testJMLCTransformSparseReuse"); invokeMethod(clazz, obj, "tearDown"); clazz = cl.loadClass("org.apache.sysml.test.integration.functions.jmlc.FrameIndexingAppendTest"); obj = clazz.newInstance(); invokeMethod(clazz, obj, "setUpBase"); invokeMethod(clazz, obj, "setUp"); invokeMethod(clazz, obj, "testJMLCTransformDense"); invokeMethod(clazz, obj, "testJMLCTransformSparse"); invokeMethod(clazz, obj, "testJMLCTransformDenseReuse"); invokeMethod(clazz, obj, "testJMLCTransformSparseReuse"); invokeMethod(clazz, obj, "tearDown"); clazz = cl.loadClass("org.apache.sysml.test.integration.functions.jmlc.FrameLeftIndexingTest"); obj = clazz.newInstance(); invokeMethod(clazz, obj, "setUpBase"); invokeMethod(clazz, obj, "setUp"); invokeMethod(clazz, obj, "testJMLCTransformDense"); invokeMethod(clazz, obj, "testJMLCTransformSparse"); invokeMethod(clazz, obj, "testJMLCTransformDenseReuse"); invokeMethod(clazz, obj, "testJMLCTransformSparseReuse"); invokeMethod(clazz, obj, "tearDown"); clazz = cl.loadClass("org.apache.sysml.test.integration.functions.jmlc.FrameReadMetaTest"); obj = clazz.newInstance(); invokeMethod(clazz, obj, "setUpBase"); invokeMethod(clazz, obj, "setUp"); invokeMethod(clazz, obj, "testJMLCTransformDenseSpec"); invokeMethod(clazz, obj, "testJMLCTransformDenseReuseSpec"); invokeMethod(clazz, obj, "testJMLCTransformDense"); invokeMethod(clazz, obj, "testJMLCTransformDenseReuse"); invokeMethod(clazz, obj, "testJMLCTransformDenseReadFrame"); invokeMethod(clazz, obj, "testJMLCTransformDenseReuseReadFrame"); invokeMethod(clazz, obj, "tearDown"); clazz = cl.loadClass("org.apache.sysml.test.integration.functions.jmlc.FrameTransformTest"); obj = clazz.newInstance(); invokeMethod(clazz, obj, "setUpBase"); invokeMethod(clazz, obj, "setUp"); invokeMethod(clazz, obj, "testJMLCTransformDense"); invokeMethod(clazz, obj, "testJMLCTransformSparse"); invokeMethod(clazz, obj, "testJMLCTransformDenseReuse"); invokeMethod(clazz, obj, "testJMLCTransformSparseReuse"); invokeMethod(clazz, obj, "tearDown"); clazz = cl.loadClass("org.apache.sysml.test.integration.functions.jmlc.JMLCInputOutputTest"); obj = clazz.newInstance(); invokeMethod(clazz, obj, "setUpBase"); invokeMethod(clazz, obj, "setUp"); invokeMethod(clazz, obj, "testScalarInputInt"); invokeMethod(clazz, obj, "testScalarInputDouble"); invokeMethod(clazz, obj, "testScalarInputBoolean"); invokeMethod(clazz, obj, "testScalarInputLong"); invokeMethod(clazz, obj, "testScalarInputStringExplicitValueType"); invokeMethod(clazz, obj, "testScalarOutputLong"); invokeMethod(clazz, obj, "testScalarOutputDouble"); invokeMethod(clazz, obj, "testScalarOutputString"); invokeMethod(clazz, obj, "testScalarOutputBoolean"); invokeMethod(clazz, obj, "testScalarOutputScalarObject"); invokeMethod(clazz, obj, "tearDown"); clazz = cl.loadClass("org.apache.sysml.test.integration.functions.jmlc.JMLCInputStreamReadTest"); obj = clazz.newInstance(); invokeMethod(clazz, obj, "setUpBase"); invokeMethod(clazz, obj, "setUp"); invokeMethod(clazz, obj, "testInputStreamReadMatrixDenseCSV"); invokeMethod(clazz, obj, "testInputStreamReadMatrixDenseText"); invokeMethod(clazz, obj, "testInputStreamReadMatrixSparseCSV"); invokeMethod(clazz, obj, "testInputStreamReadMatrixSparseText"); invokeMethod(clazz, obj, "testInputStreamReadFrameDenseCSV"); invokeMethod(clazz, obj, "testInputStreamReadFrameDenseText"); invokeMethod(clazz, obj, "testInputStreamReadFrameSparseCSV"); invokeMethod(clazz, obj, "testInputStreamReadFrameSparseText"); invokeMethod(clazz, obj, "testInputStreamReadFrameDenseCSVMeta"); invokeMethod(clazz, obj, "testInputStreamReadFrameDenseTextMeta"); invokeMethod(clazz, obj, "testInputStreamReadFrameSparseCSVMeta"); invokeMethod(clazz, obj, "testInputStreamReadFrameSparseTextMeta"); invokeMethod(clazz, obj, "tearDown"); clazz = cl.loadClass("org.apache.sysml.test.integration.functions.jmlc.MulticlassSVMScoreTest"); obj = clazz.newInstance(); invokeMethod(clazz, obj, "setUpBase"); invokeMethod(clazz, obj, "setUp"); invokeMethod(clazz, obj, "testJMLCMulticlassScoreDense"); invokeMethod(clazz, obj, "testJMLCMulticlassScoreSparse"); invokeMethod(clazz, obj, "tearDown"); clazz = cl.loadClass("org.apache.sysml.test.integration.functions.jmlc.ReuseModelVariablesTest"); obj = clazz.newInstance(); invokeMethod(clazz, obj, "setUpBase"); invokeMethod(clazz, obj, "setUp"); invokeMethod(clazz, obj, "testJMLCScoreGLMDense"); invokeMethod(clazz, obj, "testJMLCScoreGLMSparse"); invokeMethod(clazz, obj, "testJMLCScoreGLMDenseReuse"); invokeMethod(clazz, obj, "testJMLCScoreGLMSparseReuse"); invokeMethod(clazz, obj, "testJMLCScoreMSVMDense"); invokeMethod(clazz, obj, "testJMLCScoreMSVMSparse"); invokeMethod(clazz, obj, "testJMLCScoreMSVMDenseReuse"); invokeMethod(clazz, obj, "testJMLCScoreMSVMSparseReuse"); invokeMethod(clazz, obj, "tearDown"); } catch (Exception e) { e.printStackTrace(); } }
From source file:org.bigtester.ate.experimentals.DynamicClassLoader.java
/** * F2 test./*from w w w .j av a2 s .c o m*/ * * @throws IllegalAccessException * @throws InstantiationException * @throws ClassNotFoundException * @throws MalformedURLException */ @Test public void f2Test() throws InstantiationException, IllegalAccessException, ClassNotFoundException, MalformedURLException { /** Compilation Requirements *********************************************************************************************/ DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>(); JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null); // This sets up the class path that the compiler will use. // I've added the .jar file that contains the DoStuff interface within // in it... List<String> optionList = new ArrayList<String>(); optionList.add("-classpath"); optionList.add(System.getProperty("java.class.path") + ";dist/InlineCompiler.jar"); File helloWorldJava = new File(System.getProperty("user.dir") + "/generated-code/caserunners/org/bigtester/ate/model/project/CaseRunner8187856223134148550.java"); Iterable<? extends JavaFileObject> compilationUnit = fileManager .getJavaFileObjectsFromFiles(Arrays.asList(helloWorldJava)); JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, diagnostics, optionList, null, compilationUnit); /********************************************************************************************* Compilation Requirements **/ if (task.call()) { /** Load and execute *************************************************************************************************/ System.out.println("Yipe"); // Create a new custom class loader, pointing to the directory that // contains the compiled // classes, this should point to the top of the package structure! URLClassLoader classLoader = new URLClassLoader(new URL[] { new File(System.getProperty("user.dir") + "/generated-code/caserunners/").toURI().toURL() }); // Load the class from the classloader by name.... Class<?> loadedClass = classLoader .loadClass("org.bigtester.ate.model.project.CaseRunner8187856223134148550"); // Create a new instance... Object obj = loadedClass.newInstance(); // Santity check if (obj instanceof IRunTestCase) { ((IRunTestCase) obj).setCurrentExecutingTCName("test case name example"); Assert.assertEquals(((IRunTestCase) obj).getCurrentExecutingTCName(), "test case name example"); System.out.println("pass"); } /************************************************************************************************* Load and execute **/ } else { for (Diagnostic<? extends JavaFileObject> diagnostic : diagnostics.getDiagnostics()) { System.out.format("Error on line %d in %s%n", diagnostic.getLineNumber(), diagnostic.getSource().toUri()); } } }