1

I realize this is probably a very simple question, but I have been researching it for literally days on end to no avail. Any help would be much appreciated.

My project structure looks like this:

src
  |--Main.java
  |--WebScrapper.java

out
  |--Main.class
  |--WebScrapper.java

lib
  |--Jsoup

My WebScrapper.java file imports the Jsoup library utilities like this:

import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.jsoup.nodes.Element;

I am using a Java library in my project on the InteliJ IDE. In the IDE I go to "Project Structure -> Modules -> Add Jars or Directories" and my program works by me selecting the "run" button in IntelliJ, however when I compile the Java code, and try to run it with "java Main.class" from the terminal, I get the following error:

Exception in thread "main" java.lang.NoClassDefFoundError: org/jsoup/Jsoup
        at WebScrapper.returnPage(WebScrapper.java:12)
        at Main.main(Main.java:19)
Caused by: java.lang.ClassNotFoundException: org.jsoup.Jsoup
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(Unknown Source)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(Unknown Source)
        at java.base/java.lang.ClassLoader.loadClass(Unknown Source)
        ... 2 more

I know that this is a problem with the library and not my other class file, because when I changed the other class file to a simple System.out.printl statement, it works perfectly fine. How do I get this dependency to work in my project? Do I need to create a lib folder and do something? Or is it something else? I have browsed many other questions on stack overflow, and have seen solutions like cleaning the build, using a dependency management plugin like maven etc. However, rebuilding the project did not work and I would like to avoid using a dependency management system if possible, however if needed I will do so.

Thanks in advance, Michael

  • The problem is in the way that you are *running* the code. Said simply, you need to have all of the libraries you use on the **runtime** classpath. See the linked Q&A – Stephen C Nov 24 '18 at 08:01
  • I took a look at the linked questoin, and am still having a difficult time with this. Adding the classpath did not seem to work @StephenC – Michael Pope Nov 24 '18 at 09:08
  • Please update the questions with precise details of what you have done. Note that adding a library to the classpath is a normal stuff that is well described in the Oracle documentation, and many 3rd-party tutorials. It is really just a matter of understanding what you are doing. – Stephen C Nov 24 '18 at 09:59

1 Answers1

0

You’ll have to add the library to the class path when running your class; this is done by using e.g. -cp path-to-jsoup.jar.
When having more jar files you’ll have to list all by separating with ; in windows and : in linux.
You better try to spot the command used by IntelliJ when running your class upper in the IntelliJ terminal panel in order to better understand the similar way you have to compose it.

Adrian
  • 3,321
  • 2
  • 29
  • 46
  • Thank you for your response! I used "java Main -cp ../../../lib/jsoup-1.11.3.jar" which is the path to my Jsoup file, and still recieved the error. – Michael Pope Nov 24 '18 at 08:12
  • you’ll have to start the Main class by also specifying its package e.g. mypackage.Main. Also the -cp parameter must come before the class to run. – Adrian Nov 24 '18 at 09:14