Example usage for java.lang Class getResource

List of usage examples for java.lang Class getResource

Introduction

In this page you can find the example usage for java.lang Class getResource.

Prototype

@CallerSensitive
public URL getResource(String name) 

Source Link

Document

Finds a resource with a given name.

Usage

From source file:Main.java

public static ImageIcon createImageIcon(Class<?> bezugsklasse, String path) {
    final URL imgURL = bezugsklasse.getResource(path);
    if (imgURL != null) {
        return new ImageIcon(imgURL);
    }/*from w  ww. ja  va2  s .c o  m*/
    System.err.println("Couldn't find file: " + path);
    return null;
}

From source file:dev.argent.hive.SqlScriptLoader.java

@SuppressWarnings("unchecked")
public static String[] getScripts(Class<?> clazz, String sqlFileName) {
    try {/* w  ww.j  ava2 s  .  c  o  m*/
        String fileName = clazz.getResource(sqlFileName).getFile();
        String comments = stripComments((List<String>) FileUtils.readLines(new File(fileName)));
        return StringUtils.delimitedListToStringArray(comments, ";");
    } catch (IOException e) {
        log.warn(e.getMessage(), e);
        throw new IllegalArgumentException("SqlFile load failed. fileName : " + sqlFileName, e);
    }
}

From source file:Main.java

public static ImageIcon createImageIcon(Class<?> bezugsklasse, String path, int width, int height) {
    final URL imgURL = bezugsklasse.getResource(path);
    if (imgURL != null) {
        return new ImageIcon(
                new ImageIcon(imgURL).getImage().getScaledInstance(width, height, Image.SCALE_SMOOTH));
    }// ww w.java  2 s  . c om
    System.err.println("Couldn't find file: " + path);
    return null;
}

From source file:com.evanzeimet.queryinfo.QueryInfoTestUtils.java

public static String getFormattedJson(Class<?> clazz, String filename) throws IOException, QueryInfoException {
    URL url = clazz.getResource(filename);

    if (url == null) {
        String message = String.format("Resource not found at [%s]", filename);
        throw new QueryInfoException(message);
    }//w ww . ja v a  2  s.  com

    File file = new File(url.getPath());
    String rawContents = FileUtils.readFileToString(file);

    return dosToUnix(rawContents);
}

From source file:StylesExample8.java

public static void createDocumentStyles(StyleContext sc) {
    Style defaultStyle = sc.getStyle(StyleContext.DEFAULT_STYLE);

    // Create and add the main document style
    Style mainStyle = sc.addStyle(mainStyleName, defaultStyle);
    StyleConstants.setLeftIndent(mainStyle, 16);
    StyleConstants.setRightIndent(mainStyle, 16);
    StyleConstants.setFirstLineIndent(mainStyle, 16);
    StyleConstants.setFontFamily(mainStyle, "serif");
    StyleConstants.setFontSize(mainStyle, 12);

    // Create and add the constant width style
    Style cwStyle = sc.addStyle(charStyleName, null);
    StyleConstants.setFontFamily(cwStyle, "monospaced");
    StyleConstants.setForeground(cwStyle, Color.green);

    // Create and add the heading style
    Style heading2Style = sc.addStyle(heading2StyleName, null);
    StyleConstants.setForeground(heading2Style, Color.red);
    StyleConstants.setFontSize(heading2Style, 16);
    StyleConstants.setFontFamily(heading2Style, "serif");
    StyleConstants.setBold(heading2Style, true);
    StyleConstants.setLeftIndent(heading2Style, 8);
    StyleConstants.setFirstLineIndent(heading2Style, 0);

    // Create and add the Component style
    Class thisClass = StylesExample8.class;
    URL url = thisClass.getResource("java2s.gif");
    ImageIcon icon = new ImageIcon(url);
    JLabel comp = new JLabel("Displaying text with attributes", icon, JLabel.CENTER);
    comp.setVerticalTextPosition(JLabel.BOTTOM);
    comp.setHorizontalTextPosition(JLabel.CENTER);
    comp.setFont(new Font("serif", Font.BOLD | Font.ITALIC, 14));
    Style componentStyle = sc.addStyle(componentStyleName, null);
    StyleConstants.setComponent(componentStyle, comp);

    // The paragraph style for the component
    Style compParagraphStyle = sc.addStyle(compParaName, null);
    StyleConstants.setSpaceAbove(compParagraphStyle, (float) 16.0);
}

From source file:TextPaneElements.java

public static void createDocumentStyles(StyleContext sc) {
    Style defaultStyle = sc.getStyle(StyleContext.DEFAULT_STYLE);

    // Create and add the main document style
    Style mainStyle = sc.addStyle(mainStyleName, defaultStyle);
    StyleConstants.setLeftIndent(mainStyle, 16);
    StyleConstants.setRightIndent(mainStyle, 16);
    StyleConstants.setFirstLineIndent(mainStyle, 16);
    StyleConstants.setFontFamily(mainStyle, "serif");
    StyleConstants.setFontSize(mainStyle, 12);

    // Create and add the constant width style
    Style cwStyle = sc.addStyle(charStyleName, null);
    StyleConstants.setFontFamily(cwStyle, "monospaced");
    StyleConstants.setForeground(cwStyle, Color.green);

    // Create and add the heading style
    Style heading2Style = sc.addStyle(heading2StyleName, null);
    StyleConstants.setForeground(heading2Style, Color.red);
    StyleConstants.setFontSize(heading2Style, 16);
    StyleConstants.setFontFamily(heading2Style, "serif");
    StyleConstants.setBold(heading2Style, true);
    StyleConstants.setLeftIndent(heading2Style, 8);
    StyleConstants.setFirstLineIndent(heading2Style, 0);

    // Create and add the Component style
    Class thisClass = TextPaneElements.class;
    URL url = thisClass.getResource("java2s.gif");
    ImageIcon icon = new ImageIcon(url);
    JLabel comp = new JLabel("Displaying text with attributes", icon, JLabel.CENTER);
    comp.setVerticalTextPosition(JLabel.BOTTOM);
    comp.setHorizontalTextPosition(JLabel.CENTER);
    comp.setFont(new Font("serif", Font.BOLD | Font.ITALIC, 14));
    Style componentStyle = sc.addStyle(componentStyleName, null);
    StyleConstants.setComponent(componentStyle, comp);

    // The paragraph style for the component
    Style compParagraphStyle = sc.addStyle(compParaName, null);
    StyleConstants.setSpaceAbove(compParagraphStyle, (float) 16.0);
}

From source file:com.networknt.utility.Util.java

public static String getFrameworkVersion() {
    // this doesn't work.
    // TODO make it work.
    Class clazz = Util.class;
    URL location = clazz.getResource('/' + clazz.getName().replace('.', '/') + ".class");
    System.out.println("location = " + location);
    //location = jar:file:/Users/stevehu/project/light-example-4j/petstore/target/swagger-light-server-1.0.0.jar!/com/networknt/utility/Util.class
    return location.toString();
}

From source file:de.awtools.basic.io.AWToolsIOUtils.java

/**
 * Konvertiert eine Klassenpfad-Resource in ein <code>Image</code>.
 *
 * @param resource Eine Resource die im Klassenpfad liegt.
 * @param classLoader Der zu verwendende Klassenlader.
 * @return Das geladene Image./* ww  w.ja v a 2  s  .  c o m*/
 */
public static Image loadImage(final String resource, final Class<?> classLoader) {

    try {
        URL imageURL = classLoader.getResource(resource);
        // An alternative loading function:
        // return Toolkit.getDefaultToolkit().createImage(imageURL);
        return ImageIO.read(imageURL);
    } catch (IOException ex) {
        log.error("Fehler: ", ex);
        throw new UnhandledException(ex);
    }
}

From source file:com.cisco.dbds.utils.configfilehandler.ConfigFileHandler.java

/**
 * Load jar cong file./*  ww w .  j ava 2 s.  c o m*/
 *
 * @param Utilclass the utilclass
 */
public static void loadJarCongFile(Class Utilclass) {
    try {
        String path = Utilclass.getResource("").getPath();
        path = path.substring(6, path.length() - 1);
        path = path.split("!")[0];
        System.out.println(path);
        JarFile jarFile = new JarFile(path);

        final Enumeration<JarEntry> entries = jarFile.entries();
        while (entries.hasMoreElements()) {
            final JarEntry entry = entries.nextElement();
            if (entry.getName().contains(".properties")) {
                System.out.println("Jar File Property File: " + entry.getName());
                JarEntry fileEntry = jarFile.getJarEntry(entry.getName());
                InputStream input = jarFile.getInputStream(fileEntry);
                setSystemvariable(input);
                InputStreamReader isr = new InputStreamReader(input);
                BufferedReader reader = new BufferedReader(isr);
                String line;

                while ((line = reader.readLine()) != null) {
                    System.out.println("Jar file" + line);
                }
                reader.close();
            }
        }
        jarFile.close();
    } catch (Exception e) {
        System.out.println("Jar file reading Error");
    }
}

From source file:edu.duke.cabig.c3pr.AbstractTestCase.java

/**
 * Finds a file in the same module as the given class.  For example, say you have a package named
 * <kbd>web</kbd> and you need to refer to <kbd>web/src/main/foo/bar</kbd>.  Pass this method a
 * class that's in that module (i.e., compiled into <kbd>web/target/test-classes</kbd>) and
 * <kbd>src/main/foo/bar</kbd> to get the path to the local copy of
 * <kbd>web/src/main/foo/bar</kbd>.
 *
 * @param clazz    A class from the desired module
 * @param filename Path to the desired file, relative to the module root
 */// w w w .  jav a 2  s  .com
public static File getModuleRelativeFile(Class<?> clazz, String filename) {
    try {
        File classDirectory = new File(clazz.getResource("/").toURI());
        return new File(new File(classDirectory, "../.."), filename);
    } catch (URISyntaxException e) {
        throw new RuntimeException("Could not locate '/' relative to " + clazz.getName(), e);
    }
}