[英]Where do you store Java .class files, conventionally?
I have a Java src folder in which I store my .java files. 我有一个Java src文件夹,我在其中存储我的.java文件。 I then compile them using Terminal and end up getting .class files in the same directory. 然后我使用Terminal编译它们并最终在同一目录中获取.class文件。 This doesn't necessarily bother me, but I've never seen that being done professionally. 这不一定会打扰我,但我从未见过这是专业的。
By professional convention (if there exists one), in what folder in a project's directory should you store compiled .class files? 通过专业约定(如果存在),在项目目录中的哪个文件夹中应该存储已编译的.class文件?
If you're not familiar with it, you should look into the subject of the Java classpath . 如果您不熟悉它,则应该查看Java类路径的主题。 I can remember finding this confusing when I first started programming in Java. 我记得在我第一次开始使用Java编程时发现这种混乱。
There is a defined search path for .class files in Java; Java中有.class文件的定义搜索路径; when you run java -cp blahblahblah
that sets the classpath. 当你运行设置类路径的java -cp blahblahblah
。 java -jar blahblahblah.jar
opens a JAR file and the .jar file's manifest may dictate the classpath. java -jar blahblahblah.jar
打开一个JAR文件 ,而.jar文件的清单可能会指定类路径。 The default classpath is in the current directory. 默认的类路径位于当前目录中。
Usually you put Java classes in packages in a hierarchical directory structure in the filesystem, in which case the Java compiler will also put .class
files in a corresponding structure. 通常,您将Java类放在文件系统中的分层目录结构中的包中,在这种情况下,Java编译器也会将.class
文件放在相应的结构中。
If you run javac
from the command line, the -d
argument specifies the destination root directory for .class
files. 如果从命令行运行javac
,则-d
参数指定.class
文件的目标根目录。
A typical project looks like this, assuming your package is called com.example.foo
and it contains Foo.java and Bar.java: 一个典型的项目看起来像这样,假设你的包名为com.example.foo
,它包含Foo.java和Bar.java:
project_dir/
src/
com/
example/
foo/
Foo.java
Bar.java
bin/
com/
example
foo/
Foo.class
Bar.class
The java compiler will be executed with -d bin
as a destination directory, and it will create the .class
files corresponding to the .java
files. Java编译器将执行-d bin
作为目标目录,它会创建.class
对应文件.java
文件。 But the destination doesn't have to be called bin
; 但目的地不必被称为bin
; you could call it Freddy
or dumbass
if you wanted to, although that would probably confuse people used to bin
or build
. 如果你愿意的话,你可以称之为Freddy
或者dumbass
,尽管这可能会让人们习惯于bin
或者build
。
Most of the time when a Java program or library builds, the .class
files are just a temporary step towards building a .jar file (essentially just a .zip format with a .jar
extension, containing the .class files and a little bit of metadata), and when you actually run them, Java will use the .jar file if you include it as part of the classpath. 很多时候一个Java程序或库建立的时候, .class
文件只是朝着建立一个.jar文件(实际上就是用一个.zip格式的临时步骤.jar
扩展,包含.class文件和一点点元数据),当您实际运行它们时,如果将它包含在类路径中,Java将使用.jar文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.