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

发布于 2024-09-15 14:31:48 字数 194 浏览 8 评论 0原文

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

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

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

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?

How can I get the applicationcontext folder or root of the application whatever?

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

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

发布评论

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

评论(6

萌梦深 2024-09-22 14:31:48

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

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

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

The classpath is 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. (CLASSPATH environment variable, -cp flag or Class-Path: attribute in a jar manifest)

喵星人汪星人 2024-09-22 14:31:48

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

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

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

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

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 be the same, but they typically should be, especially for a small project.

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.

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.

(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.)

止于盛夏 2024-09-22 14:31:48

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

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

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.

怀中猫帐中妖 2024-09-22 14:31:48

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

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

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

另一方面,classpath 告诉运行应用程序的 JVM 在实际执行代码期间到哪里查找任何依赖项。

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

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

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

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

If your code requires code written by others, you can import them into your class using the import command. 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.

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

Also to note:
Classpath is for use by the 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.

零度° 2024-09-22 14:31:48

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

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

另外我应该做什么来创建文件
在 Spring MVC 的 src 文件夹下
项目?当我使用文件创建时
它在里面创建文件的对象
C:\SpringSourceTool...

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

请参阅了解更多信息。

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.

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

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

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

See this and this for more info.

电影里的梦 2024-09-22 14:31:48

类路径来自维基百科):

与经典的动态加载行为类似,执行Java时
程序中,Java 虚拟机会延迟查找并加载类(它
仅当首次使用该类时才加载该类的字节码)。这
类路径告诉 Java 在文件系统中的何处查找文件
定义这些类。

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

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

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

jre/lib/ext/ 用户定义的包和
图书馆

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

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

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

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

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.: .class files).

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.

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