如何使用多个包或源文件编译和运行 Java 代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15402058/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 19:27:28  来源:igfitidea点击:

How to compile and run Java code with multiple packages or source files

javabuildcompilation

提问by Tesla

I've been teaching myself Java, but I have only used Eclipse as a method of compiling and running my project, and would like to see how I would compile and run the following file structure using only the command line (Don't want to simply export a .jarfile from eclipse).

我一直在自学 Java,但我只使用 Eclipse 作为编译和运行我的项目的方法,并且想看看我将如何仅使用命令行编译和运行以下文件结构(不想只需.jar从 Eclipse导出文件)。

The file structure is as follows:

文件结构如下:

src/
    model/
        file1.java
        file2.java
    app/
        main.java (contains main method)
        file3.java
        images/
            1.gif
            2.gif
    view/
        file4.java
        file5.java

I need the image files compiled in as well, as I used them for a couple of things. I understand the javaccommand should be used to build the .classfiles, but I am confused as to who I would compile it (and run) with this file structure (and with the image files). Any help would be appreciated.

我还需要编译的图像文件,因为我将它们用于几件事。我知道javac应该使用该命令来构建.class文件,但我对使用此文件结构(和图像文件)编译(并运行)它的人感到困惑。任何帮助,将不胜感激。

Note: I am attempting to compile and run on Ubuntu 12.04 and am strictly trying to use command line.

注意:我正在尝试在 Ubuntu 12.04 上编译和运行,并严格尝试使用命令行。

回答by Abimaran Kugathasan

Try the following

尝试以下

javac -classpath ./lib/*.jar ./src/*/*.java

Give a proper jar library path, if your java classes use a 3d party libraries. It's better to use a build tool with proper directory structure.

如果您的 java 类使用 3d 方库,请提供正确的 jar 库路径。最好使用具有适当目录结构的构建工具。

回答by Nathaniel Waisbrot

You could say

你可以说

javac src/*/*.java

But that will unconditionally recompile, will be annoying with more files, and will break when you have more files than the command line can deal with.

但这将无条件地重新编译,会因更多文件而烦恼,并且当您的文件多于命令行可以处理时会中断。

The sensible thing is to use Antor Maven(Maven is easier/better in most cases). You write a build script (build.xmlor pom.xmlrespectively) and run the builder (antor mvn).

明智的做法是使用AntMaven(在大多数情况下,Maven 更容易/更好)。您编写构建脚本(build.xmlpom.xml)并运行构建器(antmvn)。

回答by picomancer

Once upon a time, I compiled Java projects with Windows batch files. I remember having to go back and laboriously edit them whenever I added a new package to the project.

曾几何时,我用 Windows 批处理文件编译 Java 项目。我记得每当我向项目添加一个新包时,都必须返回并费力地编辑它们。

With the power of the Linux shell, you can actually make a decent command-line build script without having to do this:

借助 Linux shell 的强大功能,您实际上可以制作一个不错的命令行构建脚本,而无需执行以下操作:

#!/bin/bash

set -e          # quit on error
set -u          # quit if we attempt to use undefined environment variable
set -x          # show commands as they're executed (remove this line if done debugging)

# Run this script from the project root directory

cd src

# Create directory structure
find . -type d | xargs -n 1 -I I --verbose mkdir -p ../dist/class/I

# Use javac to compile files
find . -name "*.java" | javac @/dev/stdin -d ../dist/class

# Copy assets
find . -type f -and -not -name "*.java" | xargs -n 1 -I I --verbose cp I ../dist/class/I

# Jar command
jar cvf ../dist/$(basename $(readlink -f ..)) -C ../dist/class .

You should save this in a file called "build.sh" in your project's root directory. This script is missing a few features. There's lots of verbosity so you can see exactly what's going on. It doesn't handle filenames with spaces or other strange characters very well. If you want a runnable jar file, you have to write a manifest file and include it in the "jar" command. If your project uses any external dependencies outside the Java class libraries, you'll have to add a -cp switch to the javac command line, or export the CLASSPATH environment variables.

您应该将其保存在项目根目录中名为“build.sh”的文件中。此脚本缺少一些功能。有很多详细信息,因此您可以确切地看到正在发生的事情。它不能很好地处理带有空格或其他奇怪字符的文件名。如果您想要一个可运行的 jar 文件,您必须编写一个清单文件并将其包含在“jar”命令中。如果您的项目使用 Java 类库之外的任何外部依赖项,则必须向 javac 命令行添加 -cp 开关,或导出 CLASSPATH 环境变量。

The script above will copy the assets into the jar file. This makes a one-file distribution, assuming you extract the assets at runtime. Stackoverflow can tell you more about How to Handle Assets in Single File within a Virtual File System in Java.

上面的脚本会将资产复制到 jar 文件中。假设您在运行时提取资产,这将形成一个文件分发。Stackoverflow 可以告诉你更多关于如何在 Java 中处理虚拟文件系统内单个文件中的资产

As an alternative, you can simply copy the assets from the source directory to the target directory.

作为替代方案,您可以简单地将资产从源目录复制到目标目录。

As a matter of style, most people keep their assets in a separate directory from source code. E.g. your two .gif files would be under assets/app/images instead of src/app/images. It's not technically necessary to do it this way as long as you can convince your build system to do what you want, but most of the projects I've seen separate them out.

就风格而言,大多数人将他们的资产保存在与源代码不同的目录中。例如,您的两个 .gif 文件将位于 assets/app/images 而不是 src/app/images。只要你能说服你的构建系统做你想做的事情,技术上就没有必要这样做,但我见过的大多数项目都将它们分开。