Can you tell on runtime if you’re running java from within a jar?

Well, you can tell whether or not a class has been loaded from a JAR file – use Foo.class.getResource("Foo.class") and see whether the returned URL begins with “jar:”

For example, take this program:

public class Foo {

    public static void main(String[] args) {
        System.out.println(Foo.class.getResource("Foo.class"));
    }
}

Running it loading the file from the file system:

file:/C:/Users/Jon/Test/com/whatever/Foo.class

Running it from a jar file:

jar:file:/C:/Users/Jon/Test/foo.jar!/com/whatever/Foo.class

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)