【问题标题】:Java Project Environment PreparationJava项目环境准备
【发布时间】:2017-01-18 05:02:15
【问题描述】:

我是项目环境设置的新手。下面是我在eclipse中的项目结构

Project Name
    --> .settings
    --> .bin
    --> lib
    --> resources
    --> src
    --> .classpath
    --> .project

我正在尝试将 src 文件夹导出为 jar。

当我导出到 jar 时,上述所有文件夹和文件都在 jar 中创建。但我只需要将 src 文件夹转换为。

此外,当我导出到可执行 jar 时,所有第三方库都导出为 jar 中的类文件。对不对。

导出项目的最佳做法是什么。只有 src 文件夹或所有内容。

我需要使用 jar/runnable jar。我的要求是编写启动/停止bat文件来调用jar并执行java程序。

请给我建议。提前致谢。

【问题讨论】:

    标签: java eclipse setup-project


    【解决方案1】:

    首先,了解这些文件夹的实际作用很重要。以下是其中几个文件的工作原理。

    .settings -> This file records project specific settings and workspace preferences.
    
    .bin      -> folder is usually where the compiled files are copied to.
    
    lib       -> contains external libraries that are used in your project (like Apache Commons)
    
    resources -> the resources like images, text, pdf, audio, video are usually copied here
    
    src       -> the folder where the project's source files are located.
    
    .classpath -> It contains information that the JDT feature needs in order to properly compile the project: the project's source folders, the output folders , and classpath entries.
    
    .project  -> This file is maintained by the core Eclipse platform, and its goal is to describe the project from a generic, plugin-independent Eclipse view. 
    

    所以你可以看到,如果你排除了一些文件,比如 lib、resources、bin 等......你的jar 文件可能会停止工作。您的 jar 文件需要编译后的文件及其依赖项。

    例如:您编译的所有.class 文件都在bin 文件夹中。你的jar 工作是因为这些.class 文件而不是src 中的.java 文件。如果您删除此 bin 文件夹,那么您的 jar 可能会停止工作。

    另外,您的项目可能正在使用其他人提供的一些外部库。像Apache Commonsgoogle/guava 这些通常在lib 文件夹中。所以你也不能删除这个文件夹。

    但是,如果您不再期望使用.java 代码,那么您可以排除由 eclipse 创建的文件来管理这个项目。请参阅this 帖子。

    另见:
    1.What's in an Eclipse .classpath/.project file?
    2.exclude files from jar or war in eclipse

    【讨论】:

    • 创建 jar 的最佳实践是什么,我的意思是 jar / 可运行的 jar。我们可以将第三方库保留在外部并将其设置为正确的类路径,或者我们可以将第三方库保留在主 jar 本身中。我在挣扎。请指教。
    • jar 表示Java Archive。它捆绑了您的软件运行所需的一切。你的文件,你的依赖,你的资源。因此,常规做法是将所有内容都保存在该 jar 中,以便您可以将其传输到客户端计算机并简单地部署该文件。将图书馆保持在外面有什么需要?如果这是您真正想做的事情而不是真的不需要创建 jar,您可以简单地通过客户端机器上的命令行执行它们。
    • 注意到拉曼。如果我将其导出为不可运行的 jar。那么如何编写bat文件 1. jdk [C:\Program Files (x86)\Java\jdk1.7\bin] 2. myapp.jar [D:\app\main\myapp.jar] 请帮帮我。我真的很挣扎。
    • 如果您不希望它为 JAR,那么一旦您获得 JAR 文件,您可以使用任何存档工具(如 winzip、7z)简单地解压缩它。现在您可以使用命令行简单地执行它们。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-16
    • 1970-01-01
    • 2021-02-03
    • 2011-05-31
    • 2021-04-23
    • 2017-04-02
    相关资源
    最近更新 更多