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

8

247

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: com/intellij/rt/execution/application/AppMain

Any idea on how to add a directory to the classpath for my project?

Weywadt answered 12/5, 2009 at 18:51 Comment(2)
It is not clear to me, where you are trying to add that. From the IJ command line? Or in the project properties?Uroscopy
Related: youtrack.jetbrains.net/issue/IDEA-160167Timmytimocracy
M
335

In Intellij 13, it looks it's slightly different again. Here are the instructions for Intellij 13:

  1. 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
  2. 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)
  3. click on the dependencies tab
  4. Click the "+" button on the right and select "Jars or directories..."
  5. Find your path and click OK
  6. In the dialog with "Choose Categories of Selected File", choose Classes (even if it's properties), press OK and OK again
  7. You can now run your application and it will have the selected path in the class path
Meningitis answered 19/7, 2014 at 19:2 Comment(17)
So how would you create to run configurations with two different directories that must be exclusively present in one but not the other. In eclipse you can add/remove anything to a launch configuration. Does IntelliJ not support this?Sextain
It's pretty clear that you can change classpath of run configuration changing module classpath. But exact question was "how to add directory to classpath in an application run profile". So the real was given by uaaquariusPardner
Thank you ! This solved my problem after ages of searching around !Anisotropic
Step 6 does not come up for me. I am on intellij 14. I have a folder called lib where my .jar are located. selecting the folder or jar will just parse the path into intellij. still does not workBoiled
@Boiled Did you figure this out?Dished
I would like to add to the answer that, this is not recursive. So only the files in the selected directory will be added to classpath, not the sub directories. I couldn't find a way to add recursively, so this is the best answer I could find.Improbability
I tried this solution on Itelijj Ultimate 2017.1 and it didn't work for me. I still got the problem "Caused by: java.lang.RuntimeException: The directory /Users/****/Documents/hadoopConf/ is not in the classpath!"Gone
@Gone have you found a solution yet ?? because i am still stuck at the very same problem if i run my java fx jar still failes to find the class!Nephrosis
@kinsleykajiva I have added the classpath into tomcat's conf file (classpath)Gone
I did this but it's still not enough. which distributor for the jbdc lib? there are several : Postgres, Microsoft, oracle, which version ? I tired 6.2 from microsoft and 42 from postgresDecor
@stivlo, Where can I type java -cp directly as in https://mcmap.net/q/118889/-use-jdbc-mysql-connector-in-intellij-idea ?Astereognosis
after you ran the app once you can go near the top left corner and choose edit configuration and add it in the VM optionsMeningitis
Step 1 and 2 can be simplified by just pressing Ctrl + Alt + Shift + SAzotize
I donnot understand why IntelliJ -given the popularity of this answer & question- still has not improved this in the run-dialog, just add an extra input field...Jeffery
@Meningitis I'm using Intellij 2019, and after step 5, it doesn't show dialog on step 6.Woolery
please, I don't see Choose Categories of Selected File anywhere. Intellij IDEA Community 2020.1.1 Runtime v11.0.6. thank youGibrian
Confirmed, this solution works for Intellij Ultimate 2020.1Apiece
D
99

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:

  1. Go to the Project Structure, click on Modules, and click on your Module
  2. Choose the "Dependencies" tab
  3. Click the "+" button on the right-hand side and select "Jars or directories..."
  4. Add the directory(ies) you want (note you can multi-select) and click OK
  5. In the dialog that comes up, select "classes" and NOT "jar directory"
  6. 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.

Darn answered 14/3, 2012 at 15:45 Comment(6)
I have the same problem as before with IJ11 and was happy to find an answer waiting for me this time.Weywadt
Perfect. Same process for IJ12. Ran across this post when trying to resolve 'java.lang.ClassNotFoundException: com/microsoft/jdbc/sqlserver/SQLServerDriver' This resolved the connection issue. ThanksStere
I don't get step 5 :( The dialog just doesn't come up.Godfree
@CharlesWood: When I wanted to select two folders with one of them containing sub-folders. I had to expand the one containing sub-folders via the little arrow symbol next to the folder name in order for the dialog to show up. Otherwise, the dialog would be skipped.Gastrovascular
The process has changed for IJ 13. For those of us using 11, this advice is still good.Weywadt
I am not sure if this is a different version, but the IntelliJ 2016.1.1 build seems to be a bit different. Select the "Project" view on the left Click the "File" menu in the top left Click "Project Structure" Navigate to the right hand tab "Dependencies"Churchwarden
W
45

You can try -Xbootclasspath/a:path option of java application launcher. By description it specifies "a colon-separated path of directires, JAR archives, and ZIP archives to append to the default bootstrap class path."

Wretch answered 22/7, 2010 at 9:5 Comment(6)
Some documentation on how -Xbootclasspath works: docs.oracle.com/cd/E15289_01/doc.40/e15062/optionx.htm#i1018570Demarcation
This is a good answer as it does not require you to change your project configuration. In fact I think it answers the question best.Runlet
This unfortunately did not work for me, as this results in all classes loaded from that classpath to be loaded by the bootstrap class loader, which breaks code that assumes that Xyz.class.getClassLoader() is not null.Leyva
@uaaquarius, Why is there no way to edit the command line string directly? This is frustrating.Astereognosis
@uaaquarius, Why is it that if I add mysql-connector-java-5.1.45/mysql-connector-java-5.1.45-bin.jar as dependency using the other method at https://mcmap.net/q/118890/-adding-jar-files-to-intellijidea-classpath it works, but doing using -Xbootclasspath/a it doesn't?Astereognosis
If using Windows you should use a semi-colon ";" instead of a ':' docs.oracle.com/cd/E15289_01/JRCLR/optionx.htmHinkle
N
26

Set "VM options" like: "-cp $Classpath$;your_classpath"

VM options

Nimocks answered 21/9, 2019 at 8:33 Comment(5)
This is what I used to do, but it seems to have stopped working on at least 2020.2Jollity
It still works with "-cp $Classpath$:your_classpath" using a colon instead of a semicolon as the delimiter. I'm surprised it worked using a semicolon since that is not a valid delimiter in the classpath.Transgress
If using Windows you should use a semi-colon ";" instead of a ':' which is for unix docs.oracle.com/cd/E15289_01/JRCLR/optionx.htmHinkle
I get ` error=206, Der Dateiname oder die Erweiterung ist zu lang`Heavensent
@Transgress This is the only solution works for me in a Windows machine. Thank you so muchCalamanco
P
19

I am using Idea 8. in your module dependancies tab (in the project structure dialog). Add a "Module Library". There you can select a Jar Directory to add. Then make sure the run profile is using the Classpath and JDK of the correct module when it runs (this is in the run config dialog.

Peterman answered 12/5, 2009 at 19:24 Comment(0)
L
4

Simply check that the directory/package of the class is marked as "Sources Root". I believe the package should be application or execution in your case.

To do so, right click on the package, and select Mark Directory As->Sources Root.

1

Larrikin answered 9/8, 2020 at 12:2 Comment(0)
R
1

You need not specify the classes folder. Intellij should be able to load it. You will get this error if "Project Compiler output" is blank.

Just make sure that below value is set: Project Settings -> Project -> Project Compiler output to your projectDir/out folder

Requisition answered 10/6, 2019 at 21:17 Comment(0)
T
0
  • Open Settings
  • Under project Settings, select "project settings (1)"
  • On the left, select "modules"
  • Select the desired module
  • Select the "Sources" tab
  • In the tree structure, find the directory you want to add. Then either add as a source or test source.
Throb answered 8/3, 2021 at 9:3 Comment(1)
Maybe mention the IntelliJ version you are using, it looks like it changed in the past and might change in the feature.Shammy

© 2022 - 2024 — McMap. All rights reserved.