public static String getCodeBase(Class<?> cls) { if (cls == null) { return null; } ProtectionDomain domain = cls.getProtectionDomain(); if (domain == null) { return null; } CodeSource source = domain.getCodeSource(); if (source == null) { return null; } URL location = source.getLocation(); if (location == null) { return null; } return location.getFile(); }
return new File(MyClass.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath());
/** * Returns location of the class. If class is not in a jar, it's classpath * is returned; otherwise the jar location. */ public static String classLocation(Class clazz) { return clazz.getProtectionDomain().getCodeSource().getLocation().getPath(); }
public class Test { public static void main(String... args) throws Exception { URL location = Test.class.getProtectionDomain().getCodeSource().getLocation(); System.out.println(location.getFile()); } }
package org.life.java.so.questions; /** * * @author jigar */ public class GetClassPath { public static void main(String[] args) { System.out.println(GetClassPath.class.getProtectionDomain().getCodeSource().getLocation().getPath()); } }
System.out.println(Base64.class.getProtectionDomain().getCodeSource().getLocation());
private String getSparkexJar() { try { Path path = Paths.get(EnableSparkSupportMagicCommand.class.getProtectionDomain().getCodeSource().getLocation().toURI()); return path.getParent().getParent().getParent().resolve("sparkex").resolve("lib").resolve("sparkex.jar").toString(); } catch (Exception e) { throw new RuntimeException(e); } } }
/** * Get the code source file or class path of the Class passed in. * * @param clazz * @return Jar file name or class path. */ public static String getCodeSource(Class<?> clazz) { ProtectionDomain protectionDomain = clazz.getProtectionDomain(); if (protectionDomain == null || protectionDomain.getCodeSource() == null) { return null; } CodeSource codeSource = clazz.getProtectionDomain().getCodeSource(); URL location = codeSource.getLocation(); if (location == null) { return null; } String path = codeSource.getLocation().toExternalForm(); if (path.endsWith(".jar") && path.contains("/")) { return path.substring(path.lastIndexOf('/') + 1); } return path; }
private static void setRunningLocation(CliMain m) { mRunningLocation = m.getClass().getProtectionDomain().getCodeSource().getLocation().getPath(); try { mRunningLocation = URLDecoder.decode(mRunningLocation, "utf-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } if (mRunningLocation.endsWith(".jar")) { mRunningLocation = mRunningLocation.substring(0, mRunningLocation.lastIndexOf(File.separator) + 1); } File f = new File(mRunningLocation); mRunningLocation = f.getAbsolutePath(); }
public static void main(String[] args) throws IOException { String f = VideoAudioChecker.class.getProtectionDomain().getCodeSource().getLocation().getFile() + "/count-video.mp4"; IsoFile isoFile = new IsoFile(new FileInputStream(f).getChannel()); System.err.println(getType(isoFile)); }
protectionDomain = EmbulkVersion.class.getProtectionDomain(); } catch (SecurityException ex) { System.err.println("Embulk version unavailable due to ProtectionDomain inaccessible."); ex.printStackTrace(); return null; final CodeSource codeSource = protectionDomain.getCodeSource(); if (codeSource == null) { System.err.println("Embulk version unavailable due to CodeSource unavailable."); return null; final URL selfJarUrl = codeSource.getLocation(); if (selfJarUrl == null) { System.err.println("Embulk version unavailable due to the location of CodeSource unavailable."); return null; } else if (!selfJarUrl.getProtocol().equals("file")) { final String selfJarPathString = selfJarUrl.getPath(); if (selfJarPathString == null) { System.err.println("Embulk version unavailable due to the path of CodeSource unavailable.");
private static void OutputJaxpImplementationInfo() { System.out.println(getJaxpImplementationInfo("DocumentBuilderFactory", DocumentBuilderFactory.newInstance().getClass())); System.out.println(getJaxpImplementationInfo("XPathFactory", XPathFactory.newInstance().getClass())); System.out.println(getJaxpImplementationInfo("TransformerFactory", TransformerFactory.newInstance().getClass())); System.out.println(getJaxpImplementationInfo("SAXParserFactory", SAXParserFactory.newInstance().getClass())); } private static String getJaxpImplementationInfo(String componentName, Class componentClass) { CodeSource source = componentClass.getProtectionDomain().getCodeSource(); return MessageFormat.format( "{0} implementation: {1} loaded from: {2}", componentName, componentClass.getName(), source == null ? "Java Runtime" : source.getLocation()); }
String path = Test.class.getProtectionDomain().getCodeSource().getLocation().getPath(); String decodedPath = URLDecoder.decode(path, "UTF-8");
public static String getCodeBase(Class<?> cls) { if (cls == null) { return null; } ProtectionDomain domain = cls.getProtectionDomain(); if (domain == null) { return null; } CodeSource source = domain.getCodeSource(); if (source == null) { return null; } URL location = source.getLocation(); if (location == null) { return null; } return location.getFile(); }
public String getExecJarPathLocal () throws URISyntaxException { // returns the location on disc of the jar of this class. return DagUtils.class.getProtectionDomain().getCodeSource().getLocation().toURI().toString(); }
static String getEagleEyeLocation() { try { URL resource = EagleEye.class.getProtectionDomain().getCodeSource().getLocation(); if (resource != null) { return resource.toString(); } } catch (Throwable t) { // ignore } return "unknown location"; }
@Override public String toString() { final URL location = klass.getProtectionDomain().getCodeSource().getLocation(); try { final String jar = new File(location.toURI()).getName(); if (jar.endsWith(".jar")) { return jar; } return "project.jar"; } catch (Exception ignored) { return "project.jar"; } } }
public static void main(String[] args) throws IOException { Movie movie = MovieCreator.build(DumpAmf0TrackToPropertyFile.class.getProtectionDomain().getCodeSource().getLocation().getFile() + "/example.f4v"); for (Track track : movie.getTracks()) { if (track.getHandler().equals("data") ) { long time = 0; Iterator<Sample> samples = track.getSamples().iterator(); Properties properties = new Properties(); File f = File.createTempFile(DumpAmf0TrackToPropertyFile.class.getSimpleName(), "" + track.getTrackMetaData().getTrackId()); for (long decodingTime : track.getSampleDurations()) { ByteBuffer sample = samples.next().asByteBuffer(); byte[] sampleBytes = new byte[sample.limit()]; sample.reset(); sample.get(sampleBytes); properties.put("" + time, new String(Base64.encodeBase64(sampleBytes, false, false))); time += decodingTime; } FileOutputStream fos = new FileOutputStream(f); System.err.println(properties); properties.store(fos, ""); } } }
@Test @Ignore public void testVWLoadMocker() { String cmd = "java -cp . -Duser.dir=" + VWLoadMocker.class.getProtectionDomain().getCodeSource().getLocation().getPath() + " org.pentaho.di.trans.steps.ivwloader.VWLoadMocker 5000 0 /tmp/error.txt"; // String cmd ="java -version"; try { System.out.println( cmd ); Process pr = Runtime.getRuntime().exec( cmd ); pr.getOutputStream().write( ( "testVWLoadMaker" + lineSeparator ).getBytes() ); pr.getOutputStream().flush(); pr.getOutputStream().write( ( "\\q" + lineSeparator ).getBytes() ); pr.getOutputStream().flush(); int i; System.out.println( i = pr.waitFor() ); assertEquals( 0, i ); } catch ( IOException e ) { e.printStackTrace(); fail( e.toString() ); } catch ( InterruptedException e ) { e.printStackTrace(); fail( e.toString() ); } }
private static File whoAmI() throws IOException { // JNLP returns the URL where the jar was originally placed (like http://...) // not the local cached file. So we need a rather round about approach to get to // the local file name. // There is no portable way to find where the locally cached copy // of war/jar is; JDK 6 is too smart. (See JENKINS-2326.) try { return whoAmIFromJnlp(); } catch (final Exception x) { getSystemErrorStream().println( "INFO: ZipFile.name trick did not work (" + x.toString() + "), using fallback"); } final File myself = File.createTempFile("javamelody", ".jar"); myself.deleteOnExit(); final InputStream is = Main.class.getProtectionDomain().getCodeSource().getLocation() .openStream(); try { final OutputStream os = new FileOutputStream(myself); try { copyStream(is, os); } finally { os.close(); } } finally { is.close(); } return myself; }