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
153
votes
6 answers

Differences between "java -cp" and "java -jar"?

What is the difference between running a Java application withjava -cp CLASSPATH and java -jar JAR_FILE_PATH? Is one of them preferred to the other for running a Java application? I mean which one of these ways is more expensive for JVM (according…
Reza
  • 2,058
  • 3
  • 20
  • 33
145
votes
15 answers

Classpath including JAR within a JAR

Is it possible to specify a Java classpath that includes a JAR file contained within another JAR file?
Paul Reiners
  • 8,576
  • 33
  • 117
  • 202
140
votes
7 answers

IntelliJ gives Fatal Error: Unable to find package java.lang in classpath or bootclasspath

When I try to make a simple module in IntelliJ it responds with: Fatal Error: Unable to find package java.lang in classpath or bootclasspath
Brig
  • 10,211
  • 12
  • 47
  • 71
138
votes
14 answers

Unbound classpath container in Eclipse

I have checked out a project using Subversive for Eclipse and I am getting the following errors: The project cannot be built until build path errors are resolved Unbound classpath container: 'JRE System Library[jdk1.5.0_08]' I'm assuming the first…
Jessica
  • 1,693
  • 5
  • 13
  • 9
137
votes
11 answers

Add a properties file to IntelliJ's classpath

I'm running a simple Java program from the IntelliJ IDE using the Run->Run menu. It works fine. Now I want to add log4j logging. I added a resources folder under my project root. I added a log4j.properties file in that folder. I changed the code to…
Tony Ennis
  • 12,000
  • 7
  • 52
  • 73
135
votes
5 answers

Java, Classpath, Classloading => Multiple Versions of the same jar/project

I know this may be a silly question for experienced coders. But I have a library (an http client) that some of the other frameworks/jars used in my project require. But all of them require different major versions like: httpclient-v1.jar => Required…
jens
  • 2,319
  • 4
  • 18
  • 11
132
votes
62 answers

Eclipse "Error: Could not find or load main class"

I have a project in eclipse on my laptop that I pushed to Git https://github.com/chrisbramm/LastFM-History-Graph.git It works fully on my laptop and runs/builds without a problem but on my desktop it doesn't Eclipse gives the error Error: Could not…
Chris
  • 1,435
  • 2
  • 11
  • 9
126
votes
7 answers

Compile time vs Run time Dependency - Java

What is the difference between compile time and run time dependencies in Java? It is related to class path, but how do they differ?
Kunal
  • 2,929
  • 6
  • 21
  • 23
114
votes
4 answers

Call "java -jar MyFile.jar" with additional classpath option

I created a jar file containing all my compiled stuff. Additionally my ant build script copies the required libs into a subfolder "libs". The structure looks like this: MyProgram.jar libs/ So when I try to run my program now I get the following…
toom
  • 12,864
  • 27
  • 89
  • 128
111
votes
9 answers

List of dependency jar files in Maven

Using Maven 2, is there a way I can list out the jar dependencies as just the file names? mvn dependency:build-classpath can list the jar files, but that will include the full path to their location in my local repository. What I need is…
Sindri Traustason
  • 5,445
  • 6
  • 48
  • 66
110
votes
4 answers

How to use a wildcard in the classpath to add multiple jars?

I have been using so many 3rd party libraries(jar files) that my CLASSPATH is completely messed up as i have to include the path for every single jar file that i use. I've been wondering if there is a way to include all the jar files in a folder…
paulbullard
  • 1,187
  • 2
  • 8
  • 9
94
votes
5 answers

How to use ClassLoader.getResources() correctly?

How can I use ClassLoader.getResources() to find recursivly resources from my classpath? E.g. finding all resources in the META-INF "directory": Imagine something like getClass().getClassLoader().getResources("META-INF") Unfortunately, this does…
MRalwasser
  • 15,605
  • 15
  • 101
  • 147
90
votes
11 answers

how to make jni.h be found?

In Ubuntu 12.04, I have jdk7 from sun/oracle installed. When locate jni.h, it prints multiple locations /usr/lib/jvm/java-6-openjdk-amd64/include/jni.h /usr/lib/jvm/jdk1.7.0_07/include/jni.h ... In the header file generated by JDK, there is…
Richard
  • 14,642
  • 18
  • 56
  • 77
89
votes
6 answers

What is the difference between Class Path and Build Path

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…
mehmet6parmak
  • 4,777
  • 16
  • 50
  • 71
87
votes
5 answers

Adding to the classpath on OSX

Can anyone tell me how to add to the classpath on OSX?
Mike2012
  • 7,629
  • 15
  • 84
  • 135