1

I know this question is probably very easy for you, but I have real problems with it.

When I put, for example, apache commons-lang jar library into project's folder lib that is on the same level as src folder and add this jar library into project's build patch, eclipse compiles everything without errors and it puts all sources into exported .jar file (including lib folder and the contents).

BUT, when I start application, I am getting this error:

23:55:42 [SEVERE] Exception in thread "main" 23:55:42 [SEVERE] java.lang.NoClassDefFoundError: org/apache/commons/lang3/StringUtils

Can someone explain me why JVM does not see libraries that I have in lib folder and that are defined in project's build patch?

What I have to do to make my compiled application see this jar files?

Microz
  • 97
  • 1
  • 8
  • When you launch your application jar using the [`java`](https://docs.oracle.com/javase/7/docs/technotes/tools/windows/java.html) command, you can specify the runtime classpath using `-cp` option. – Bhesh Gurung Apr 03 '18 at 22:32
  • Thanks for your reply, but is it possible to make application use jars in lib folder automaticaly or I have to create simple launcher that will execute java -cp with libraries locations? – Microz Apr 03 '18 at 22:35
  • No. But, to avoid typing the command-line every time, you can create a shell/bat script to launch your application. – Bhesh Gurung Apr 03 '18 at 22:37
  • 1
    Did you check the other tab in your build path called 'Order and Export'? The lib must be selected if you want to include it within your .jar – Skeith Apr 03 '18 at 22:48
  • jar files need to be listed explicitly in the -cp, unlike class files that can be added by just including the folder they are located [link] (https://docs.oracle.com/javase/tutorial/deployment/jar/basicsindex.html) – Guillem Apr 04 '18 at 09:58
  • If I will create maven project instead of normal java project and I will add this apache common library to maven setup, will it fix this error? – Microz Apr 07 '18 at 09:23
  • @Bhesh Gurung I did not know about something like this. If I will check this .jar in this tab, will my jar application use jar files from lib folder that is inside exported application? – Microz Apr 07 '18 at 09:29

1 Answers1

0

You have to mention the classpath explicitly in the startup script unless you are using any container like tomcat, where you can place the libraries and dependencies in the lib folder.

You can use -cp and provide the path the library, so that the java program will use all the libraries which are required from the classpath mentioned.

Hope this helps!

Regards, Eby J

Eby Jacob
  • 1,418
  • 1
  • 10
  • 28
  • If I will create maven project instead of normal java project and I will add this apache common library to maven setup, will it fix this error? – Microz Apr 07 '18 at 09:23
  • Yes you can create a maven project and compile with all dependencies. maven goal to do this is . `mvn clean compile assembly:single` – Eby Jacob Apr 07 '18 at 09:39
  • I found something in eclipse called "Order and Export" in Build Path of my application's java project. If I will check apache library in this tab, will it find common apache lib if it will be inside compiled jar of my application? – Microz Apr 08 '18 at 07:59