什么是JAR文件的正确内部结构 [英] what is correct internal structure of JAR file
问题描述
这是一个非常愚蠢的问题,我不能罚款一difinitive答案。
This is a really silly question that I can't fine a difinitive answer to.
背景。
我使用Eclipse(与ANT插件之一,XP的终端上)。
I'm using Eclipse (with one of the ANT plugins, on an XP terminal).
我刚开始玩蚂蚁,在[罐子]指示我设置我完成的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
META-INF / MANIFEST.MF
MyMainFile.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...
META-INF / MANIFEST.MF
dtvcontrol / DTVControlApp.class
META-INF/MANIFEST.MF dtvcontrol/DTVControlApp.class
(从这里<一个href=\"http://www.$c$cranch.com/t/528312/java/java/standalone-application\">http://www.$c$cranch.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
如果是这样,为什么我得到使用ANT不正确的结构,或者为什么有2个不同的正确的内部结构? (如何specifify主类和类路径每个/控制我所得到的)
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构建)
[属性名=主类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 /箱(包含类文件从日食,或从ANT javac任务,有我把它命名不正确?我应该把它叫做建?)
/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的可能这会造成一个问题?我认为不是,如果我有很好的理解是这样的[主类]定义的东西都是什么。
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'是actualy,我在其他地方使用一个小的视觉误差消息库,并且我有了用于测试的主类的第二个文件。因此它指向的各种库(我需要所有的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?)
我已经在其他地方见过,如果我加载库到我的主类(我明明做在打开的窗口),然后尝试运行的程序将失败,如果我用他们没有找到主类,同样也是如此,在任何'静态最后的成员(我使用错误的洛)加入。
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和我的'包装',我使用(它是一个坏的进口做)其他错误的载荷,但存在的图书馆,他们应该为集在classpath中)。
'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
那么罐子的结构需要
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.
编辑:它一直以来我最后一次这样做蚂蚁一段时间,但我认为你需要的东西是这样的:以反映该包struture源文件结构,比如
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约定在这里,蚂蚁不关心,你可以使用(单击鼠标右键) - >属性 - > Java构建path->来源选项卡在Eclipse中设置源目录为src / main / java目录和目标瞄准/班)。然后蚂蚁,有一个从源代码编译成目标编译目标:
(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>
让蚂蚁编译
后,你会看到类文件中的目标
so that after ant compile
you should see the class file in the target
target/classes/mypackage/MyMainFile.class
然后请有打包这个蚂蚁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>
他说以后蚂蚁编译JAR
你应该有内部的目标文件与myJarFile.jar以及
After saying ant compile jar
you should have a file MyJarFile.jar inside target and
java -jar MyJarFile.jar
应该运行的主要方式。
should run the main method.
这篇关于什么是JAR文件的正确内部结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!