Here you can find the source of getResourceFile(Class clazz, String relPath)
static public File getResourceFile(Class clazz, String relPath) throws IOException, URISyntaxException
//package com.java2s; /**//from ww w . j a va 2s . c o m * Copyright (c) 2010- 2011 QUT (Brisbane, Australia) and others. * 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: * Jim Steel - initial API and implementation * Joerg Kiegeland - extension for database storage */ import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.URISyntaxException; public class Main { static public File getResourceFile(Class clazz, String relPath) throws IOException, URISyntaxException { String uri = clazz.getResource(clazz.getSimpleName() + ".class") .toString(); String result = clazz.getResource(clazz.getSimpleName() + ".class") .toURI().getPath(); File file; if (result != null && result.indexOf("/bin/") != -1) { // standalone executed within Eclipse file = new File(result.substring(0, result.indexOf("/bin/")) + relPath); } else if (uri.indexOf("!/") != 0 && uri.startsWith("jar:file:")) { relPath = relPath.replace("/bin/", "/"); // standalone executed as jar uri = uri.substring("jar:file:".length()); uri = uri.substring(0, uri.indexOf("!/")); uri = uri.substring(0, uri.lastIndexOf("/")); uri = uri + "/resources" + relPath; file = new File(uri); InputStream s = clazz.getResourceAsStream(relPath); if (s != null) { // create parent folders file.getParentFile().mkdirs(); // extract from jar file to local file System.out.println("Extracting: " + relPath); streamToFile(file, s); } else System.out.println("Handle as new file: " + relPath); } else throw new RuntimeException("Cannot process relative path " + relPath); System.out.println("getResourceFile(" + clazz.getSimpleName() + "," + relPath + ") = " + file.toString()); return file; } private static void streamToFile(File file, InputStream in) throws IOException { OutputStream out = new FileOutputStream(file); // Copy the bits from instream to outstream byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } in.close(); out.close(); } }