你能告诉运行时你是否在jar中运行java? [英] Can you tell on runtime if you're running java from within a jar?

查看:113
本文介绍了你能告诉运行时你是否在jar中运行java?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,我的一些用户从Eclipse运行,而其他用户使用jar文件运行它。

I have an application that some of my users run from Eclipse, and others run it by using a jar file.

我想在运行时执行一些操作从jar中运行,但我不希望它们在从Eclipse运行时完成。

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?

谢谢!

Dikla

推荐答案

那么,您可以判断是否已从JAR文件加载类 - 使用 Foo.class.getResource(Foo.class)并查看返回的URL是否以jar:开头。

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:"

例如,请使用此程序:

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

从jar文件运行它:

jar:file:/C:/Users/Jon/Test/foo.jar!/com/whatever/Foo.class

这篇关于你能告诉运行时你是否在jar中运行java?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆