Location of currently running class or JAR file

岁酱吖の 提交于 2019-12-04 03:09:59

I have managed to overcome this by wrapping my code within a "AccessController.doPrivileged" block i.e:

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 this approach works exactly as I want when the JAR file is located within the client's JVM directory i.e. points 2 and 3 from my original post. However when the same code runs when the code is being executed from within a Java script library, the following exception is thrown:

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

Which is fine because at least there is a differentiation there between 1 and (2 & 3) which I can then handle properly.

This solution was pointed out to me by the excellent Mikkel from lekkimworld.com (Twitter: @lekkim) so big thanks go out to him.

Chris Aldrich

I am not sure if this helps, but we "switch" our user.dir (System property) to make sure we run "out of" a certain directory for our application. In our case we have a web start application that we kick off through Lotus Notes (which calls a BAT file, which invokes Java Web Start (JAWS) ...)

But anyway, what we do is the following.

//Normally you don't ever want to do this......
Class clazz = ClassLoader.class;
Field field = clazz.getDeclaredField("sys_paths");
field.setAccessible(true);
field.setClass(clazz, null);
try {
     //Basically we read in the path and then loop through and remove our current
     //directory which is where we tend to "kick off from" rather than where we want
     //to run from.
     String minusCurrentDir = removeCurrentDirectoryFromPath(System.getProperty("java.library.path");
     System.setProperty("java.library.path", minusCurrentDir);
}
finally {
     field.setAccessible(true);
}

Now later on you can access and modify the user.dir property or ask it where it is. This may give you access to what you want (?) Or at least maybe the top code can help.

Your last line of code has myCL.getResouce(myName), where it should use myName2 instead. That could possibly be causing a problem. If it's using the version of the name with a leading '/', then you won't get anything from a ClassLoader.getResource() call. It has to have no leading slash. If that doesn't help, you could also try:

ClassLoader.getSystemResource(myName2); // name without leading slash

If that still doesn't work, it could be a security issue. According to ClassLoader.getResource() docs, it can return null if "the invoker doesn't have adequate privileges to get the resource". I just know Java, not Lotus, so I don't know what kind of security environment it runs the code in.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!