Questions tagged [classpath]

In Java, the classpath tells the Java Virtual Machine where to look for user-defined classes and packages when running Java programs. The classpath is a parameter and can be set either on the command-line, or through an environment variable.

When executing Java programs, the JVM uses a method that is similar to classic dynamic-loading. That is, the JVM lazily finds and loads classes (meaning, the JVM loads a class only when it is first used). The classpath tells the JVM where to look for the files that define these classes, on the filesystem.

The JVM searches for and loads classes in the following order:

  • Bootstrap classes: These are fundamental classes belonging to the Java platform. These are public classes that belong to the Java Class Library, and it also includes the private classes that are necessary for this library to function properly.
  • Extension classes: These are packages that are in the extension directory of either the JRE or the JDK (usually in jre/lib/ext/).
  • User-defined packages and libraries: These are packages and libraries that are created by the user or that are being used by the user. These can include third-party libraries.

Packages from the JDK standard API and extension classes are accessible by default (the classpath doesn't need to be explicitly set to provide access to them). However, the path for all user-defined packages and libraries must be set. This can be done either via the command-line, an environment variable, or in the manifest file associated with the JAR file that contains the classes.

4839 questions
1169
votes
25 answers

Including all the jars in a directory within the Java classpath

Is there a way to include all the jar files within a directory in the classpath? I'm trying java -classpath lib/*.jar:. my.package.Program and it is not able to find class files that are certainly in those jars. Do I need to add each jar file to…
Chris Serra
  • 13,226
  • 3
  • 25
  • 25
491
votes
10 answers

What is a classpath and how do I set it?

I was just reading this line: The first thing the format() method does is load a Velocity template from the classpath named output.vm Please explain what was meant by classpath in this context, and how I should set the classpath.
Blankman
  • 259,732
  • 324
  • 769
  • 1,199
413
votes
17 answers

How to read text file from classpath in Java?

I am trying to read a text file which is set in CLASSPATH system variable. Not a user variable. I am trying to get input stream to the file as below: Place the directory of file (D:\myDir) in CLASSPATH and try below: InputStream in =…
Chaitanya MSV
  • 6,706
  • 12
  • 40
  • 46
387
votes
15 answers

What causes and what are the differences between NoClassDefFoundError and ClassNotFoundException?

What is the difference between NoClassDefFoundError and ClassNotFoundException? What causes them to be thrown? How can they be resolved? I often encounter these throwables when modifying existing code to include new jar files. I have hit them on…
krisp
  • 4,727
  • 5
  • 25
  • 26
326
votes
16 answers

Get a list of resources from classpath directory

I am looking for a way to get a list of all resource names from a given classpath directory, something like a method List getResourceNames (String directoryName). For example, given a classpath directory x/y/z containing files a.html,…
Marat Salikhov
  • 6,367
  • 4
  • 32
  • 35
306
votes
9 answers

Purpose of buildscript block in Gradle

I am new to Gradle and I am reading the documentation but I don't understand some parts of it. One of these parts is connected with buildscript block. What is its purpose? If your build script needs to use external libraries, you can add them to…
Xelian
  • 16,680
  • 25
  • 99
  • 152
243
votes
8 answers

How to add directory to classpath in an application run profile in IntelliJ IDEA?

I'm trying to add a directory to the classpath of an application run profile If I override by using -cp x:target/classes in the VM settings, I get the following error: java.lang.NoClassDefFoundError:…
sal
  • 23,373
  • 15
  • 66
  • 85
224
votes
32 answers

Intellij Cannot resolve symbol on import

This problem happens intermittently for different libraries and different projects. When trying to import a library, the package will be recognized, but the class name can't be resolved. If on the import statement, I right-click -> Goto -> the…
a5af
  • 2,514
  • 2
  • 14
  • 13
215
votes
12 answers

Find where java class is loaded from

Does anyone know how to programmaticly find out where the java classloader actually loads the class from? I often work on large projects where the classpath gets very long and manual searching is not really an option. I recently had a problem…
luke
  • 14,518
  • 4
  • 46
  • 57
182
votes
15 answers

How to read a text file from resources in Kotlin?

I want to write a Spek test in Kotlin. How to read an HTML file from the src/test/resources folder? class MySpec : Spek( { describe("blah blah") { given("blah blah") { var fileContent: String = "" …
Olaf
  • 3,786
  • 4
  • 25
  • 38
163
votes
13 answers

File inside jar is not visible for spring

All I created a jar file with the following MANIFEST.MF inside: Manifest-Version: 1.0 Ant-Version: Apache Ant 1.8.3 Created-By: 1.6.0_25-b06 (Sun Microsystems Inc.) Main-Class: my.Main Class-Path: . lib/spring-core-3.2.0.M2.jar…
BTakacs
  • 2,397
  • 3
  • 24
  • 26
161
votes
29 answers

How do I resolve ClassNotFoundException?

I am trying to run a Java application, but getting this error: java.lang.ClassNotFoundException: After the colon comes the location of the class that is missing. However, I know that that location does not exist since the class is located…
user2426316
  • 7,131
  • 20
  • 52
  • 83
158
votes
14 answers

IDEA 10.5 Command line is too long

In Maven project when I run test case (on Windows): Error running TestApp.readParameter: Command line is too long. In order to reduce its length classpath file can be used. Would you like to enable classpath file mode for all run configurations of…
qinmiao
  • 5,559
  • 5
  • 36
  • 39
158
votes
4 answers

Spring classpath prefix difference

Documented at 4.7.2.2 The classpath*: prefix it states This special prefix specifies that all classpath resources that match the given name must be obtained (internally, this essentially happens via a ClassLoader.getResources(...) call), and then…
JavaRocky
  • 19,203
  • 31
  • 89
  • 110
153
votes
5 answers

Run a JAR file from the command line and specify classpath

I've compiled a JAR file and specified the Main-Class in the manifest (I used the Eclipse Export function). My dependencies are all in a directory labeled lib. I can't seem to get a straight answer on how to execute my JAR file while specifying it…
SnakeDoc
  • 13,611
  • 17
  • 65
  • 97
1
2 3
99 100