Exemplo n.º 1
0
  public static File getResourceAsFile(String name, Class<?> thisClass) {
    ClassLoader classLoader = thisClass.getClassLoader();
    try {
      //noinspection ConstantConditions
      // String path = classLoader.getResource("").getPath();
      // ///C:/Users/tenti/Desktop/Projects/gate-basic/target/test-classes/
      return new File(classLoader.getResource(name).getFile());
    } catch (NullPointerException e) {
      try {
        // return new
        // File(Thread.currentThread().getContextClassLoader().getResource(name).getFile());
        // ClassLoader classLoader = Config.class.getClassLoader();
        // URL resource = classLoader.getResource(name);
        // String path = Paths.get(resource.toURI()).toAbsolutePath().toString();
        File file = null;
        URL url = null;

        if (classLoader != null) {
          url = classLoader.getResource(name);
        }
        if (url == null) {
          url = ClassLoader.getSystemResource(name);
        }
        if (url != null) {
          try {
            file = new File(url.toURI());
          } catch (URISyntaxException e5) {
            file = new File(url.getPath());
          }
        }
        if (file == null) {
          file = getResourceAsFileFromBuildFolder(name);
        }
        // return new File(path);
        return file;
      } catch (NullPointerException | IOException e2) {
        logger.error(e2.getMessage(), e2);
        return new File("");
      }
    }
  }