将 WAR 添加到 Java 的类路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1186863/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 15:32:32  来源:igfitidea点击:

Adding WARs to Java's classpath

javajardependencieswarclassnotfound

提问by ripper234

I have two wars, foo.war and bar.war. foo uses classes from bar. I'm trying to start foo, and add to Java's classpath bar.war, but java throws a ClassNotFoundException.

我有两个War,foo.war 和 bar.war。foo 使用 bar 中的类。我试图启动 foo,并添加到 Java 的类路径 bar.war,但 java 抛出 ClassNotFoundException。

If I rename bar.war to bar.jar and edit its directory structure to look like a jar, it works.

如果我将 bar.war 重命名为 bar.jar 并编辑其目录结构使其看起来像一个 jar,它就可以工作。

Java's documentation on the -CP switch does not mention war files:

关于 -CP 开关的 Java 文档没有提到War文件:

 -classpath <class search path of directories and zip/jar files>
               A ; separated list of directories, JAR archives,
               and ZIP archives to search for class files.

回答by Jon Skeet

WAR files are meant to be whole web applications, not libraries. They containlibraries, but in themselves they're applications.

WAR 文件是整个 Web 应用程序,而不是库。它们包含库,但它们本身就是应用程序。

One web application shouldn't depend on another application. They may depend on the same libraries, but not on each other.

一个 Web 应用程序不应依赖于另一个应用程序。它们可能依赖于相同的库,但不依赖于彼此。

Basically extract out the common functionality, make it a library (as a jar file) and either include that jar file in both WAR files or add it to a common part of the classpath.

基本上提取出公共功能,使其成为一个库(作为一个 jar 文件),并将该 jar 文件包含在两个 WAR 文件中,或者将其添加到类路径的公共部分。

回答by Bhushan Bhangale

The structure of a war file is very different then a jar so it doesn't work. The Java class files are kept inside classes directory of the WAR whereas Jar directly have the Java class files. Why don't you add the folder where you have the Java class files of bar.war?

war 文件的结构与 jar 非常不同,因此它不起作用。Java 类文件保存在 WAR 的 classes 目录中,而 Jar 直接包含 Java 类文件。为什么不添加bar.war 的Java 类文件所在的文件夹?

What are you trying to achieve?

你想达到什么目的?