Java 类路径和构建路径有什么区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3529459/
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-08-14 01:34:25  来源:igfitidea点击:

What is the difference between Class Path and Build Path

javaclasspathbuildpath

提问by mehmet6parmak

I'm confused with these two terms.

我对这两个术语感到困惑。

Also what should I do to create a file under the src folder of a Spring MVC Project? When I create using a File object it creates the file inside C:\SpringSourceTool... I guess this is ClassPath right?

另外我应该怎么做才能在Spring MVC项目的src文件夹下创建一个文件?当我使用 File 对象创建时,它会在 C:\SpringSourceTool 中创建文件...我猜这是 ClassPath 对吗?

How can I get the applicationcontextfolder or root of the application whatever?

如何获取applicationcontext应用程序的文件夹或根目录?

采纳答案by Andreas Dolk

The build pathis used for building your application. It contains all of your source files and all Java libraries that are required to compile the application.

构建路径是用于构建应用程序。它包含编译应用程序所需的所有源文件和所有 Java 库。

The classpathis used for executing the application. This includes all java classes and libraries that are needed to run the java application. A Classpath is mandatory, the default path is .which is used if the java virtual machine can't find a user defined path. (CLASSPATHenvironment variable, -cpflag or Class-Path:attribute in a jar manifest)

类路径被用于执行该应用程序。这包括运行 Java 应用程序所需的所有 Java 类和库。Classpath 是必需的,.如果java 虚拟机找不到用户定义的路径,则使用默认路径。(jar 清单中的CLASSPATH环境变量、-cp标志或Class-Path:属性)

回答by pavanlimo

Each Java project has its own build path that specifies all dependencies required to compile the project. Those dependencies may come from other Java projects in the workspace, from Java archive .jar files, or from folders containing .class files.

每个 Java 项目都有自己的构建路径,用于指定编译项目所需的所有依赖项。这些依赖项可能来自工作区中的其他 Java 项目、Java 存档 .jar 文件或包含 .class 文件的文件夹。

In CLASSPATH environment you need to specify only .class files (i.e., jar, zip files – Inside jar, zip files you will find only java classes) i.e. you are helping Java Virtual Machine (JVM) to find Java class files

在 CLASSPATH 环境中,您只需要指定 .class 文件(即 jar、zip 文件——在 jar、zip 文件中,您只能找到 java 类)即您正在帮助 Java 虚拟机 (JVM) 查找 Java 类文件

Also what should i do to create a file under the src folder of a Spring MVC Project? When i create using a File object it creates the file inside C:\SpringSourceTool...

另外我应该怎么做才能在 Spring MVC 项目的 src 文件夹下创建一个文件?当我使用 File 对象创建时,它会在 C:\SpringSourceTool 中创建文件...

This is where the JVM was started, if you want to create the file else where, use relative path from here.

这是 JVM 启动的地方,如果您想在其他地方创建文件,请使用此处的相对路径。

See thisand thisfor more info.

有关更多信息,请参阅

回答by KeatsPeeks

The class path is used at runtime to load compiled classes and resources.

类路径在运行时用于加载已编译的类和资源。

The build path is used at compile time to find the dependencies needed to build your project.

构建路径在编译时用于查找构建项目所需的依赖项。

回答by Stephen C

The classpath is the conventional way to tell the (standard) Java compiler and the Java runtime where to find compiled classes. It is typically a sequence of JAR file names and directory names. The classpath used by the compiler and the runtime system don't have to bethe same, but they typically should be, especially for a small project.

类路径是告诉(标准)Java 编译器和 Java 运行时在哪里可以找到已编译类的常规方法。它通常是 JAR 文件名和目录名的序列。由编译器和运行时系统所使用的类路径并不必须是相同的,但它们通常应该是,尤其是对于一个小项目。

Buildpath is not standard Java terminology. It is the term for the richer way that a typical IDE specifies the relationship between the "projects" that make up an application. The IDE uses this to figure out the classpath and sourcepath for compiling the Java code, and the classpath for running it. The IDE also uses the build path to figure out how to package up your code and its dependencies as (for example) a WAR file.

Buildpath 不是标准的 Java 术语。它是典型 IDE 指定构成应用程序的“项目”之间关系的更丰富方式的术语。IDE 使用它来确定用于编译 Java 代码的类路径和源路径,以及用于运行它的类路径。IDE 还使用构建路径来确定如何将代码及其依赖项打包为(例如)WAR 文件。

For example, an Eclipse build path for a project includes the other projects that it depends on, and lists any additional library JARs that the project contains / relies on. It also lists the packages in the current project that downstream projects can depend on.

例如,项目的 Eclipse 构建路径包括它所依赖的其他项目,并列出该项目包含/依赖的任何其他库 JAR。它还列出了当前项目中下游项目可以依赖的包。

(If you are using Maven for your project, the IDE buildpath mechanism is secondary to the dependencies declared in the POM files. For example, using Eclipse with the m2eclipse, the buildpath is synthesized from the POM files.)

(如果您的项目使用 Maven,则 IDE 构建路径机制次于 POM 文件中声明的依赖项。例如,将 Eclipse 与 m2eclipse 一起使用,构建路径是从 POM 文件合成的。)

回答by IqbalHamid

I would like to add to Andreas_D's answer to explain that the build pathis required by the IDE/compiler to locate external packages and classes used by your code.We sometimes refer to these as 'dependencies'.

我想添加到 Andreas_D 的答案中,以解释IDE/编译器需要构建路径来定位您的代码使用的外部包和类我们有时将这些称为“依赖项”

NB: These external packages may be packaged inside a compressed .jar file or indeed, there may be several jar files packaged inside a 'library'. A library or group of libraries often make up a 'framework'.

注意:这些外部包可能打包在一个压缩的 .jar 文件中,或者实际上,可能有几个 jar 文件打包在一个'library' 中。一个库或一组库通常构成一个“框架”

If your code requires code written by others, you can importthem into your class using the importcommand. However, this command on its own is insufficient as the compiler or IDE needs to know where those classes are located. You specify this in the build path.

如果您的代码需要其他人编写的代码,您可以使用命令它们导入到您的类中import。但是,这个命令本身是不够的,因为编译器或 IDE 需要知道这些类的位置。您在构建路径中指定它。

The classpathon the other hand tells the JVM running your application where to find any dependencies during the actual execution of your code.

classpath另一方面告诉运行应用程序在您的代码的实际执行过程中发现任何依赖关系的JVM。

Also to note:Classpath is for use by the JVM.

还要注意:类路径供 JVM 使用。

Buildpath is for use by the IDE/compiler and is a means to construct the classpath from your development environment. When you configure your buildpath via your IDE, you are also configuring a hidden file in your project called .classpath. This is used to provide the classpath to JVM at deployment.

Buildpath 供 IDE/编译器使用,是一种从开发环境构建类路径的方法。当您通过 IDE 配置构建路径时,您还将在项目中配置一个名为 .classpath 的隐藏文件。这用于在部署时提供 JVM 的类路径。

回答by Tom O.

Classpath(from Wikipedia):

类路径来自维基百科):

Similar to the classic dynamic loading behavior, when executing Java programs, the Java Virtual Machine finds and loads classes lazily (it loads the bytecode of a class only when the class is first used). The classpath tells Java where to look in the filesystem for files defining these classes.

The virtual machine searches for and loads classes in this order:

bootstrap classes: the classes that are fundamental to the Java Platform (comprising the public classes of the Java Class Library, and the private classes that are necessary for this library to be functional).

extension classes: packages that are in the extension directory of the JRE or JDK,

jre/lib/ext/ user-defined packages and libraries

By default only the packages of the JDK standard API and extension packages are accessible without needing to set where to find them. The path for all user-defined packages and libraries must be set in the command-line (or in the Manifest associated with the Jar file containing the classes).

与经典的动态加载行为类似,在执行 Java 程序时,Java 虚拟机会懒惰地查找和加载类(它仅在第一次使用类时加载类的字节码)。类路径告诉 Java 在文件系统中的何处查找定义这些类的文件。

虚拟机按以下顺序搜索和加载类:

bootstrap 类:Java 平台的基础类(包括 Java 类库的公共类,以及该库发挥功能所需的私有类)。

扩展类:JRE或JDK扩展目录下的包,

jre/lib/ext/ 用户定义的包和库

默认情况下,只有 JDK 标准 API 和扩展包的包可以访问,而无需设置在哪里找到它们。所有用户定义的包和库的路径必须在命令行中设置(或在与包含类的 Jar 文件关联的清单中)。

Simply put - while your program is running, the JVM loads classes only as needed. When a class is needed, the JVM will depend on the classpath to it know where to load the bytecode from (i.e.: .classfiles).

简而言之 - 当您的程序运行时,JVM 仅根据需要加载类。当需要一个类时,JVM 将依赖于它的类路径知道从哪里加载字节码(即:.class文件)。

Build path, on the other hand, is typically used by an IDE, such as Eclipse, to know where to look for additional libraries that are required to compile a project's source code. Build path isn't used during runtime.

另一方面,构建路径通常由 IDE(例如 Eclipse)使用,以了解在何处查找编译项目源代码所需的其他库。运行时不使用构建路径。