What is the difference between Class Path and Build Path
Asked Answered
M

6

90

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?

Millionaire answered 20/8, 2010 at 8:59 Comment(0)
H
107

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)

Halflight answered 20/8, 2010 at 9:7 Comment(3)
But javac takes a classpath argument. Could one say that this parameter "overloaded" for specifying the build path?Thomsen
No. The classpath argument is a hint to the compiler what he should expect to be available/provided at runtime, since you don't build every class you use yourself (e.g. JFC, Libraries, ...). The buildpath contains both source and/or source/compiled dependencies and where to look for them.Thrall
While developing a plugin I added all needed libraries to the classpath, but couldn't import nothing. Eclipse did not see them till I added them to build path.Fimbriate
D
55

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

Demented answered 20/8, 2010 at 11:8 Comment(0)
C
29

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.

Commines answered 20/8, 2010 at 9:3 Comment(2)
Could you say that the class path is used to locate .class files (and other compiled resources), while the build path is used to locate source files that need to be compiled?Frication
@Frication I don't think thats completely true. The build path needs .class files to compile source files sometimes. So the build path uses the class path behind the scenes as well. If you take some time to compile and run a java project using the java CLI you can see this. The javac and the java command both have a classpath option because they both may need it.Cogitative
M
8

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.

Miserere answered 20/3, 2018 at 13:32 Comment(0)
P
4

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.

Pegboard answered 20/8, 2010 at 9:1 Comment(1)
You didn't even mention build path in your answer.Thomsen
R
2

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.

Raspberry answered 27/12, 2019 at 16:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.