简体   繁体   English

Java:如何从包中编译可运行的jar?

[英]Java: How to compile a runnable jar from packages?

My Java application has got a package structure similar to this: 我的Java应用程序具有类似于以下的包结构:

src/com/name/app 
src/com/name/app/do 
src/com/name/utils/db

How would I go about compiling Java files in these directories in to a runnable jar? 如何将这些目录中的Java文件编译到可运行的jar中? I need to package required libraries into the generated JAR (jdbc). 我需要将所需的库打包到生成的JAR(jdbc)中。

I've always done these things in Eclipse but now I need to supply a couple of people with a way to compile the repository without the use of eclipse and I was thinking of making a makefile or a script that invokes the necessary javac pattern. 我一直在Eclipse中完成这些操作,但是现在我需要为几个人提供一种无需使用eclipse即可编译存储库的方法,并且我正在考虑制作一个makefile或调用必要的javac模式的脚本。

Take a look at Ant. 看一下蚂蚁。 It's a relatively simple build tool to understand, and provides everything that meets your requirements. 这是一个相对简单的构建工具,易于理解,并提供满足您要求的所有内容。 Here's a quick skeleton build.xml to get you started: 这是一个快速入门build.xml,可以帮助您入门:

<project name="my_app_name" default="jar">

  <target name="compile">
     <javac srcdir="src" destdir="bin">
        <classpath>
           <fileset dir="lib">
              <include name="**/*.jar" />
           </fileset>
        </classpath>
     </javac>
  </target>

  <target name="jar">
     <jar manifest="manifest_file" destfile="dist/my_app_name.jar">
        <fileset dir="bin" />
        <fileset dir="lib" />
     </jar>
  </target>

You need to create a manifest file that will tell the java process which class holds the "main" method. 您需要创建一个清单文件,该文件将告诉java进程哪个类包含“ main”方法。 Here is a good place to start learning about manifests. 是开始学习清单的好地方。

As an alternate that produces really cluttered Ant build files, you can right click on your Eclipse project and choose "Export...", then choose "General > Ant Buildfiles". 作为产生真正混乱的Ant构建文件的替代方法,您可以右键单击Eclipse项目并选择“导出...”,然后选择“常规> Ant构建文件”。

Anyway, that should get you started. 无论如何,那应该让您入门。 You can ask more specific questions as you run into them. 您可以在遇到问题时提出更具体的问题。

First of all, consider using Ant for such a task. 首先,考虑将Ant用于此类任务。

But since you asked for a manual process, you need to first create a manifest file , like so: 但是,由于您要求手动进行操作,因此需要首先创建一个清单文件 ,如下所示:

Manifest-Version: 1.0
Created-By: 1.6.0 (Sun Microsystems Inc.)
Class-Path: lib/jdbc.jar lib/otherlib.jar
Main-Class: com.name.app.MainClass

Replace the contents of Class-Path with your libs, and Main-Class with the fully qualified name of your main class. 用您的库替换Class-Path的内容,并用您的主类的完全限定名替换Main-Class

Then, you need to generate the actual .jar, using the following command: 然后,您需要使用以下命令生成实际的.jar:

jar cfm app.jar MANIFEST.MF src/com/name/app/*.class src/com/name/app/do/*.class

Where MANIFEST.MF is the previously mentioned manifest file, and the rest is the folders where your .java classes lie in. 其中MANIFEST.MF是前面提到的清单文件,其余的是您的.java类所在的文件夹。

Finally, to run your app, you simply execute: java -jar app.jar . 最后,要运行您的应用程序,只需执行以下命令: java -jar app.jar

Consider using Ant to do this. 考虑使用Ant来执行此操作。 http://ant.apache.org/ http://ant.apache.org/

I recommend that you use Apache Ant to implement your build scripts. 我建议您使用Apache Ant来实现构建脚本。

If implemented correctly, Ant is easy to use and the build scripts can be run on any platform that you can install a JDK on. 如果正确实现,则Ant易于使用,并且构建脚本可以在可以安装JDK的任何平台上运行。 Indeed, with a little bit of work, you can even set up your project so that users don't even need to download / install Ant. 确实,只需做一些工作,您甚至可以设置您的项目,以便用户甚至不需要下载/安装Ant。 (Hint: add the Ant JAR files and a wrapper script to your project distro) (提示:将Ant JAR文件和包装脚本添加到项目发行版中)

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

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