本文介绍了如何从命令行在 WAR 中运行类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Java 类,它有一个主类,我曾经从命令行作为独立应用程序运行,例如

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

我需要重新打包代码以在 apache 下运行,而我的所有代码,包括来自旧 jar 的入口点类,都以一个 WAR 文件结束,以便于部署到 Web 服务器中.

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.

这是我尝试过的...

我认为战争就像一个罐子,所以

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.

我注意到在我的战争中我有一个名为 META-INF 的文件夹,其中包含一个 manifest.mf,所以我添加了一行来声明我的主类,就像我对普通清单一样......

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

这给出了一个 noClassDefFoundError 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,我认为应该没有什么不同.

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

推荐答案

类似于 Richard Detsch,但更容易理解(也适用于包)

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

第 1 步:解开 War 文件.

Step 1: Unwrap the War file.

jar -xvf MyWar.war

第2步:进入目录

cd WEB-INF

第 3 步:运行包含所有依赖项的主程序

Step 3: Run your main with all dependendecies

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

这篇关于如何从命令行在 WAR 中运行类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-05 08:12