1 /* 2 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 import java.net.*; 25 import java.io.File; 26 import java.util.jar.*; 27 28 /** 29 * Attempts to load classes or resources from a JAR file. The load should succeed 30 * if the runtime supports the profile indicated by the Profile attribute, fail 31 * with UnsupportedProfileException otherwise. 32 */ 33 34 public class Basic { 35 36 static int indexOf(String profile) { 37 if (profile == null || "compact1".equals(profile)) return 1; 38 if ("compact2".equals(profile)) return 2; 39 if ("compact3".equals(profile)) return 3; 40 if ("".equals(profile)) return 4; 41 return Integer.MAX_VALUE; // unknown profile name 42 } 43 44 public static void main(String[] args) throws Exception { 45 if (args.length < 2) 46 throw new RuntimeException("Usage: java <jarfile> <classname>"); 47 String jar = args[0]; 48 String cn = args[1]; 49 50 File lib = new File(jar); 51 URL url = lib.toURI().toURL(); 52 URL urls[] = { url }; 53 54 // ## replace this if there is a standard way to determine the profile 55 String thisProfile = sun.misc.Version.profileName(); 56 57 String jarProfile = null; 58 try (JarFile jf = new JarFile(lib)) { 59 Manifest manifest = jf.getManifest(); 60 if (manifest != null) { 61 Attributes mainAttrs = manifest.getMainAttributes(); 62 if (mainAttrs != null) { 63 jarProfile = mainAttrs.getValue(Attributes.Name.PROFILE); 64 } 65 } 66 } 67 68 boolean shouldFail = indexOf(thisProfile) < indexOf(jarProfile); 69 70 try (URLClassLoader cl = new URLClassLoader(urls)) { 71 System.out.format("Loading %s from %s ...%n", cn, jar); 72 Class<?> c = Class.forName(cn, true, cl); 73 System.out.println(c); 74 if (shouldFail) 75 throw new RuntimeException("UnsupportedProfileException expected"); 76 } catch (UnsupportedProfileException x) { 77 if (!shouldFail) 78 throw x; 79 System.out.println("UnsupportedProfileException thrown as expected"); 80 } 81 82 try (URLClassLoader cl = new URLClassLoader(urls)) { 83 System.out.format("Loading resource from %s ...%n", jar); 84 URL r = cl.findResource("META-INF/MANIFEST.MF"); 85 System.out.println(r); 86 if (shouldFail) 87 throw new RuntimeException("UnsupportedProfileException expected"); 88 } catch (UnsupportedProfileException x) { 89 if (!shouldFail) 90 throw x; 91 System.out.println("UnsupportedProfileException thrown as expected"); 92 } 93 } 94 } 95