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

发布于 2024-08-26 13:22:51 字数 342 浏览 7 评论 0原文

我的 Java 应用程序具有与此类似的包结构:

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

How would I go about compiling Java files in those paths in to a runnable jar?我需要将所需的库打包到生成的 JAR (jdbc) 中。

我一直在 Eclipse 中完成这些事情,但现在我需要为一些人提供一种无需使用 Eclipse 即可编译存储库的方法,并且我正在考虑制作一个 makefile 或一个调用必要 javac 模式的脚本。

My Java application has got a package structure similar to this:

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? I need to package required libraries into the generated 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

醉态萌生 2024-09-02 13:22:51

看看蚂蚁。它是一个相对简单易懂的构建工具,并提供满足您要求的一切。下面是一个可以帮助您入门的快速框架 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>

您需要创建一个清单文件来告诉 java 进程哪个类拥有“main”方法。 这里是开始学习清单的好地方。

作为生成非常混乱的 Ant 构建文件的替代方法,您可以右键单击 Eclipse 项目并选择“Export...”,然后选择“General > Ant Buildfiles”。

不管怎样,这应该能让你开始。遇到问题时,您可以提出更具体的问题。

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:

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

Anyway, that should get you started. You can ask more specific questions as you run into them.

你的他你的她 2024-09-02 13:22:51

首先,考虑使用 Ant 来完成这样的任务。

但由于您要求手动过程,因此您需要首先创建一个 清单文件,如下所示:

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

Class-Path 的内容替换为您的库,将 Main-Class 替换为完整的内容主类的限定名称。

然后,您需要使用以下命令生成实际的 .jar:

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

其中 MANIFEST.MF 是前面提到的清单文件,其余的是 .java 类所在的文件夹。

最后,要运行您的应用程序,您只需执行:java -jar app.jar

First of all, consider using Ant for such a task.

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.

Then, you need to generate the actual .jar, using the following command:

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.

Finally, to run your app, you simply execute: java -jar app.jar.

桃扇骨 2024-09-02 13:22:51

考虑使用 Ant 来执行此操作。 http://ant.apache.org/

Consider using Ant to do this. http://ant.apache.org/

千寻… 2024-09-02 13:22:51

我建议您使用 Apache Ant 来实现构建脚本。

如果正确实现,Ant 很容易使用,并且构建脚本可以在任何可以安装 JDK 的平台上运行。事实上,只需做一点工作,您甚至可以设置您的项目,以便用户甚至不需要下载/安装 Ant。 (提示:将 Ant JAR 文件和包装器脚本添加到您的项目发行版中)

I recommend that you use Apache Ant to implement your build scripts.

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. 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. (Hint: add the Ant JAR files and a wrapper script to your project distro)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文