当前位置: 动力学知识库 > 问答 > 编程问答 >

java - Classpaths and reflection

问题描述:

In a project that makes heavy use of reflection to select classes based on environment, is there a way to determine what libraries to include in the build? At the moment, taking a library out of the project is a gamble, but sticking with the current plan of "include everything, it's the only way to be sure" makes the whole thing bloated and unwieldy.

网友答案:

Unfortunately there isn't as this would be difficult for classes loaded dynamically, especially using factories where class name is passed as Strings.

If you jvm supports the -Xcomp and -Xbatch options along with the -XX:CompileThreshold, you can use it in your development and testing environment. It will increase your startup time though.

These options will just make your job easier but you will still probably have to test you thoroughly.

网友答案:

You can use JavaAssist lib to include new classpaths or modify classes at runtime.

Here is a brief tutorial

You can register a directory name as the class search path. For example, the following code adds a directory /usr/local/javalib to the search path:

  ClassPool pool = ClassPool.getDefault();
  pool.insertClassPath("/usr/local/javalib");

The search path that the users can add is not only a directory but also a URL:

  ClassPool pool = ClassPool.getDefault();
  ClassPath cp = new URLClassPath("www.javassist.org", 80, "/java/", "org.javassist.");
  pool.insertClassPath(cp);
分享给朋友:
您可能感兴趣的文章:
随机阅读: