48

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

  • How do your users run your application from Eclipse? As a plugin? As an external tool? As simple classes from the bin folder? – Hosam Aly Jan 27 '09 at 08:50
  • Dikla, I am assuming you need this for a quick-n-dirty setup, check if System.console() == null, usually, when launched from eclipse, the console will be null. – Ustaman Sangat Jul 27 '12 at 22:41
  • But I guess launching a process headless (e.g. javaw in windows, or running the process in background in Linux) might also give you a null console - please check; pardon my laziness. – Ustaman Sangat Jul 27 '12 at 22:43
  • Same here! Inside the IDE i want to perform certain "dev" operations, totally different from the ones when running the JAR! – marcolopes Dec 03 '22 at 17:33

8 Answers8

48

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
MC Emperor
  • 22,334
  • 15
  • 80
  • 130
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    This can't tell whether he's running from inside Eclipse, especially if other users also use a JAR. – Hosam Aly Jan 27 '09 at 07:59
  • 6
    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 Jan 27 '09 at 08:14
  • 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 Jan 27 '09 at 08:27
  • 1
    Note that if you build the jar using the `Package required libraries into generated JAR` option in the export menu, Eclipse will use its jar-in-jar loader and the resource url will start with `rsrc:` (e.g. `rsrc:com/whatever/Foo.class`). Therefore, to be sure you're running from within a jar, you should check `if (resourcePath.startsWith("jar:") || resourcePath.startsWith("rsrc:"))` – starwarswii Jul 17 '17 at 15:48
  • If you obfuscate the class names in a build, you better use `foo.class.getName() + ".class"` instead of hardcoding the class name. – MasterHD Feb 22 '23 at 12:09
18

I have two better solutions. first:

URL url = this.getClass().getClassLoader().getResource("input.txt");
URLConnection urlConnection = url.openConnection();
if (urlConnection instanceof JarURLConnection) {
    // run in jar
} else {
    // run in ide
}

second:

String protocol = this.getClass().getResource(this.getClass.getName() + ".class").getProtocol();
if(Objects.equals(protocol, "jar")){
    // run in jar
} else if(Objects.equals(protocol, "file")) {
    // run in ide
}
zhenguoli
  • 2,268
  • 1
  • 14
  • 31
IT man
  • 563
  • 6
  • 9
  • 3
    I would consider opening a `URLConnection` to the resource to be very wasteful in terms of resources, and therefore not a "better" solution that simply checking the protocol. – Christopher Schultz Jun 20 '20 at 19:37
  • 1
    Will `this.getClass().getResource("")` return null? Should it be `this.getClass().getResource(this.getClass.getName() + ".class")`? – zhenguoli Jan 17 '22 at 07:02
  • @zhenguoli inside eclipse it should be this.getClass.getSimpleName() – marcolopes Dec 04 '22 at 03:17
8

If you query the JAR file name it will work if running from a JAR file otherwise it will return something like classes so the following code can be used:

import java.io.File;

public class JarUtilities
{
    public static String getJarName()
    {
        return new File(JarUtilities.class.getProtectionDomain()
                                .getCodeSource()
                                .getLocation()
                                .getPath())
                       .getName();
    }

    public static boolean runningFromJar()
    {
        return getJarName().contains(".jar");
    }
}

EDIT:

If you need more accuracy and to be resistant against renaming of the file extension, checking whether the file contains the MANIFEST.MF should work:

public static boolean runningFromJAR()
{
    try
    {
        String jarFilePath = new File(JarUtilities.class.getProtectionDomain()
                .getCodeSource()
                .getLocation()
                .getPath()).
                toString();
        jarFilePath = URLDecoder.decode(jarFilePath, "UTF-8");

        try (ZipFile zipFile = new ZipFile(jarFilePath))
        {
            ZipEntry zipEntry = zipFile.getEntry("META-INF/MANIFEST.MF");

            return zipEntry != null;
        }
    } catch (Exception exception)
    {
        return false;
    }
}
BullyWiiPlaza
  • 17,329
  • 10
  • 113
  • 185
  • 1
    It says that ´JarUtilities´ is inside a Jar but not if the running class which called ´JarUtilities´ is. So it has false positive matches. – deFreitas Jul 09 '22 at 15:14
8

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
  • 41,555
  • 36
  • 141
  • 182
  • This is specific to Eclipse, and not a good way to determine whether your code is running inside a JAR file in general. – foo Oct 08 '20 at 20:02
  • 3
    @foo The question specifically asked about detecting when it's run inside Eclipse. Other answers showed good ways to detect the JAR. This one adds a potential solution for detecting Eclipse. – Hosam Aly Oct 09 '20 at 07:15
  • Does not work (tested with Eclipse 3.8 IDE) – marcolopes Dec 03 '22 at 17:54
6

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.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • He can't use this to tell whether he's running from within Eclipse. – Hosam Aly Jan 27 '09 at 08:00
  • 2
    Well this is beginning to sound like finger printing... I think at this point it might be worth knowing what exactly the motivation is. If one needs to distinguish whether the app is running with a console, one could check if System.console() == null. When an application was launched by eclipse, it is null; but this is a very indirect deduction. – Ustaman Sangat Jul 27 '12 at 22:39
2

Here is some code you can run from a normally or as a jar.

import java.applet.Applet;
import java.net.URL;
import java.security.CodeSource;

public class TestApplet extends Applet {

    public TestApplet(){    
        URL path = TestApplet.class.getResource("TestApplet.class");

        if(path.toString().startsWith("jar:"))
            System.out.println("I'm in a jar");
        else
            System.out.println("I'm not in a jar");
    }

    public static void main(String args[]) throws Exception{
        new TestApplet();
    }
}

To put it in a jar and run it use the following:

jar cf test.jar TestApplet.class
java -cp test.jar TestApplet
AtlasMeh-ed
  • 469
  • 5
  • 7
  • 5
    Instead of the string manipulation, just use URL's getProtocol() method, which will return "jar" if you're in a jar. – mwoodman Jun 19 '14 at 16:32
1

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
  • 73,784
  • 33
  • 194
  • 347
  • E.g. you could provide a SplashScreen-Image in the manifest and then SplashScreen.getSplashScreen() will return non-null. – Matthew Wise Aug 14 '13 at 08:14
1

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
  • 286
  • 2
  • 9