简体   繁体   English

用多个类构建Jar文件

[英]Build Jar File with multiple classes

Is there a way to build a jar file that is anything inside a specific folder? 有没有一种方法可以构建特定文件夹中包含任何内容的jar文件?

Inside my folder I have my main class named "GamesCombined.java" with it's .class files 在我的文件夹中,我的主类名为.GamesCombined.java。

Here's what my directory looks like inside GamesCombined folder 这是我的目录在GamesCombined文件夹中的样子

-> GamesCombined.java
-> GamesCombined.class //classes*
-> games (folder)
|
|----> pacman (folder)
|   |
|   |----> PacmanGame.java
|   |----> PacmanGame.class //classes*
|
|----> racinggame(folder)
     |
     |----> RacingGame.java
     |----> RacingGame.class //classes*
-> images (folder)
|
|----> pacman (folder)
|    |
|    |----> //jpg's and png's
|
|----> racinggame (folder)
     |
     |----> //jpg's and png's

I already made a jar file of the GamesCombined.java by following this link: http://www.skylit.com/javamethods/faqs/createjar.html 通过以下链接,我已经制作了GamesCombined.java的jar文件: http : //www.skylit.com/javamethods/faqs/createjar.html

But then it has an error: 但是然后有一个错误:

Exception in thread "main" java.lang.NoClassDefFoundError: games/pacman/PacmanGame (wrong name: PacmanGame) 线程“主要” java.lang.NoClassDefFoundError中的异常:games / pacman / PacmanGame(错误名称:PacmanGame)

GamesCombined.java has import games.pacman.*; GamesCombined.java具有导入的games.pacman。*;

Is there a way where I could just build a jar file including the other classes? 有没有一种方法可以构建包含其他类的jar文件? What should I do? 我该怎么办? :o :o

I'm new when it comes on JAR files. 关于JAR文件,我是新手。 :/ :/

The easiest way is to use the included Jar Builder of your IDE. 最简单的方法是使用IDE附带的Jar生成器。 Java brings a tool which also can create JarFiles, take a look : Creating a JAR File Java带来了一个也可以创建JarFiles的工具,看看: 创建一个JAR文件

(your jdk/bin)jar cf jar-file input-file(s)

In both ways you have to check your classpath, written in your Manifest 两种方式都必须检查清单中写的类路径

EDIT: 编辑:

JCreator Pro is quite easy: JCreator Pro非常简单:

click: Configure>Options>Tools>New>Create JAR File 单击:配置>选项>工具>新建>创建JAR文件

this creates an user tool to create a JAR file, this does not yet include the manifest, you will have to manually add the manifest. 这将创建一个用于创建JAR文件的用户工具,该工具尚未包含清单,您将必须手动添加清单。

OR you can configure the command line switches to incorporate the manifest, try this for example: cvmf $[PrjDir]\\MANIFEST.MF $[PrjName].jar . 或者,您可以配置命令行开关以合并清单,例如,尝试:cvmf $ [PrjDir] \\ MANIFEST.MF $ [PrjName] .jar。

this should include the manifest 'MANIFEST.MF' in your project directory as a manifest. 这应该在清单中包含清单“ MANIFEST.MF”作为清单。 You will have to create that yourself though. 但是,您将必须自己创建它。

NOTE: this has NOT been tested 注意:这个没有经过测试

The NoClassDefFoundError indicates that you have not specified the package inside PacManGame.java (or you didn't recompile it before adding it to the jar). NoClassDefFoundError指示您尚未在PacManGame.java中指定包(或者您没有在将其添加到jar中之前重新编译它)。

package games.pacman;
public class PacManGame {
    ...

To answer the original question, you can create the jar file using 要回答原始问题,可以使用以下命令创建jar文件

jar cvfm gamescombined.jar MANIFEST.MF -C GamesCombined/ . 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM