关于java:执行WAR文件时出错

Error while executing WAR file

我已经使用maven插件为我的项目结构构建了一个war文件。现在,当我运行这个war文件时,出现错误

1
Error: Could not find or load main class com.abc.HelloWorld.App

由于某种原因,当我检查war文件时,在WEB-INF/classes/com/abc/HelloWorld/

下生成了我的主类

我尝试将类路径添加到Manifest.MF文件,但没有帮助。

这是我用于创建war文件的maven插件。该项目还包含一个嵌入式码头服务器。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<plugin>
  maven-war-plugin</artifactId>
  <version>2.1.1</version>
  <configuration>
  <webXml>WebContent\\WEB-INF\\web.xml</webXml>
    <warName>${project.artifactId}-${project.version}</warName>              
   
      <manifest>
        true</addClasspath>
        <mainClass>com.infor.HelloWorld.App</mainClass>
      </manifest>
    </archive>
    <overlays>
    <overlay>
     <id>com.abc.HelloWorld</id>    
     <type>jar</type>
    </overlay>
    </overlays>
  </configuration>
  <executions>
    <execution>
      <id>default-war</id>
      <phase>package</phase>
      <goals>
        <goal>war</goal>
      </goals>
    </execution>
  </executions>
</plugin>

我尝试了这个问题,但没有帮助。


WebApp WAR文件是打包为Web容器部署的专用jar文件。

您可以有一个自执行的war文件,但最终会得到一个WAR文件,该文件具有JAR行为,因此META-INF/MANIFEST.MF及其Main-Class可以实例化Jetty,然后加载到webapp本身中进入该服务器实例。

看看以下由Jetty项目维护的项目。

https://github.com/jetty-project/embedded-jetty-live-war

在以下情况下请务必小心:

  • 将服务器类合并到WAR文件中,以确保webapp部署不会失败(服务器类路径和webapp类路径之间的类重叠)。
  • 合并META-INF/services/文件
  • 不要将过多的自我执行的Server端暴露给HTTP客户端(不要让它们下载任何敏感内容!)-这可以通过将该启动的服务器部分放在/WEB-INF/目录中来实现
  • 您不想让webapp(战争)启动程序走动并了解服务器组件(这可能会导致双重初始化,一次是在服务器类加载器中,一次是在webapp类加载器中)

关于该项目的说明:

该项目应为那些从自我执行WAR文件的angular研究嵌入式Jetty使用的人员提供基线。

该项目有4个主要部分:

  • /thewebapp/-这是WAR文件,即webapp,它以其本机格式存在,带有普通的maven <packaging>war</packaging>,并且生成的工件只是一个(尚未)自执行的WAR文件。
  • /theserver/-这是嵌入式Jetty服务器jetty.livewar.ServerMain.main(String args[]),您可以对其进行自定义以初始化Jetty服务器及其WebApp。您还可以在该项目中自定义JDBC服务器库,JNDI,日志记录等内容。此项目将生成一个uber-jar,其中包含运行服务器所需的所有依赖项。 maven-shade-plugin会特别注意合并META-INF/services/文件。
  • /server-bootstrap/-它包含2个小类,这些小类根据实时WAR中的内容设置LiveWarClassLoader,然后从此新的ClassLoader运行jetty.livewar.ServerMain.main(String args[])。该项目还包含实时WAR需要/使用的实时META-INF/MANIFEST.MF
  • /livewar-assembly/-这是将上述3个项目链接到一个Live / Executable WAR文件中的项目。来自上述3个项目的工件由maven-assembly-plugin进行解包,并放置在它们将发挥最大作用(且最安全)的位置。例如,将/theserver/中的服务器类放置在/WEB-INF/jetty-server/中,以使访问WAR文件的Web客户端无法访问它们。

Note: there are 3 files present in your new assembled WAR file that you should be aware of, as these files can be downloaded by a Web Client as static content if you use this setup.

  • /jetty/bootstrap/JettyBootstrap.class
  • /jetty/bootstrap/LiveWarClassLoader.class
  • /META-INF/MANIFEST.MF

The example project is setup in such a way that information present in these bootstrap files should not reveal private or sensitive information about your Server or its operations. Merely that the Webapp can be started as a Live/Executable WAR file.