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
86
votes
4 answers

Maven cannot resolve dependency for module in same multi-module project

When running commands such as mvn dependency:build-classpath or mvn exec:java Maven is unable to resolve a dependency of one of my modules on another. [ERROR] Failed to execute goal on project parser-app: Could not resolve dependencies for…
84
votes
26 answers

Java error: Only a type can be imported. XYZ resolves to a package

I get the error: "Only a type can be imported. XYZ resolves to a package." Someone has explained the cause here but I am not sure what I supposed to do to fix this. FYI: I am using Eclipse. I have added the code that does the importing below. The…
Ankur
  • 50,282
  • 110
  • 242
  • 312
83
votes
2 answers

Is it possible to have Ant print out the classpath for a particular target? If so, how?

I'm trying to get a target to build that has quite a long list of and elements in its element (in the build.xml file). I keep getting "package…
Daryl Spitzer
  • 143,156
  • 76
  • 154
  • 173
74
votes
7 answers

Adding files to java classpath at runtime

Is it possible to add a file (not necessarily a jar file) to java classpath at runtime. Specifically, the file already is present in the classpath, what I want is whether I can add a modified copy of this file to the classpath. Thanks,
rdsr
73
votes
4 answers

Java: how to import a jar file from command line

I'm trying to call a class (main method) from command line (Windows) with Java. The class imports other classes (other jars). I always get "class not found exception" from a class that my main program imports. Here's what I tried: Add a CLASSPATH…
mrmuggles
73
votes
4 answers

How to load/reference a file as a File instance from the classpath

I have a file that is in my classpath, e.g. com/path/to/file.txt. I need to load or reference this file as a java.io.File object. The is because I need to access the file using java.io.RandomAccessFile (the file is large, and I need to seek to a…
jake
  • 1,405
  • 3
  • 19
  • 33
73
votes
6 answers

Adding an external directory to Tomcat classpath

I have a folder in my C: drive as C:\app_config\java_app This folder contains some locale specific property files. I have a java class (PrjPropertilesLocator) that loads the property files based on default locale on the startup of the web App.My web…
Rajat
  • 1,095
  • 1
  • 13
  • 20
72
votes
7 answers

NoClassDefFoundError while trying to run my jar with java.exe -jar...what's wrong?

I have an application that I'm trying to wrap into a jar for easier deployment. The application compiles and runs fine (in a Windows cmd window) when run as a set of classes reachable from the CLASSPATH. But when I jar up my classes and try to run…
Ogre Psalm33
  • 21,366
  • 16
  • 74
  • 92
71
votes
11 answers

Caused By: java.lang.NoClassDefFoundError: org/apache/log4j/Logger

I've got an interesting problem in which the org.apache.log4j.Logger class is not found during runtime. I'm trying to get authorized and that is where it's failing: OAuthAuthorizer oauthAuthorizer = new OAuthAuthorizer(OAUTH_CONSUMER_KEY,…
68
votes
1 answer

In Eclipse, what is the difference between modulepath and classpath?

In Eclipse, what is the difference between modulepath and classpath, and which one should I use to add a JAR file in the lib folder? And why does the JRE System Library appear in modulepath?
Invariance
  • 1,027
  • 1
  • 7
  • 15
68
votes
7 answers

In Maven, how output the classpath being used?

For my current purposes I have a Maven project which creates a war file, and I want to see what actual classpath it is using when creating the war. Is there a way to do that in a single command -- without having to compile the entire project? One…
Alexander Bird
  • 38,679
  • 42
  • 124
  • 159
67
votes
4 answers

Classpath does not work under linux

Anyone have an idea why this command works fine in Windows but in Linux I get a ClassNotFoundException game.ui.Main java -cp ".;lib/*" game.ui.Main -Xms64m -Xmx128m my folder structure looks like this: lib/ - Jars game/ - Class files This is the…
sproketboy
  • 8,967
  • 18
  • 65
  • 95
67
votes
9 answers

IntelliJ can't find classpath test resource

I'm having a problem where IntelliJ 13.1.4, when running a unit test, can't find a ServiceLoader file in the src/test/resources directory of my module. Please note before answering that I've done all of the following: The module is a Gradle…
Luis Casillas
  • 29,802
  • 7
  • 49
  • 102
66
votes
4 answers

Using bash, how do you make a classpath out of all files in a directory?

This will be a really simple freebie for a bash guru: Question Using bash, how do you make a classpath out of all files in a directory? Details Given a directory: LIB=/path/to/project/dir/lib that contains nothing but *.jar files such…
gMale
  • 17,147
  • 17
  • 91
  • 116
65
votes
4 answers

Maven - how can I add an arbitrary classpath entry to a jar?

I have an unusual situation where I need to add an arbitrary classpath entry (that points to a jar file) into the manifest of an executable jar. (This is for a Swing desktop application.) The maven-jar-plugin generates the "Class-Path" entry for the…
Ken Liu
  • 22,503
  • 19
  • 75
  • 98