243

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?

Mahozad
  • 18,032
  • 13
  • 118
  • 133
sal
  • 23,373
  • 15
  • 66
  • 85

8 Answers8

333

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
kevinarpe
  • 20,319
  • 26
  • 127
  • 154
stivlo
  • 83,644
  • 31
  • 142
  • 199
  • 3
    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? – Καrτhικ Dec 09 '14 at 16:52
  • 12
    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 uaaquarius – Pavel Vyazankin Feb 04 '15 at 15:06
  • Thank you ! This solved my problem after ages of searching around ! – Jonas Czech May 22 '15 at 18:53
  • 1
    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 work – Takeru Jan 21 '16 at 07:20
  • @Takeru Did you figure this out? – rosshamish Jul 22 '16 at 05:23
  • 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. – Suraj Bajaj Oct 17 '16 at 18:59
  • 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!" – Am1rr3zA Jun 08 '17 at 20:26
  • @Am1rr3zA 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! – kinsley kajiva Sep 01 '17 at 06:30
  • @kinsleykajiva I have added the classpath into tomcat's conf file (classpath) – Am1rr3zA Sep 01 '17 at 20:36
  • 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 postgres – tatsu Nov 17 '17 at 09:39
  • @stivlo, Where can I **type java -cp directly** as in https://stackoverflow.com/a/44302960/632951 ? – Pacerier Dec 10 '17 at 00:31
  • after you ran the app once you can go near the top left corner and choose edit configuration and add it in the VM options – stivlo Dec 10 '17 at 20:59
  • Step 1 and 2 can be simplified by just pressing Ctrl + Alt + Shift + S – Clijsters Jan 17 '18 at 10:48
  • 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... – dr jerry Apr 02 '19 at 11:17
  • @stivlo I'm using Intellij 2019, and after step 5, it doesn't show dialog on step 6. – Dr.jacky Sep 15 '19 at 13:03
  • please, I don't see *Choose Categories of Selected File* anywhere. Intellij IDEA Community 2020.1.1 Runtime v11.0.6. thank you – Scaramouche May 04 '20 at 17:26
  • Confirmed, this solution works for Intellij Ultimate 2020.1 – Ed J May 14 '20 at 20:39
98

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.

samkass
  • 5,774
  • 2
  • 21
  • 25
  • 1
    I have the same problem as before with IJ11 and was happy to find an answer waiting for me this time. – sal Aug 09 '12 at 13:36
  • 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. Thanks – jkmurphy1 Jan 21 '13 at 19:32
  • 6
    I don't get step 5 :( The dialog just doesn't come up. – Charles Wood Dec 03 '13 at 22:32
  • 1
    @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. – Timo Reimann May 16 '14 at 16:44
  • The process has changed for IJ 13. For those of us using 11, this advice is still good. – sal Jul 20 '14 at 04:40
  • 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" – Mark McGookin Apr 07 '16 at 08:59
43

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."

uaaquarius
  • 663
  • 5
  • 9
  • 2
    Some documentation on how -Xbootclasspath works: http://docs.oracle.com/cd/E15289_01/doc.40/e15062/optionx.htm#i1018570 – Lambart May 06 '15 at 17:37
  • 5
    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. – Chewy Mar 30 '17 at 20:16
  • 2
    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. – Njol May 04 '17 at 10:37
  • @uaaquarius, Why is there no way to edit the command line string directly? This is frustrating. – Pacerier Dec 10 '17 at 00:54
  • @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://stackoverflow.com/a/16742141/632951 it works, but doing using `-Xbootclasspath/a` it doesn't? – Pacerier Dec 10 '17 at 01:29
  • If using Windows you should use a semi-colon ";" instead of a ':' https://docs.oracle.com/cd/E15289_01/JRCLR/optionx.htm – Aldahunter Jun 24 '22 at 11:47
25

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

VM options

zoirs
  • 591
  • 5
  • 6
  • This is what I used to do, but it seems to have stopped working on at least 2020.2 – Mike Miller Sep 29 '20 at 19:54
  • 1
    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. – neildo Jul 14 '21 at 04:55
  • 1
    If using Windows you should use a semi-colon ";" instead of a ':' which is for unix https://docs.oracle.com/cd/E15289_01/JRCLR/optionx.htm – Aldahunter Jun 24 '22 at 11:48
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.

Mike Pone
  • 18,705
  • 13
  • 53
  • 68
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

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

VNN456
  • 137
  • 8
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.
Jens Baitinger
  • 2,230
  • 14
  • 34
O.Kaplan
  • 59
  • 1
  • 5