eclipse JAR文件的正确内部结构是什么

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

what is correct internal structure of JAR file

javaeclipseantmanifest.mf

提问by DaveM

This is a really silly question that I can't fine a difinitive answer to.

这是一个非常愚蠢的问题,我无法给出明确的答案。

Background.

背景。

I'm using Eclipse (with one of the ANT plugins, on an XP terminal).

我正在使用 Eclipse(在 XP 终端上使用 ANT 插件之一)。

I have just started playing with ANT, in the [jar] directive I am setting the location of my finished JAR file and I get the following when I 'unzip' the file

我刚刚开始玩 ANT,在 [jar] 指令中我正在设置我完成的 JAR 文件的位置,当我“解压缩”文件时,我得到以下信息

META-INF/MANIFEST.MF MyMainFile.class

META-INF/MANIFEST.MF MyMainFile.class

which is consistent with that found on the oracle web site for the internal structure. (here http://docs.oracle.com/javase/tutorial/deployment/jar/view.html)

这与在oracle网站上找到的内部结构一致。(这里http://docs.oracle.com/javase/tutorial/deployment/jar/view.html

But when I try to run my file I get a 'main class not found' error ?

但是当我尝试运行我的文件时,我收到一个“找不到主类”的错误?

I have seen some other posts where people have 'unzipped' the JAR file and ended up with a structure of the following...

我看过其他一些帖子,其中人们“解压缩”了 JAR 文件并最终得到了以下结构...

META-INF/MANIFEST.MF dtvcontrol/DTVControlApp.class

META-INF/MANIFEST.MF dtvcontrol/DTVControlApp.class

(from here http://www.coderanch.com/t/528312/java/java/standalone-application)

(从这里http://www.coderanch.com/t/528312/java/java/standalone-application

So should I get a structure where my class files are in a directory that reflects the name of the package... eg

那么我应该得到一个结构,其中我的类文件位于反映包名称的目录中......例如

META-INF/MANIFEST.MF MyPackage/MyMainFile.class

META-INF/MANIFEST.MF MyPackage/MyMainFile.class

and if so, why am I getting the incorrect structure using ANT, or why are there 2 different 'correct' internal structures? (how to specifify main-class and classpath for each / control what I get)

如果是这样,为什么我使用 ANT 得到不正确的结构,或者为什么有 2 个不同的“正确”内部结构?(如何为每个指定主类和类路径/控制我得到的)

Also in case you are interested, in the MANIFEST file states (build using ANT) [attribute name="Main-Class" value="MyPackage.MyMainFile"/]

另外,如果您有兴趣,在 MANIFEST 文件状态(使用 ANT 构建)[attribute name="Main-Class" value="MyPackage.MyMainFile"/]

Also the directory structure of the package under development is as follows...

另外正在开发的包的目录结构如下...

/JavaDev/MyTestPackage/src (contains the source files) //JavaDev/MyTestPackage/bin (contains the class files from eclipse, or from the ANT JAVAC task, have I named it incorrectly? should I have called it build ? )

/JavaDev/MyTestPackage/src(包含源文件) //JavaDev/MyTestPackage/bin(包含来自 eclipse 或来自 ANT JAVAC 任务的类文件,我是否将其命名错误?我应该将其称为 build 吗?)

Further to this, when I create the jar I am not naming it 'MyTestPackage.jar' but simply 'test.jar' could this be causing a problem? I assume not as if I have well understood that is what the [main-class] definition stuff is all about.

此外,当我创建 jar 时,我没有将其命名为“MyTestPackage.jar”,而只是将其命名为“test.jar”,这会导致问题吗?我认为我并没有很好地理解这就是 [main-class] 定义的全部内容。

Further to all this...

除此之外……

'MyTestPackage' is actualy a small visual error messaging library that I use elsewhere, and I have a second file that has a main class to use for testing. As such it points to various libraries (do I need to copy all the SWT libraries to a specified directory?)

'MyTestPackage' 实际上是我在别处使用的一个小的可视化错误消息库,我有第二个文件,它有一个用于测试的主类。因此,它指向各种库(我是否需要将所有 SWT 库复制到指定目录?)

I have read somewhere that if I load libraries into my main class (which I obviously do to open the window) then trying to run the program will fail on a 'main class not found' if I use them, same is also true for adding in any 'static final' members (which I use for the loggin of errors).

我在某处读到,如果我将库加载到我的主类中(我显然这样做是为了打开窗口)然后尝试运行该程序将在“未找到主类”上失败,如果我使用它们,添加也是如此在任何“静态最终”成员中(我用于记录错误)。

'Static Final' problem... I tried to adjust the classpath in ANT, and I get a load of other errors for the connection to Log4J and my 'wrapper' that I use (to do with it being a bad import), but the libraries exist where they should as set in the classpath).

“静态最终”问题......我试图调整 ANT 中的类路径,但我收到了大量其他错误,用于连接到 Log4J 和我使用的“包装器”(因为它是一个错误的导入),但是库存在于它们应该在类路径中设置的位置)。

I feel like I am almost there.... but not quite...

我觉得我快到了……但不完全……

I'm doing this for the small 'library projects' that I am creating with the intention of using MAVAN for the main outer package that will connect them all together... for now however I just want to get this going so as it works.

我正在为我正在创建的小型“库项目”执行此操作,目的是将 MAVAN 用于将它们连接在一起的主要外部包......但是现在我只想让它继续工作.

I can supply the full source, or any other info as required.

我可以根据需要提供完整的来源或任何其他信息。

Thanks in advance...

提前致谢...

David

大卫

回答by wallenborn

It's simple when you know where to look. Say your META-INF/MANIFEST.MF contains the line:

当您知道去哪里寻找时,这很简单。假设您的 META-INF/MANIFEST.MF 包含以下行:

Main-Class: mypackage.MyMainFile

then the structure of the jar needs to be

那么罐子的结构需要是

META-INF/MANIFEST.MF
mypackage/MyMainFile.class

where MyMainFile has to be in the proper package:

其中 MyMainFile 必须在正确的包中:

package mypackage;
public class MyMainFile {
  public static void main(String[] args) {
...

Your error message is caused by MyMainFile being in the wrong place.

您的错误消息是由 MyMainFile 位于错误的位置引起的。

Edit:it's been a while since the last time i did that with ant, but i think you need something like this: a source file structure that reflects the package struture, say

编辑:自从我上次用 ant 做这件事已经有一段时间了,但我认为你需要这样的东西:一个反映包结构的源文件结构,说

src/main/java/mypackage/MyMainFile.java

and a directory to put the compiled class file into, say

和一个将编译后的类文件放入的目录,比如说

target

(I'm using maven conventions here, ant doesn't care and you can use the (rightclick)->properties->Java Build path->Sources tab in eclipse to set the source dir to src/main/java and the target to target/classes). Then in ant, have a compile target that compiles from source to target:

(我在这里使用 maven 约定,ant 不在乎,您可以使用 eclipse 中的 (rightclick)->properties->Java Build path->Sources 选项卡将源目录设置为 src/main/java 和目标到目标/类)。然后在 ant 中,有一个从源编译到目标的编译目标:

<target name="compile">
    <mkdir dir="target/classes"/>
    <javac srcdir="src/main/java" destdir="target/classes"/>
</target> 

so that after ant compileyou should see the class file in the target

以便在ant compile您应该看到目标中的类文件之后

target/classes/mypackage/MyMainFile.class

Then have a ant jar task that packages this:

然后有一个打包这个的 ant jar 任务:

<target name="jar" depends="compile">
    <jar destfile="target/MyJarFile.jar" basedir="target/classes">
        <manifest>
            <attribute name="Main-Class" value="mypackage.MyMainFile"/>
        </manifest>
    </jar>
</target>

After saying ant compile jaryou should have a file MyJarFile.jar inside target and

在说ant compile jar你应该在目标内部有一个文件 MyJarFile.jar 之后

java -jar MyJarFile.jar

should run the main method.

应该运行 main 方法。