Java – the location of the currently running class or jar file

I have a Lotus Notes database that is performing some interaction with remote web services I wrote a custom Java class to perform the interaction

According to the user's settings, class methods can be executed from one of three locations:

>In the java script library called through the Lotus Notes Java agent > in the jar file under the user's "JVM / lib / ext" Directory > in the jar file in the custom directory in the user's "JVM / lib" directory (for example, "JVM / lib / custom_dir") The Lotus Notes JVM uses "javauserclassesext" to create local notes Ini variable to understand the custom directory

In my class, I just want to return where the current class is executing Therefore, if executed from option 2 or option 3, the jar file path is returned If I execute from option 1, then return to other things I can handle

I have tried the following

Getprotectiondomain() method

getClass().getProtectionDomain().getCodeSource().getLocation()

The results are as follows:

java.security.AccessControlException: Access denied (java.lang.RuntimePermission getProtectionDomain)

There are no options to change the security settings of any client running this security setting

Class. Getresource method

String myName = "/" + getClass().getName().replace('.','/') + ".class";
URL myResourceURL = getClass().getResource(myName);

Result: myresourceurl is always null

ClassLoader. Getresource method

String myName2 = getClass().getName().replace('.','/') + ".class";
ClassLoader myCL = getClass().getClassLoader();
URL myResourceURL2 = myCL.getResource(myName);

Result: myresourceurl2 is always null

a) What's wrong with me?

and

b) How to use different methods to get the location of the currently executing class?

Solution

I have managed to overcome this by wrapping my code in the "accesscontroller. Doprivileged" block, that is:

final String[] myLocationViaProtectionDomain = {null};
AccessController.doPrivileged(new PrivilegedAction(){
    public Object run(){
        myLocationViaProtectionDomain[0] = getClass().getProtectionDomain().getCodeSource().getLocation().toString();
        debug("myLocationViaProtectionDomain: " + myLocationViaProtectionDomain[0]);
        return null;
    }
});

Interestingly, when the jar file is located in the JVM directory of the client (i.e. points 2 and 3 in my original post), this method is as normal as I want However, when running the same code when executing code from the java script library, the following exception is thrown:

java.security.AccessControlException: Access denied (java.lang.RuntimePermission getProtectionDomain)

This is good because at least there is a difference between 1 and (2 and 3), and I can handle it correctly

This solution was developed by lekkimworld Com's excellent mikkel (Twitter: @ lekkim) pointed it out to me, so thank him very much for coming forward

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>