zjffdu
zjffdu

Reputation: 28764

maven provided dependency will cause NoClassDefFoundError in intellij?

IntelliJ doesn't seem to put the provided dependency on the classpath when I run it, however I can do this successfully in Eclipse.
As it would be a lot more convenient for me, how can I do this in IntelliJ?

Upvotes: 6

Views: 2849

Answers (4)

Siddhartha
Siddhartha

Reputation: 4454

IntelliJ now has an option to Include dependencies with provided scope in the Run Configuration:

enter image description here

Any library marked as scope - provided means that the library (as the name suggests) is supposed to be provided by the JDK or the container (e.g. tomcat) at runtime.

Upvotes: 2

宏杰李
宏杰李

Reputation: 12158

this answer is based on @Meo's answer.

ALT + Enter to create a unit test:

enter image description here

then run it :

enter image description here

Upvotes: 0

ivanprado
ivanprado

Reputation: 51

I'm having the same problem. Intellij does not include provided dependencies in classpath. See this source. The best solution I found is to run it as maven app, using the exec:java goal. For example:

exec:java -Dexec.classpathScope=compile -Dexec.mainClass=com.splout.db.integration.NShardEnsemble -Dexec.args=4

Better solutions are welcome.

Upvotes: 5

Meo
Meo

Reputation: 12491

Does it work in Maven via command line? The behaviour seems correct. Eclipse used to handle classpath badly, so I guess it still does.

There is a difference if you run something in Test source root or Source root, since the scope provided is only available on the compilation and test classpath.

If you run a test, or a main method in Test source root, then it can use provided dependencies, but if you try to execute something (via IntelliJ, or exec-maven-plugin) in Source root, then it will fail on ClassNotFoundException.

Upvotes: 2

Related Questions