How to run java class file inside the war file [du

2019-02-16 20:58发布

This question already has an answer here:

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

标签: java war
5条回答
劳资没心,怎么记你
2楼-- · 2019-02-16 21:33

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

java -classpath application.war Maintest

That will load the class Maintest from the war file application.war and run it

查看更多
我想做一个坏孩纸
3楼-- · 2019-02-16 21:39

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.

查看更多
家丑人穷心不美
4楼-- · 2019-02-16 21:41

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

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

查看更多
倾城 Initia
5楼-- · 2019-02-16 21:42

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.

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

查看更多
太酷不给撩
6楼-- · 2019-02-16 21:57

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

Step 1: Unwrap the War file.

jar -xvf MyWar.war

Step 2: move into the directory

cd WEB-INF

Step 3: Run your main with all dependendecies

java -classpath "lib/*:classes/." my.packages.destination.FileToRun
查看更多
登录 后发表回答