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

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

问题描述

这是一个非常愚蠢的问题,我无法理解一个不同寻常的答案。



背景。



<我正在使用Eclipse(其中一个ANT插件,在XP终端上)。



我刚刚开始玩ANT,在[jar]指令我正在设置我完成的JAR文件的位置,当我解压缩文件



META-INF / MANIFEST.MF
MyMainFile时,我得到以下内容。 class



这与在内部结构的oracle网站上找到的一致。
(这里是 http://docs.oracle.com/javase /t>

但是,当我尝试运行我的文件时,我得到一个主类没有找到错误? / p>

我已经看到一些其他帖子,人们已经解压缩了JAR文件,最后结束了以下结构...



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



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



所以我应该得到一个结构,我的类文件在反映包的名称的目录中...例如


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



另外如果你有兴趣,在更清晰的文件状态(使用ANT构建)
[attribute name =Main-Classvalue =MyPackage.MyMainFile/]



正在开发的包的目录结构如下。



/ JavaDev / MyTestPackage / src(包含源文件)
// JavaDev / MyTestPackage / bin(包含来自eclipse的类文件, ANT JAVAC的任务,我是否命名错误?应该调用它构建?)



此外,当我创建jar时,我没有命名它的MyTestPackage。 jar'但是简单的'test.jar'可能会导致问题?我假设不如我已经很好地理解,这是[主要]定义的所有东西。



进一步这一切...



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



我已经读过某个地方,如果我将库加载到我的主类(我明显地打开窗口),然后尝试运行该程序将失败在主类没有找到,如果我使用它们,同样也是真的添加任何'静态final'成员(我用于$ / b>

'静态最终'问题...
我试图调整ANT中的类路径,并且我收到其他错误的负载为了连接到Log4J和我使用的'wrapper'(使用它是一个不好的导入),但库存在于应该在类路径中设置的位置。)



我觉得我几乎在那里....但不完全相同...



我正在为小型图书馆项目做这件事创建意图使用MAVAN作为主要的外包,将它们连接在一起...现在,但我只是wan



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



提前感谢...



David

解决方案

 主类:mypackage.MyMainFile 

那么jar的结构需要是

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

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

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

您的错误信息是由MyMainFile在错误的地方引起的。



编辑:因为我最后一次用蚂蚁做了这件事,但是我觉得你需要这样的东西:一个源文件结构,反映了包的结构,比如说

  src / main / java / mypackage / MyMainFile.java 

编辑的类文件,说

 目标

$ (我在这里使用maven约定,ant不在乎,你可以使用eclipse中的(rightclick) - > properties-> Java Build path-> Sources选项卡来设置源代码dir to src / main / java和target to target / classes)。然后在蚂蚁中,有一个从源到目标编译的编译目标:

 < target name =compile> 
< mkdir dir =target / classes/>
< javac srcdir =src / main / javadestdir =target / classes/>
< / target>

所以在 ant编译后,你应该看到目标中的类文件

  target / classes / mypackage / MyMainFile.class 

然后有一个ant jar任务来打包它:

 < target name =jardepends =compile> 
< jar destfile =target / MyJarFile.jarbasedir =target / classes>
< manifest>
< attribute name =Main-Classvalue =mypackage.MyMainFile/>
< / manifest>
< / jar>
< / target>

说出 ant编译jar 目标中的文件MyJarFile.jar和

  java -jar MyJarFile.jar 
/ pre>

应该运行main方法。


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

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

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 ?

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

(from here 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

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)

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

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.

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

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

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.

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

Thanks in advance...

David

解决方案

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

where MyMainFile has to be in the proper package:

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

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

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

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:

<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

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>

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屋!

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