Java tutorial
//package com.java2s; /* * #%L * HAPI FHIR - Core Library * %% * Copyright (C) 2014 University Health Network * %% * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * #L% */ import java.net.MalformedURLException; import java.net.URL; public class Main { private static URL getRootUrlForClass(Class<?> cls) { ClassLoader classLoader = cls.getClassLoader(); String resource = cls.getName().replace('.', '/') + ".class"; if (classLoader == null) { // A null class loader means the bootstrap class loader. In this case we use the // system class loader. This is safe since we can assume that the system class // loader uses parent first as delegation policy. classLoader = ClassLoader.getSystemClassLoader(); } URL url = classLoader.getResource(resource); if (url == null) { return null; } String file = url.getFile(); if (file.endsWith(resource)) { try { return new URL(url.getProtocol(), url.getHost(), url.getPort(), file.substring(0, file.length() - resource.length())); } catch (MalformedURLException ex) { return null; } } else { return null; } } }