How to add directory to classpath in an application run profile in IntelliJ IDEA?
To add a directory to the classpath in an application run profile in IntelliJ IDEA, follow these steps:
In the Project pane, right-click the directory that you want to add to the classpath and select «Mark Directory as» > «Classes Root».
In the «Run/Debug Configurations» dialog, select the run configuration that you want to modify.
In the «Classpath» tab, click the «Add» button and select «Classes» from the menu.
In the «Classes» dialog, select the directory that you want to add to the classpath and click the «OK» button.
The selected directory will be added to the classpath for the selected run configuration.
Introduction
![]()
Java is one of the most popular programming languages in the world, used to build a wide range of applications. One of the fundamental aspects of Java programming is the concept of the classpath, which plays a crucial role in how Java programs are compiled and run.
Understanding classpath is essential for any Java developer, although many Java devs don’t understand what it is and just use their favourite IDE. In this article, we’ll explore Java classpath, how it works, and what every developer should know about it.
What is Java Classpath?
To simplify, the Java Classpath is just a collection of paths (directories and JAR files) used by the Java Compiler to compile and by Java Virtual Machine (JVM) to look for classes or other resources that are required by a Java program at runtime.
Our compiled classes can be located inside a directory or a JAR file. And accordingly, to compile/run the java app we need to provide them in the classpath.
PATH vs CLASSPATH
If you are already familiar with such a thing as PATH in Linux you will understand this really easily. The PATH is used by the operating system to find executable files, while CLASSPATH is used by the JVM to find class files needed by a Java program.
Setting the Classpath
How to set up the Classpath
There are 3 ways you set up the Classpath in Java:
- using command-line arguments
- using environment variables
- using IDEs
Let’s take a look at how to compile and run the following file:
Using the “-cp” or “-classpath” options
To run a Java application using the “-cp” or “-classpath” options, you need to specify the classpath when you invoke the “java” command
Using the “CLASSPATH” environment variable
To run a Java application using the “CLASSPATH” environment variable, you need to set up “CLASSPATH” environment variable before you invoke the “java” command
Using IntelliJ IDEA
If you use IntelliJ IDEA[3] — no need to think much about it because it’s quite easy.
You may modify the classpath in the “Project Structure” settings.
What if our code uses an external library?
Imagine you have added some dependencies (class or method from an external library) in your code.
Then to be able to compile you definitely need that class available in the classpath during compilation and during the runtime. In this case, we will provide the path to JARs (with class and method dependencies) and to our compiled class.
Please note: Sometimes you don’t use the same dependencies for compilation and in runtime (for e.g. jar with API interfaces — for compilation, jar with implementation classes — for runtime). And it’s also a good thing to know “What is the difference between compile time and run time dependencies in Java”[4]
Conclusion
In conclusion, the Java classpath is an essential component of Java development. It specifies where the Java Virtual Machine should look for class files and other resources needed by a Java program.
We have seen that the classpath can be set in various ways, including using environment variables and command-line options.
A misconfigured classpath can result in errors and make it difficult to run Java programs successfully. By understanding the classpath and how to configure it correctly, developers can avoid these issues and ensure that their Java programs run smoothly and reliably.
Understanding how the classpath works and how to configure it correctly can save developers a lot of time and frustration.
How to add directory to classpath in an application run profile in IntelliJ IDEA?
If I override by using -cp x:target/classes in the VM settings, I get the following error:
Any idea on how to add a directory to the classpath for my project?
![]()
![]()
8 Answers 8
In Intellij 13, it looks it’s slightly different again. Here are the instructions for Intellij 13:
- click on the Project view or unhide it by clicking on the «1: Project» button on the left border of the window or by pressing Alt + 1
- find your project or sub-module and click on it to highlight it, then press F4, or right click and choose «Open Module Settings» (on IntelliJ 14 it became F12)
- click on the dependencies tab
- Click the «+» button on the right and select «Jars or directories. «
- Find your path and click OK
- In the dialog with «Choose Categories of Selected File», choose Classes (even if it’s properties), press OK and OK again
- You can now run your application and it will have the selected path in the class path
![]()
It appears that IntelliJ 11 has changed the method, and the checked answer no longer works for me. In case anyone else arrives here via a search engine, here’s how I solved it in IntelliJ 11:
- Go to the Project Structure, click on Modules, and click on your Module
- Choose the «Dependencies» tab
- Click the «+» button on the right-hand side and select «Jars or directories. «
- Add the directory(ies) you want (note you can multi-select) and click OK
- In the dialog that comes up, select «classes» and NOT «jar directory»
- Make sure you’re using that Module in your run target
Note that step 5 seems to be the key difference. If you select «jar directory» it will look exactly the same in the IDE but won’t include the path at runtime. There appears to be no way to determine whether you’ve previously selected «classes» or «jar directory» after the fact.