JAR文件的正确内部结构是什么 [英] what is correct internal structure of JAR file

查看:35
本文介绍了JAR文件的正确内部结构是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

背景.

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

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

我刚刚开始使用 ANT,在 [jar] 指令中我正在设置我完成的 JAR 文件的位置,当我解压缩"文件时,我得到以下内容

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

元信息/清单.MFMyMainFile.class

META-INF/MANIFEST.MF MyMainFile.class

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

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 )

但是当我尝试运行我的文件时,我收到未找到主类"错误?

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

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

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

元信息/清单.MFdtvcontrol/DTVControlApp.class

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

(从这里 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

元信息/清单.MFMyPackage/MyMainFile.class

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

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

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)

如果您有兴趣,还可以查看 MANIFEST 文件状态(使用 ANT 构建)[属性名称="Main-Class" value="MyPackage.MyMainFile"/]

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

还有正在开发的包的目录结构如下...

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

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

/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 ? )

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

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.

进一步...

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

'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?)

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

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).

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

'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).

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

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

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

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.

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

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

提前致谢...

大卫

推荐答案

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

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

Main-Class: mypackage.MyMainFile

那么jar的结构就需要

then the structure of the jar needs to be

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

其中 MyMainFile 必须位于正确的包中:

where MyMainFile has to be in the proper package:

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

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

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

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

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

src/main/java/mypackage/MyMainFile.java

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

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

target

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

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

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

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

so that after ant compile you should see the class file in the target

target/classes/mypackage/MyMainFile.class

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

Then have a ant jar task that packages this:

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

说完ant compile jar后,你应该在target里面有一个MyJarFile.jar文件,

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

java -jar MyJarFile.jar

应该运行main方法.

should run the main method.

这篇关于JAR文件的正确内部结构是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆