Java Class.getResource Examples

Java Class.getResource - 30 examples found. These are the top rated real world Java examples of Class.getResource extracted from open source projects. You can rate examples to help us improve the quality of examples.
  /**
   * This method open stream through the current context
   *
   * @param aFileStream file pname to open
   * @return Stream on file specified by aFileStream
   * @throws MalformedURLException
   */
  public URL openStreamUrl(String aFileStream, Class<?> clazz) throws MalformedURLException {
    URL out = ClassLoader.getSystemResource(aFileStream);

    if (out == null) {
      ResourceResolver.LOGGER.info("Default class loader failed (file:" + aFileStream + ") !");
      out = clazz.getResource(aFileStream);
    }

    if (out == null) {
      ResourceResolver.LOGGER.info(
          "ResourceResolver class loader failed (file:" + aFileStream + ") !");
      out = ClassLoader.getSystemResource("/" + aFileStream);
    }

    if (out == null) {
      ResourceResolver.LOGGER.info(
          "ResourceResolver class loader failed (+ /) (file:" + aFileStream + ") !");
      out = clazz.getResource("/" + aFileStream);
    }

    if (out == null) {
      out = resourceResolverHook(aFileStream);
    }

    if (out == null) {
      ResourceResolver.LOGGER.log(Level.SEVERE, "FileNotFoundException");
    }

    if (out != null) {
      ResourceResolver.LOGGER.info("Success found file:" + aFileStream + " !");
    }

    return out;
  }
  /**
   * Setting the internal field {@link #document} directly (bypassing {@link #setDocument(URL)}) is
   * used to deplay the document loading until {@link #ready()}.
   */
  @Override
  protected void initialize(final String[] as) {
    if ("Linux".equals(System.getProperty("os.name")))
      getContext().getResourceManager().setPlatform("linux");

    final Class<?> mc = this.getClass();
    {
      final ResourceMap r = Application.getInstance().getContext().getResourceMap();
      initialScene =
          mc.getResource("/" + r.getResourcesDir() + r.getString("Application.defaultDocument"));
      templateScene =
          mc.getResource("/" + r.getResourcesDir() + r.getString("Application.templateDocument"));
    }

    // schedule the document to load in #ready()
    document = initialScene;
    for (final String p : as) {
      // ignore javaws parameters
      if ("-open".equals(p) || "-print".equals(p)) continue;
      try {
        document = new URL(p);
        break;
      } catch (final MalformedURLException e) {
        final File f = new File(p);
        if (f.canRead())
          try {
            document = f.toURL();
            break;
          } catch (final MalformedURLException e2) {
            log.warn("Cannot load '" + p + "'.", e);
          }
        else log.warn("Cannot load '" + p + "'.", e);
      }
    }
  }
  private NativeLibrary openLibraryImpl(final boolean global) throws URISyntaxException {
    final ClassLoader cl = getClass().getClassLoader();
    System.err.println("CL " + cl);

    String libBaseName = null;
    final Class<?> clazz = this.getClass();
    Uri libUri = null;
    try {
      libUri = Uri.valueOf(clazz.getResource("/libtest1.so"));
    } catch (final URISyntaxException e2) {
      // not found .. OK
    }
    if (null != libUri) {
      libBaseName = "libtest1.so";
    } else {
      try {
        libUri = Uri.valueOf(clazz.getResource("/test1.dll"));
        if (null != libUri) {
          libBaseName = "test1.dll";
        }
      } catch (final URISyntaxException e) {
        // not found
      }
    }
    System.err.println("Untrusted Library (URL): " + libUri);

    if (null != libUri) {
      Uri libDir1 = libUri.getContainedUri();
      System.err.println("libDir1.1: " + libDir1);
      libDir1 = libDir1.getParent();
      System.err.println("libDir1.2: " + libDir1);
      System.err.println("Untrusted Library Dir1 (abs): " + libDir1);
      final Uri absLib = libDir1.concat(Uri.Encoded.cast("natives/" + libBaseName));
      Exception se0 = null;
      NativeLibrary nlib = null;
      try {
        nlib = NativeLibrary.open(absLib.toFile().getPath(), cl);
        System.err.println("NativeLibrary: " + nlib);
      } catch (final SecurityException e) {
        se0 = e;
        if (usesSecurityManager) {
          System.err.println("Expected exception for loading native library");
          System.err.println("Message: " + se0.getMessage());
        } else {
          System.err.println("Unexpected exception for loading native library");
          se0.printStackTrace();
        }
      }
      if (!usesSecurityManager) {
        Assert.assertNull("SecurityException thrown on loading native library", se0);
      } else {
        Assert.assertNotNull("SecurityException not thrown on loading native library", se0);
      }
      return nlib;
    } else {
      System.err.println("No library found");
      return null;
    }
  }
Example #4
0
 /**
  * 取得当前类所在的文件
  *
  * @param clazz
  * @return
  */
 public static File getClassFile(Class clazz) {
   URL path =
       clazz.getResource(
           clazz.getName().substring(clazz.getName().lastIndexOf(".") + 1) + ".classs");
   if (path == null) {
     String name = clazz.getName().replaceAll("[.]", "/");
     path = clazz.getResource("/" + name + ".class");
   }
   return new File(path.getFile());
 }
 private static String locationFor(String name) throws RemoteException, RemoteException {
   startManagers();
   URL location = CLASS_SERVLET_CONTEXT.getResource("resources/" + name);
   if (location == null && CLASS_JSP_CONTEXT != null) {
     location = CLASS_JSP_CONTEXT.getResource("resources/" + name);
   }
   if (location == null) {
     log.warn(sm.getString("digesterFactory.missingSchema", name));
     return null;
   }
   return location.toExternalForm();
 }
 @NotNull
 @Override
 protected String compute() {
   final Class<JavaFxHtmlPanel> clazz = JavaFxHtmlPanel.class;
   //noinspection StringBufferReplaceableByString
   return new StringBuilder()
       .append("<script src=\"")
       .append(clazz.getResource("scrollToElement.js"))
       .append("\"></script>\n")
       .append("<script src=\"")
       .append(clazz.getResource("processLinks.js"))
       .append("\"></script>\n")
       .toString();
 }