Java Resource Path Get getFileFromBundle(String bundleName, String resourcePath)

Here you can find the source of getFileFromBundle(String bundleName, String resourcePath)

Description

Get File from bundle if exists

License

Open Source License

Parameter

Parameter Description

Return

according to the path from the specified bundle if exists

Declaration

public static File getFileFromBundle(String bundleName,
        String resourcePath) throws IOException, URISyntaxException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2016 Red Hat, Inc. /*from   w ww .j a v  a2  s .co 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:
 *        Red Hat Inc. - initial API and implementation and/or initial documentation
 *******************************************************************************/

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;
import org.osgi.framework.Bundle;

public class Main {
    /**
     * Get {@link File} from bundle if exists
     * @param {@link String} bundle name
     * @param {@link String} relative path to resource 
     * @return {@link File} according to the path from the specified bundle if exists
     */
    public static File getFileFromBundle(String bundleName,
            String resourcePath) throws IOException, URISyntaxException {
        Bundle bundle = Platform.getBundle(bundleName);
        URL fileURL = bundle.getEntry(resourcePath);
        URL resolvedFileURL = FileLocator.toFileURL(fileURL);
        URI resolvedURI = new URI(resolvedFileURL.getProtocol(),
                resolvedFileURL.getPath(), null);
        File file = new File(resolvedURI);
        return file;
    }
}

Related

  1. getBasePath(Class clazz, String resource)
  2. getChildResources(String path)
  3. getClassResource(Class clazz, String resPath)
  4. getClassResources(Class clazz, String resPath)
  5. getExistingResourceAsFile(final String resourcePath)
  6. getFileFromResource(Class clazz, String path)
  7. getFileResourcePaths(final Class clazz, final String fileNameRegex)
  8. getPath(final Object resource)
  9. getPathOfResource(Class clazz, String fileName)