Here is my attempt to find the directory from which the bank is running.
public class JarLocation { public static void main(String[] args) { new JarLocation().say(); } public void say() { String className = this.getClass().getName().replace('.', '/'); String classJar = this.getClass().getResource("/" + className + ".class").toString(); if (classJar.startsWith("jar:")) { System.out.println("*** running from jar!"); } javax.swing.JOptionPane.showConfirmDialog((java.awt.Component) null, classJar, "jar location", javax.swing.JOptionPane.DEFAULT_OPTION); } }
First I create a "path" of the current class, replacing ".". "/", then I extract the class as a resource. The toString () method returns something like:
jar:file:/C:/temp/testing.jar!/my/package/JarLocation.class
From there you will parse this line to get the directory in which the jar is located.
RealHowTo
source share