无法将Java项目转换为jar文件 [英] Trouble turning java project into jar file

查看:74
本文介绍了无法将Java项目转换为jar文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个项目,该项目包含一些Java文件,一个包含一些图像的images文件夹和一个包含一些文本文件的data文件夹.当我编译程序并通过终端运行它时,一切都可以正常运行.我决定尝试将其转换为jar文件,因此经过大量研究,我设法做到了,包括使用清单文件.这是我在终端输入的内容:

I've made a project consisting of some java files, an images folder with a few images and a data folder with a few text files. Everything works perfectly when I compile the program and run it through terminal. I decided to try and turn it into a jar file, so after lots of research I managed to do it, including using a manifest file. Here is what I type into terminal to do it:

jar cvfm Program.jar manifest.txt *.class images data  

提供此输出:

added manifest  
adding: a.class (in = 500) (out = 500) (deflated 50%)  
adding: b.class (in = 500) (out = 500) (deflated 50%)  
adding: images/ (in = 0) (out = 0) (stored 0%)  
adding: images/a.png (in = 500) (out = 500) (deflated 50%)  
adding: images/b.png (in = 500) (out = 500) (deflated 50%)  
adding: data/ (in = 0) (out = 0) (stored 0%)  
adding: data/a.txt (in = 500) (out = 500) (deflated 50%)  

其中唯一突出的行是图像,数据和数据,它们的进出存储为= 0.

The only lines of which stand out being the images/ and data/ which have in, out, stored =0.

这有效,它在jar文件中正确列出了所有文件. jar文件甚至在程序正确的目录中时也可以完美运行,但是,如果我从该目录中将其删除,它将无法再找到任何资源文件(图像和数据),我也不知道为什么.这可能是我在代码中引用它们或资源文件未正确附加的一种方式,但是我不确定.我的清单文件包括:

This works and it lists all of the files correctly within the jar file. The jar file even runs perfectly when in the correct directory of the program, however if I remove it from that directory, it no longer can find any of the resource files (images and data) and I have no idea why. It may be a way that I am referencing them in my code or that the resource files aren't attached properly, but I'm not really sure. My manifest file consists of:

Manifest-Version: 1.0  
Main-Class: Strike

任何帮助将不胜感激.

推荐答案

您遇到类路径问题.实际上,当您在项目目录中启动JAR时,实际上是从文件系统而不是从存档中读取资源.

You have a classpath problem. When you launch the JAR in your project directory, in fact you are reading resources from the filesystem, not from the archive.

要从类路径中读取资源,请使用

To read resources from the classpath use Class.getResourceAsStream(String)

InputStream in = getClass().getResourceAsStream("/images/foo.png");

并将foo.png放在最终存档中的images目录中,就像您已经做过的一样.注意/images/foo.png中的斜杠.引用文档

And put foo.png inside an images directory in the final archive like you already do. Note the leading slash in /images/foo.png . Quoting the doc

从给定资源名称构造绝对资源名称 使用此算法:

An absolute resource name is constructed from the given resource name using this algorithm:

  1. 如果名称以'/'开头('\ u002f'),则绝对名称为 资源是名称中'/'之后的部分.

  1. If the name begins with a '/' ('\u002f'), then the absolute name of the resource is the portion of the name following the '/'.

否则, 绝对名称的格式如下:modified_package_name/name 其中modified_package_name是此对象的程序包名称 用"/"代替." ('\ u002e').

Otherwise, the absolute name is of the following form: modified_package_name/name Where the modified_package_name is the package name of this object with '/' substituted for '.' ('\u002e').

如果您不想使用IDE来构建项目,则可以使用 Ant 之类的工具, Maven .

If you don't want to use an IDE to build your project, there are tools like Ant and Maven.

这篇关于无法将Java项目转换为jar文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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