Error while executing WAR file

2019-08-07 05:22发布

I have used maven plugin to build a war file for my project structure. And now when I run this war file, I get the error

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

For some reason, when I check the war file, my main class is getting generated under WEB-INF/classes/com/abc/HelloWorld/

I have tried added a classpath to Manifest.MF file, but it has not helped.

Here is my maven plugin for creating a war file. Also this project contains an embedded jetty server.

<plugin>
  <artifactId>maven-war-plugin</artifactId>
  <version>2.1.1</version>
  <configuration>
  <webXml>WebContent\WEB-INF\web.xml</webXml>
    <warName>${project.artifactId}-${project.version}</warName>              
    <archive>
      <manifest>
        <addClasspath>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>

I have tried this question, but it has not helped.

1条回答
Lonely孤独者°
2楼-- · 2019-08-07 05:37

WebApp WAR files are a specialized jar files packaged for Web Containers to deploy from.

You can have a self-executing war file, but you'll wind up having a WAR file that has JAR behavior overlaid so that the META-INF/MANIFEST.MF and its Main-Class can instantiate Jetty, then load in the webapp itself into that Server instance.

Take a look at the following project maintained by the Jetty Project.

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

It is important to be careful when:

  • merging the Server classes into the WAR file to not make the webapp deployment fail (for overlapping classes between the server classpath and the webapp classpath).
  • merging META-INF/services/ files
  • not exposing too much of the Server side of the self-executable to HTTP clients (don't let them download anything sensitive!) - this can be accomplished by putting the server parts of this startup in the /WEB-INF/ directory
  • you don't want to have the webapp (war) startup walking and knowing about the server components (this can cause double initialization, once in the server classloader, and once again in the webapp classloader)

As stated on that project:

This project should provide a baseline for those investigating the use of Embedded Jetty from the point of view of a Self Executing WAR file.

The project has 4 main parts:

  • /thewebapp/ - this is the WAR file, the webapp, as it exists in its native format, with normal maven <packaging>war</packaging> and a produced artifact that is just a WAR file that isn't (yet) self-executing.
  • /theserver/ - this is the Embedded Jetty Server jetty.livewar.ServerMain.main(String args[]) which you customize to initialize your Jetty server and its WebApp. This project also is the place where you customize for things like JDBC servers libraries, JNDI, logging, etc. This project produces a uber-jar with all of the dependencies needed to run the server. Special care is taken with the maven-shade-plugin to merge META-INF/services/ files.
  • /server-bootstrap/ - this contains 2 small classes that sets up a LiveWarClassLoader from the content in the live WAR and then runs jetty.livewar.ServerMain.main(String args[]) from this new ClassLoader. This project also contains the live META-INF/MANIFEST.MF that the live WAR will need/use
  • /livewar-assembly/ - this is the project that ties together the above 3 projects into a Live/Executable WAR file. The artifacts from from the above 3 projects are unpacked by the maven-assembly-plugin and put into place where they will be most functional (and safe). For example, the server classes from /theserver/ are placed in /WEB-INF/jetty-server/ to make them inaccessible from Web Clients accessing the WAR file.

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.

查看更多
登录 后发表回答