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

后端 未结 8 1504
星月不相逢
星月不相逢 2020-12-09 14:54

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,

相关标签:
8条回答
  • 2020-12-09 15:07

    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("").getProtocol();
    if(Objects.equals(protocol, "jar")){
        // run in jar
    } else if(Objects.equals(protocol, "file")) {
        // run in ide
    }
    
    0 讨论(0)
  • 2020-12-09 15:07

    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
    
    0 讨论(0)
  • 2020-12-09 15:08

    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.

    0 讨论(0)
  • 2020-12-09 15:17

    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
    
    0 讨论(0)
  • 2020-12-09 15:17

    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.

    0 讨论(0)
  • 2020-12-09 15:17

    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.

    0 讨论(0)
提交回复
热议问题