下面列出了java.io.File#toURL ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static void main(String args[]) throws Exception {
// create files from given arguments and tools.jar
File f1 = new File(args[0]);
String home = System.getProperty("java.home");
String tools = ".." + File.separatorChar + "lib" +
File.separatorChar + "tools.jar";
File f2 = (new File(home, tools)).getCanonicalFile();
// create class loader
URL[] urls = { f1.toURL(), f2.toURL() };
URLClassLoader cl = new URLClassLoader(urls);
// load ListConnectors using the class loader
// and then invoke the list method.
Class c = Class.forName("ListConnectors", true, cl);
Method m = c.getDeclaredMethod("list");
Object o = c.newInstance();
m.invoke(o);
}
public Double genericNashornTest(final String benchmark, final String testPath, final String[] args) throws Throwable {
try {
final PerformanceWrapper wrapper = new PerformanceWrapper();
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final PrintStream ps = new PrintStream(baos);
final java.io.File test=new java.io.File(testPath);
final File absoluteFile=test.getAbsoluteFile();
@SuppressWarnings("deprecation")
final
URL testURL=absoluteFile.toURL();
wrapper.runExecuteOnlyTest(testPath, 0, 0, testURL.toString(), ps, System.err, args);
final byte[] output = baos.toByteArray();
final List<String> result = outputToStrings(output);
final Double _result = filterBenchmark(result, benchmark);
return _result;
} catch (final Throwable e) {
e.printStackTrace();
throw e;
}
}
public static URLClassLoader getCalcClassLoader() {
if (calcLoader == null) {
File cl = new File(Configuration.calc_indoor_libdir);// ��Jar��
URL indoor = null;
try {
indoor = cl.toURL();
} catch (Exception e) {
e.printStackTrace();
}
calcLoader = (new URLClassLoader(new URL[] { indoor }, Thread
.currentThread().getContextClassLoader()));
}
return calcLoader;
}
public static void main(String args[]) throws Exception {
// create files from given arguments and tools.jar
File f1 = new File(args[0]);
String home = System.getProperty("java.home");
String tools = ".." + File.separatorChar + "lib" +
File.separatorChar + "tools.jar";
File f2 = (new File(home, tools)).getCanonicalFile();
// create class loader
URL[] urls = { f1.toURL(), f2.toURL() };
URLClassLoader cl = new URLClassLoader(urls);
// load ListConnectors using the class loader
// and then invoke the list method.
Class c = Class.forName("ListConnectors", true, cl);
Method m = c.getDeclaredMethod("list");
Object o = c.newInstance();
m.invoke(o);
}
public static void main(String args[]) throws Exception {
// create files from given arguments and tools.jar
File f1 = new File(args[0]);
String home = System.getProperty("java.home");
String tools = ".." + File.separatorChar + "lib" +
File.separatorChar + "tools.jar";
File f2 = (new File(home, tools)).getCanonicalFile();
// create class loader
URL[] urls = { f1.toURL(), f2.toURL() };
URLClassLoader cl = new URLClassLoader(urls);
// load ListConnectors using the class loader
// and then invoke the list method.
Class c = Class.forName("ListConnectors", true, cl);
Method m = c.getDeclaredMethod("list");
Object o = c.newInstance();
m.invoke(o);
}
public static void main(String args[]) throws Exception {
// create files from given arguments and tools.jar
File f1 = new File(args[0]);
String home = System.getProperty("java.home");
String tools = ".." + File.separatorChar + "lib" +
File.separatorChar + "tools.jar";
File f2 = (new File(home, tools)).getCanonicalFile();
// create class loader
URL[] urls = { f1.toURL(), f2.toURL() };
URLClassLoader cl = new URLClassLoader(urls);
// load ListConnectors using the class loader
// and then invoke the list method.
Class c = Class.forName("ListConnectors", true, cl);
Method m = c.getDeclaredMethod("list");
Object o = c.newInstance();
m.invoke(o);
}
public Double genericNashornTest(final String benchmark, final String testPath, final String[] args) throws Throwable {
try {
final PerformanceWrapper wrapper = new PerformanceWrapper();
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final PrintStream ps = new PrintStream(baos);
final java.io.File test=new java.io.File(testPath);
final File absoluteFile=test.getAbsoluteFile();
@SuppressWarnings("deprecation")
final
URL testURL=absoluteFile.toURL();
wrapper.runExecuteOnlyTest(testPath, 0, 0, testURL.toString(), ps, System.err, args);
final byte[] output = baos.toByteArray();
final List<String> result = outputToStrings(output);
final Double _result = filterBenchmark(result, benchmark);
return _result;
} catch (final Throwable e) {
e.printStackTrace();
throw e;
}
}
static void go(String fn) throws Exception {
File f = new File(fn);
URL u = f.toURL();
String ufn = u.getFile();
if (!ufn.endsWith("/"))
throw new Exception(u + " does not end with slash");
if (ufn.endsWith("//"))
throw new Exception(u + " ends with two slashes");
}
static void go(String fn) throws Exception {
File f = new File(fn);
URL u = f.toURL();
String ufn = u.getFile();
if (!ufn.endsWith("/"))
throw new Exception(u + " does not end with slash");
if (ufn.endsWith("//"))
throw new Exception(u + " ends with two slashes");
}
static void go(String fn) throws Exception {
File f = new File(fn);
URL u = f.toURL();
String ufn = u.getFile();
if (!ufn.endsWith("/"))
throw new Exception(u + " does not end with slash");
if (ufn.endsWith("//"))
throw new Exception(u + " ends with two slashes");
}
public void preview(File file) {
try {
if(loader != null)
loader.stopClip();
name = file.getName();
setStatus("Loading " + name + "...");
loader = new Loader(file.toURL());
} catch(MalformedURLException e) {
Util.printStackTrace(e);
}
}
static void go(String fn) throws Exception {
File f = new File(fn);
URL u = f.toURL();
String ufn = u.getFile();
if (!ufn.endsWith("/"))
throw new Exception(u + " does not end with slash");
if (ufn.endsWith("//"))
throw new Exception(u + " ends with two slashes");
}
@SuppressWarnings("deprecation")
@Override
public URL apply(File from) {
try {
return from.toURL();
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
}
static void go(String fn) throws Exception {
File f = new File(fn);
URL u = f.toURL();
String ufn = u.getFile();
if (!ufn.endsWith("/"))
throw new Exception(u + " does not end with slash");
if (ufn.endsWith("//"))
throw new Exception(u + " ends with two slashes");
}
/**
* The Stub class is compiled by jtreg, but we want to move it so it is not
* on the application classpath. We want to load it through a separate
* classloader.
*/
static Class<?> getStub() throws Exception {
final String testclasses = System.getProperty("test.classes");
final File subdir = new File(testclasses, "stub");
subdir.mkdir();
final Path src = Paths.get(testclasses, "Stub.class");
final Path dest = subdir.toPath().resolve("Stub.class");
Files.move(src, dest, StandardCopyOption.REPLACE_EXISTING);
loader = new URLClassLoader(new URL[]{subdir.toURL()});
return Class.forName("Stub", true, loader);
}
static void go(String fn) throws Exception {
File f = new File(fn);
URL u = f.toURL();
String ufn = u.getFile();
if (!ufn.endsWith("/"))
throw new Exception(u + " does not end with slash");
if (ufn.endsWith("//"))
throw new Exception(u + " ends with two slashes");
}
public void testGetClasspathComponentsDontHaveDuplicates() throws MalformedURLException
{
String classpath = System.getProperty("java.class.path");
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
try
{
String pathSep = System.getProperty("path.separator");
File file = new File("/some/package/dir");
URL[] urls = new URL[] { file.toURL() };
// assert my package can't be found yet
List components = ClasspathUtils.getClasspathComponents();
Collections.sort(components);
assertTrue(Collections.binarySearch(components, file.getPath()) < 0);
// test my package is found and only appears once
URLClassLoader parentClassLoader = new URLClassLoader(urls, classLoader);
URLClassLoader childClassLoader = new URLClassLoader(new URL[] {}, parentClassLoader);
Thread.currentThread().setContextClassLoader(childClassLoader);
System.setProperty("java.class.path", classpath + pathSep + file.getPath());
components = ClasspathUtils.getClasspathComponents();
Collections.sort(components);
assertTrue(Collections.binarySearch(components, file.getPath()) >= 0);
assertTrue(frequency(components, file.getPath()) == 1);
}
finally
{
Thread.currentThread().setContextClassLoader(classLoader);
System.setProperty("java.class.path", classpath);
}
}
static void go(String fn) throws Exception {
File f = new File(fn);
URL u = f.toURL();
String ufn = u.getFile();
if (!ufn.endsWith("/"))
throw new Exception(u + " does not end with slash");
if (ufn.endsWith("//"))
throw new Exception(u + " ends with two slashes");
}
/**
* Finds the <tt>META-INF/sun-jaxb.episode</tt> file to add as a binding customization.
*/
public void scanEpisodeFile(File jar) throws BadCommandLineException {
try {
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();
addBindFile(new InputSource(url.toExternalForm()));
}
} catch (IOException e) {
throw new BadCommandLineException(
Messages.format(Messages.FAILED_TO_LOAD,jar,e.getMessage()), e);
}
}
/**
* Creates a <code>STLFileReader</code> object to read a STL file from a
* file. The data may be in ASCII or binary format. A progress monitor will
* show the progress during reading.
* @param file <code>File</code> object of STL file to read.
* @param parentComponent Parent <code>Component</code> of progress monitor.
* Use <code>null</code> if there is no parent.
* @throws IllegalArgumentException The file was structurally incorrect
*/
public STLFileReader(File file, Component parentComponent)
throws IllegalArgumentException, IOException
{
this(file.toURL(), parentComponent);
}