Java tutorial
//package com.java2s; /* * Copyright 2002-2007 the original author or authors. * * 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. */ public class Main { /** * Given an input class object, return a string which consists of the class's package name as a pathname, i.e., all dots ('.') are replaced by slashes ('/'). Neither a leading nor trailing slash is added. The result could be concatenated with a slash and the name of a resource, and fed directly to <code>ClassLoader.getResource()</code>. For it to be fed to <code>Class.getResource</code> instead, a leading slash would also have to be prepended to the returned value. * * @param clazz * the input class. A <code>null</code> value or the default (empty) package will result in an empty string ("") being returned. * @return a path which represents the package name * @see ClassLoader#getResource * @see Class#getResource */ public static String classPackageAsResourcePath(Class<?> clazz) { if (clazz == null) { return ""; } String className = clazz.getName(); int packageEndIndex = className.lastIndexOf('.'); if (packageEndIndex == -1) { return ""; } String packageName = className.substring(0, packageEndIndex); return packageName.replace('.', '/'); } }