高效地从JAR文件内部加载类

huangapple 未分类评论42阅读模式
英文:

EFFICIENTLY Load classes from jar file INSIDE jar file

问题

Java程序员在这里。

我正在尝试在Java中创建一个“插件系统”,我想让用户能够导入他们自己的Java库,以便他们不仅仅局限于将我的插件加载应用程序添加到他们的构建路径中。

我打算允许用户在他们的项目中创建一个名为“lib”的文件夹,他们可以将所有其他需要的库放在其中,然后可以将这些库添加到构建路径中,以便在加载所有必要的插件时出现错误。但是,我在这方面遇到了问题。

这是我有的一个示例插件jar文件,它的结构与我想要的方式相同:

Plugin.jar
    |---org
        |---pluginpackage
            |---PluginMainClass.class
    |---lib
        |---user-lib.jar
            |---libpackage
                |---InterfaceA.class
                |---ClassB.class
                |---...
    |---plugin-description.xml

我想知道的是:加载用户在“lib”文件夹中加载的所有库的最有效方法是什么?

这是我目前的代码,但加载库需要的时间比预期的要长:

private void readLibraryInJar(JarInputStream jarLibraryInputStream, File dirOut) throws IOException {
        if (jarLibraryInputStream == null)
			return;

		JarEntry entry = null;

		while ((entry = jarLibraryInputStream.getNextJarEntry()) != null) {
			final String jarPath = entry.getName().trim();

			if (jarPath.endsWith(".class")) {

				File f = new File(dirOut, jarPath);

				if (!f.exists()) {
					if (!f.getParentFile().exists()) {
						f.getParentFile().mkdirs();
					}
					f.createNewFile();

				}

				byte[] buffer = new byte[1024 * 100];

				FileOutputStream out = new FileOutputStream(f);

				int len;

				while ((len = jarLibraryInputStream.read(buffer)) > 0) {
					out.write(buffer, 0, len);
				}

				out.close();

			}
		}
}

上述代码读取所需库中的每个类文件并将其内容复制到新文件中。我知道还有更高效的方法(例如,Apache Tomcat项目可以有一个“lib”文件夹),所以我想知道是否可以比我目前的方式更有效地加载库jar文件(打算使用URLClassLoader来加载它,所以如果您可以将它转换成URL,那将是很棒的,但如果这不可能,我仍然可以接受比我目前的方法更高效的任何方式)。

提前感谢!

英文:

Java programmer here.

I'm trying to make a "plugin system" in java, and I would like ot make it so that the user can import their own java libraries if they needed to so they are not limited to only have my plugin loading application in their build path.

I was planning on allowing the user to make a folder called "lib" in their project, which is where they would put all the other libraries they needed, so they can then add those to the build path with the application erroring when loading all the necessary plugins. But, I am having trouble to do so.

Here is an example plugin jar file I have, that is structured the way I want it to be:

Plugin.jar
    |---org
        |---pluginpackage
            |---PluginMainClass.class
    |---lib
        |---user-lib.jar
            |---libpackage
                |---InterfaceA.class
                |---ClassB.class
                |---...
    |---plugin-description.xml

What I was wondering is this: What would be the most efficient way to load all the libraries the user has loaded inside the "lib" folder?

This is the current code I have, but it takes longer than desired to load the libraries:

private void readLibraryInJar(JarInputStream jarLibraryInputStream, File dirOut) throws IOException {
        if (jarLibraryInputStream == null)
			return;

		JarEntry entry = null;

		while ((entry = jarLibraryInputStream.getNextJarEntry()) != null) {
			final String jarPath = entry.getName().trim();

			if (jarPath.endsWith(".class")) {

				File f = new File(dirOut, jarPath);

				if (!f.exists()) {
					if (!f.getParentFile().exists()) {
						f.getParentFile().mkdirs();
					}
					f.createNewFile();

				}

				byte[] buffer = new byte[1024 * 100];

				FileOutputStream out = new FileOutputStream(f);

				int len;

				while ((len = jarLibraryInputStream.read(buffer)) > 0) {
					out.write(buffer, 0, len);
				}

				out.close();

			}
		}
}

The above code reads each class file inside the desired library and copy's its content's into a new file. I know that a more efficient way is out there somewhere (e.g. Apache Tomcat project can have a "lib" folder), so I was wondering if I could load the library jar file more effeciently than I currently do (planning on using URLClassLoader to load it, so if you could transform it into a URL that would be awesome, but if that is impossible, I am still okay with any way more efficient than my current one.)

Thanks in advance!

huangapple
  • 本文由 发表于 2020年4月10日 02:49:41
  • 转载请务必保留本文链接:https://java.coder-hub.com/61128199.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定