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

问题描述

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

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.

这就是我的意思试过......

Here's what I tried...

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

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.

推荐答案

你可以做什么(持续集成项目)。
你下载了一个战争,可以在tomcat中部署或使用

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

(因为它有一个嵌入式Jetty引擎,从命令行运行它会导致服务器启动。)无论如何通过查看hudson的清单,我了解到他们将Main类放在归档的根目录中。在您的情况下,您的战争布局应如下所示:

(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:

在root下:


  • mypackage / MyEntryPointClass.class

  • WEB-INF / lib

  • WEB-INF / classes

  • META -INF / MANIFEST.MF

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

清单应包括以下行:

Main-Class: mypackage.MyEntryPointClass

请注意mypackage / MyEntryPointClass.class只能从命令行访问,WEB-INF / classes下的类只能从应用程序服务器访问。

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

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

07-05 08:12