How can I see if I'm running from JAR?

G

giangiammy

Hi all,

is it possible for a Java application identify if it is running
from a Jar file?
Any functions?

thanks
giammy
 
G

giangiammy

Found an example:
thanks
giammy

public class HelloClass {
public static void main(String[] args) {
new HelloClass().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!");
}
System.out.println(classJar);

}
}
 
A

Alan Krueger

is it possible for a Java application identify if it is running
from a Jar file?
Any functions?

boolean isRunningInJar() {
return getClass().getProtectionDomain().getCodeSource()
.getLocation().getPath().endsWith( ".jar" );
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,438
Messages
2,571,699
Members
48,796
Latest member
Greg L.
Top