3

I'm trying to explore webscraping in Java and am just starting with a very basic Jsoup program to get started. I am pretty sure there is some sort of path issue with what I am doing. I have tried different variations, and just to get it working have simplified the process by including the library in the same directory as my source file(to simplify the path while I figure out what is going on). Here is what I have been doing and the output:

javac -cp jsoup-1.7.3.jar URLParse.java

The above compiles with no errors(it also compiled fine when I had the jar in its own folder and specified the path), the below happens when I try to run the program:

java -cp jsoup-1.7.3.jar URLParse.java
Exception in thread "main" java.lang.NoClassDefFoundError: URLParse/java
Caused by: java.lang.ClassNotFoundException: URLParse.java
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:323)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:268)
Could not find the main class: URLParse.java. Program will exit.

The following is the code in case it help:

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;

public class URLParse{

   public static void main(String[] args){


      String URL = "http://www.google.com";

      try{

         Document doc = Jsoup.connect(URL).get();
         System.out.println("Ok here");
         String title = doc.title();

         System.out.println(title);
      } catch (IOException e){
         e.printStackTrace();
      }

   }

}

Thanks for any help or suggestions.

Sandeep Chatterjee
  • 3,220
  • 9
  • 31
  • 47
user3694599
  • 33
  • 1
  • 4

2 Answers2

2

I think the issue here is that you're not including the directory with the code of your main class in the classpath variable.

java -cp jsoup-1.7.3.jar URLParse.java

Should be

java -cp .:jsoup-1.7.3.jar URLParse

On Mac/Linux and

java -cp .;jsoup-1.7.3.jar URLParse

On Windows. Note that you don't include .java on the class you're trying to run.

Information taken right from this.

Community
  • 1
  • 1
hallfox
  • 66
  • 3
0

Assuming you have a similar directory structure:

enter image description here

and you are trying to run your URLParse class you would need:

java -classpath testPackage/jsoup-1.7.2.jar:. testPackage.URLParse

enter image description here

For more details, please refer Java Glossary

Sandeep Chatterjee
  • 3,220
  • 9
  • 31
  • 47
  • 1
    Hi Sandeep thank you for taking the time to respond. It is executing now. Thank you for posting the Java Glossary link, I will take a look at it and hopefully get a better grip on working with the classpath. – user3694599 May 31 '14 at 20:00