Java Resource Path Get getResource(String relativePath)

Here you can find the source of getResource(String relativePath)

Description

get Resource

License

Open Source License

Declaration

public static File getResource(String relativePath) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2016 IBM Corporation.//from w ww  .j a v a2  s  .  c o  m
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 *    Contributors:
 *        IBM Corporation - initial API and implementation
 *******************************************************************************/

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.Platform;

public class Main {
    public static final String PLUGIN_ID = "org.eclipse.wst.jsdt.js.node.common.tests";

    public static File getResource(String relativePath) throws IOException {
        if (Platform.isRunning()) {
            URL fileURL = Platform.getBundle(PLUGIN_ID).getEntry(
                    relativePath);
            File file = null;
            try {
                if (FileLocator.resolve(fileURL).toString().indexOf(" ") > -1) {
                    URL location = FileLocator.resolve(fileURL);
                    URI resolvedUri = new URI(location.getProtocol(),
                            location.getPath(), null);
                    file = new File(resolvedUri);
                } else {
                    file = new File(FileLocator.resolve(fileURL).toURI());
                }

            } catch (URISyntaxException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            return file;
        }

        throw new IllegalStateException("Platform not running"); //$NON-NLS-1$
    }
}

Related

  1. getResource(Class cl, String path)
  2. getResource(final Class baseClass, final String path)
  3. getResource(final String path)
  4. getResource(String path)
  5. getResource(String path)
  6. getResource(String resourcePath)
  7. getResourceAsFile(Object relativeTo, String relativePath)
  8. getResourceAsFile(String relativePath)
  9. getResourceAsStream(final String path)