Can you tell on runtime if you're running java from within a jar?
jar, java
Solution
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
Problem
I have an application that some of my users run from Eclipse, and others run it by using a jar file. I want some actions to be done when running from within the jar, but I don't want them to be done when running from Eclipse. Is there a way to know on runtime whether the current application is running from within a jar? Thanks! Dikla