Setting Jar's Class Path

xiaoxiao2021-02-27  220

如何在我们的Jar引用另外的JAR, 目前知道的有两种方法: 1. 通过-classpath 参数指定引用JAR的路径信息 2. 在JAR中定义MANIFEST.MF, 然后在MANIFEST.MF里面指定引用到的JAR的相对路径 比较一下这两种方法: 1. 用第一种方法的话,如果引用的JAR比较少,那么在命令行中通过-classpath flag也OK如下:     > java MyJar.jar -classpath MyUtilies.jar 2. 但是如果引用的JAR比较多的话,还是通过命令行来指定classpath就显得太过冗长了,而且每次远行你的JAR时都需要写一次,太麻烦了,这时就推荐第二种方法,将classpath定义在MANIFEST.MF. 如何创建MANIFEST.MF 1. 创建一个文件: Manifest.txt,加入如下内容: Class-Path: MyUtils.jar
Warning: The text file must end with a new line or carriage return. The last line will not be parsed properly if it does not end with a new line or carriage return. 2. 创建你的JAR:      jar cfm MyJar.jar Manifest.txt MyPackage/*.class 如果你用Maven的ART plugin的话,可以配置如下:             <plugin>                 <groupId>org.apache.maven.plugins</groupId>                 <artifactId>maven-antrun-plugin</artifactId>                 <version>1.7</version>                 <executions>                     <execution>                         <id>jar</id>                         <phase>process-classes</phase>                         <configuration>                             <target>                                 <jar destfile="target/lib/${project.name}.jar"                                     basedir="target/classes">                                     <include name="**/*" />                                     <manifest>                                        <attribute name="Class-Path" value="MyUtils.jar" />                                    </manifest>                                 </jar>                             </target>                         </configuration>                         <goals>                             <goal>run</goal>                         </goals>                     </execution>                 </executions>             </plugin> 详情参考Oracel官方文档: http://docs.oracle.com/javase/tutorial/deployment/jar/downman.html
转载请注明原文地址: https://www.6miu.com/read-10011.html

最新回复(0)