tags:

views:

441

answers:

6

Hi,

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

+4  A: 

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

For example, take this program:

package com.whatever;

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
Jon Skeet
This can't tell whether he's running from inside Eclipse, especially if other users also use a JAR.
Hosam Aly
I was interpreting the question as "running from a build created from Eclipse (i.e. in a bin directory) vs from a jar file". There's no mention of using the jar file *within* Eclipse. It's a vague question though.
Jon Skeet
I agree with you. Upon reading the question again, it's not clear whether his users run the application as an Eclipse plugin or a normal app (as an external tool). But I guess he'd be using a JAR in both cases, thus this may be more about the "startup path".
Hosam Aly
+1  A: 

From How-To

package com.rgagnon;

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);
 }
}

Will give:

>jar cvfm Hello.jar manifest.mft com\rgagnon\HelloClass.class
added manifest
adding: com/rgagnon/HelloClass.class (in=1059) (out=601) (deflated 43%)

>java com.rgagnon.HelloClass
file:/C:/DEV/WORK/JAVA/com/rgagnon/HelloClass.class

>java -jar Hello.jar
*** running from jar!
jar:file:/C:/DEV/WORK/JAVA/Hello.jar!/com/rgagnon/HelloClass.class


As pointed out by Hosam Aly, this does not answer exactly the question.
I leave it there for general reference, as a wiki answer.

VonC
He can't use this to tell whether he's running from within Eclipse.
Hosam Aly
+3  A: 

You could check the system class path property for the Equinox launcher:

if (System.getProperty("java.class.path").contains("org.eclipse.equinox.launcher")) {
    System.out.println("You're running inside Eclipse");
}

There are some other potential properties that you may check for, which you can find in Eclipse through Help -> About -> Configuration Details.

Jon's answer is good if you want to know whether you'r running from a JAR versus running from a bunch of class files. But if you use a JAR in both cases then it won't tell you what you need.

Hosam Aly
A: 

Another approach is putting information in the manifest file, and testing for if that information is available at runtime.

If it is, then the code was started with "-jar". Otherwise it was started in another way, e.g. directly from Eclipse.

Thorbjørn Ravn Andersen
A: 

You could try:

boolean inJar = false;

try
{
  CodeSource cs = DataResolver.class.getProtectionDomain().getCodeSource();
  inJar = cs.getLocation().toURI().getPath().endsWith(".jar");
}
catch (URISyntaxException e)
{
e.printStackTrace();
}

If you're running from a jar file then cs.getLocation().toURI() will give you the URI of that file; if you're running from inside Eclipse then it'll probably be the path to directory containing your class files.

Brother Logic
A: 

To Whom it may concern:

I also needed to know if an RCP application was being run from within Eclipse or from a product build. I found that System property "eclipse.pde.launch" only exists when running from Eclipse (using build 3.5.2). If System.getProperty("eclipse.pde.launch") == null then your running from the build.

Ralph