繁体   English   中英

如何在Java应用程序之间共享.jar文件

[英]How to share .jar files between Java Applications

我们有一套通用的.Jar文件。 在安装每个应用程序时,这些文件将复制到每个应用程序的文件夹中。 这导致存储空间的浪费,并且使文件保持同步也是乏味的。 java是否提供共享.jars的任何方式? 像在Windows中一样,我们可以在\\ windows \\ system32中复制.dll文件,并且该文件可用于所有应用程序。

有几种选择:

  1. 您可以使用Maven之类的东西,它将为您管理JAR文件。

  2. 您可以创建一个库文件夹,并将其引用到您已有的项目中。

  3. 如果您使用的是Eclipse,则可以创建共享用户库( 在此处了解更多信息 )。

希望这可以帮助!

如果两个应用程序在同一台计算机上,则可以将jar部署/部署在一个文件夹中。
然后,两个应用程序都可以在运行时从该共享文件夹中选择/加载jar。 这是可行的。

假设您的文件夹是: /some/folder/

还有那罐子
a.jar
b.jar
c.jar

然后添加

/some/folder/a.jar
/some/folder/b.jar
/some/folder/c.jar

到两个应用程序的类路径(在两个应用程序的启动脚本中)。

但是在操作系统级别上没有一个预先定义的位置
将它们放入(比如说\\Windows\\System32
以便该计算机上的所有Java应用都可以从
那里。 我觉得这是一件非常好的事情。 所以事情不是
与DLL完全相同(您提供的示例)。

由于已实现Java 8版本40应用程序类数据共享,请参见

http://openjdk.java.net/jeps/310

花费了一点时间,但我有4个使用共享类运行的JVM。 以下脚本用于设置和测试共享,并且可以稍作修改就可以在其他地方使用:

#!/bin/bash

# Libraries to load
LIBS1="./lib/protobuf-java-2.6.1.jar:\
./lib/jetty-server-9.2.18.v20160721.jar:./lib/jetty-util-9.2.18.v20160721.jar:./lib/servlet-api-3.1.jar:./lib/jetty-http-9.2.18.v20160721.jar:./lib/jetty-io-9.2.18.v20160721.jar:\
./lib/derby.jar:\
./lib/json-simple-1.1.1.jar:"
LIBS2=":./lib/GTFS.jar"

# Uncomment these lines for the first phase where you are determining the classes to archive. During this phase aim to get as many classes loaded as possible
# which means loading a schedule and retrieving the stop list and next vehicle information
#
#APPCDS="-Xshare:off -XX:+UnlockCommercialFeatures -XX:+UseAppCDS -XX:DumpLoadedClassList=../GtfsAppCds.lst"
#java -Xmx512m $APPCDS -Dderby.system.home=database -classpath $LIBS1$LIBS2 com.transitrtd.GtfsOperatorManager

# Uncomment these lines when the class list is created and run to create the shared archive. Classes marked as unverifiable will need to be removed from the
# archived class list in GtfsAppCds.lst and the lines below run again. LIBS2 above contains jars which are left out of the archive. These are jars which change
# frequently and would therefore cause the archive to be frequently rebuilt.
#                                                                                                                                                                                                               
#APPCDS="-Xshare:dump -XX:+UnlockCommercialFeatures -XX:+UseAppCDS -XX:SharedClassListFile=../GtfsAppCds.lst -XX:SharedArchiveFile=../GtfsAppCds.jsa"
#java -Xmx512m $APPCDS -classpath $LIBS1

# Uncomment these lines when wishing to verify the application is using the shared archive.
#
#APPCDS="-Xshare:on -XX:+UnlockCommercialFeatures -XX:+UseAppCDS -XX:SharedArchiveFile=../GtfsAppCds.jsa -verbose:class"
#java -Xmx512m $APPCDS -Dderby.system.home=database -classpath $LIBS1$LIBS2 com.transitrtd.GtfsOperatorManager

请注意,共享存档文件(即jsa文件)取决于体系结构,并且需要在每种目标平台类型上进行构建。

另外,如果罐子使用密封包装,则会引发安全异常,请参见

https://docs.oracle.com/javase/tutorial/deployment/jar/sealman.html

有关密封包装的信息。 上面的derby.jar就是这种情况,但是可以通过解压缩jar文件,在清单中用Sealed:false替换Sealed:true并重新打包来解决问题。

如果需要将derby版本从10.10升级到10.14才能使用共享库,则不能在共享存档中使用使用Java的较早版本构建的jar。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM