Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I run a class in a WAR from the command line?

Tags:

java

jar

war

I have a Java class which has a main and I used to run as a standalone app from the command line e.g.

java -jar myjar.jar params 

I needed to repackage the code to run under apache and all my code, including the entry point class from the old jar, has ended up in a WAR file for easy deplyment into the web server.

However, I still want to be able to run it from the command line and the code has not changed and is all in there, I just can't figure out how to get it to run.

Here's what I tried...

I presumed the WAR was just like a jar, so

java -jar mywar.war params 

That failed saying there was no main class defined in the manifest.

I manually added a manifest to the war and tried again, with the same effect.

I noticed that in my war I had a folder called META-INF containing a manifest.mf, so I added a line to that declaring my main class as I would to a normal manifest...

Manifest-Version: 1.0 Main-Class: mypackage.MyEntryPointClass 

This gave a noClassDefFoundError mypackage.MyEntryPointClass, which is progress of a sort. That led me to believe that it was just a path issue, so I tried

Manifest-Version: 1.0 Main-Class: WEB-INF.classes.mypackage.MyEntryPointClass 

I now get the same error, but with a stack trace...

Exception in thread "main" java.lang.NoClassDefFoundError: WEB-INF/classes/mypackage/MyEntryPointClass (wrong name: mypackage/MyEntryPointClass)         at java.lang.ClassLoader.defineClass1(Native Method)         at java.lang.ClassLoader.defineClass(Unknown Source)         at java.security.SecureClassLoader.defineClass(Unknown Source)         at java.net.URLClassLoader.defineClass(Unknown Source)         at java.net.URLClassLoader.access$100(Unknown Source)         at java.net.URLClassLoader$1.run(Unknown Source)         at java.security.AccessController.doPrivileged(Native Method)         at java.net.URLClassLoader.findClass(Unknown Source)         at java.lang.ClassLoader.loadClass(Unknown Source)         at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)         at java.lang.ClassLoader.loadClass(Unknown Source)         at java.lang.ClassLoader.loadClassInternal(Unknown Source) 

I've googled for a bit but can't find anything which answers my question, and I read a couple of other questions here which are slightly different, so I thought I would post.

Java 1.5, not that I think that should make any difference.

like image 606
Simon Avatar asked Dec 03 '09 21:12

Simon


People also ask

How do I run a war file class?

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.

Can we run war file in CMD?

Run the WAR file Open up a terminal/command prompt window to the download directory. Run the command java -jar jenkins. war . Browse to http://localhost:8080 and wait until the Unlock Jenkins page appears.


2 Answers

Similar to what Richard Detsch but with a bit easier to follow (works with packages as well)

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 
like image 140
Bojan Petkovic Avatar answered Sep 24 '22 10:09

Bojan Petkovic


You can do what Hudson (continuous integration project) does. you download a war which can be deployed in tomcat or to execute using

java -jar hudson.war 

(Because it has an embedded Jetty engine, running it from command line cause a server to be launched.) Anyway by looking at hudson's manifest I understand that they put a Main class in the root for the archive. In your case your war layout should be look like:

under root:

  • mypackage/MyEntryPointClass.class
  • WEB-INF/lib
  • WEB-INF/classes
  • META-INF/MANIFEST.MF

while the manifest should include the following line:

Main-Class: mypackage.MyEntryPointClass 

please notice that the mypackage/MyEntryPointClass.class is accessable from the command line only, and the classes under WEB-INF/classes are accessable from the application server only.

HTH

like image 36
Yonatan Maman Avatar answered Sep 23 '22 10:09

Yonatan Maman