java 如何在war文件中运行java类文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6087876/
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-30 14:15:54  来源:igfitidea点击:

How to run java class file inside the war file

javawar

提问by ssbecse

i am having war file called Sample.war. in this war file i have the main class called "Maintest" .i want to run or execute the Maintest class from out side the war .how to do this

我有一个名为 Sample.war 的War文件。在这个War文件中,我有一个名为“Maintest”的主类。我想从War外部运行或执行 Maintest 类。如何做到这一点

回答by Gagravarr

War files are just Jar files with extra metadata. So, you should be fine to do:

War 文件只是带有额外元数据的 Jar 文件。所以,你应该没问题:

java -classpath application.war Maintest

That will load the class Maintestfrom the war file application.warand run it

这将从War文件application.war加载类Maintest并运行它

回答by Bojan Petkovic

same answer as I posted here: How do I run a class in a WAR from the command line?

与我在此处发布的答案相同:How do I run a class in a WAR from the command line?

Step 1: Unwrap the War file.

第 1 步:解开 War 文件。

jar -xvf MyWar.war

Step 2: move into the directory

第二步:进入目录

cd WEB-INF

Step 3: Run your main with all dependendecies

第 3 步:运行包含所有依赖项的 main

java -classpath "lib/*:classes/." my.packages.destination.FileToRun

回答by Thorbj?rn Ravn Andersen

WAR files are intended for servlet container deployment and not for stand-alone execution.

WAR 文件用于 servlet 容器部署,而不是用于独立执行。

Hence, there is no easy way to do what you want. Use executable jars for main-method application.

因此,没有简单的方法可以做你想做的事。将可执行 jar 用于主方法应用程序。

回答by Shailendra Singh

Unlike executable jar there won't be executable war file and why to run class inside war file. Classes inside war files are directly/indirectly loaded by web container/server. if really want to run a class inside a jar which is inside war file, then putting the jar in classpath it can be run as usual.

与可执行 jar 不同,不会有可执行的 war 文件以及为什么要在 war 文件中运行类。war 文件中的类由 Web 容器/服务器直接/间接加载。如果真的想在war文件中的jar中运行一个类,那么将jar放在类路径中它可以像往常一样运行。

回答by James Scriven

The entry point of a war is not a main method like it is for an application. The entry point of a war would be the web.xml, which is read by the application server and used to map urls to servlets.

War的入口点不像应用程序那样是主要方法。War的入口点是 web.xml,它由应用程序服务器读取并用于将 url 映射到 servlet。

If you really need to test a war, you could use an embedded application server like Jetty.

如果你真的需要测试一场War,你可以使用像 Jetty 这样的嵌入式应用服务器。