1

I do the compilation of this simple programm i found here

package com.stackoverflow.q2835505;

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

public class Test {

public static void main(String[] args) throws Exception {
    String url = "https://stackoverflow.com/questions/2835505";
    Document document = Jsoup.connect(url).get();

    String question = document.select("#question .post-text").text();
    System.out.println("Question: " + question);

    Elements answerers = document.select("#answers .user-details a");
    for (Element answerer : answerers) {
        System.out.println("Answerer: " + answerer.text());
    }
}

}

with this command in terminal:

javac -cp ./jsoup-1.10.2.jar Test.java

but when i try to run it i take this:

Error:Could not find or load main class

and I can't find the solution, where is the problem? Thanks.

Community
  • 1
  • 1
geo
  • 11
  • 3
  • @Pshemo Please consider reopening this question. He had a `javac` issue beyond the main `java` *classpath* error. I feel my step-by-step answer might benefit others Java beginners as well. (BTW, I already linked to the aforementioned question in my answer). Thanks man! – Paulo Mattos May 22 '17 at 20:06
  • @OP `but when i try to run it i take this` you didn't show how you try to *run* it. Your previous command shows how you are trying to *compile* it. – Pshemo May 22 '17 at 20:24
  • @PauloMattos What do you mean by "He had a javac issue beyond the main java classpath error"? `javac -cp ./jsoup-1.10.2.jar Test.java` should create proper *.class* file (at least OP doesn't mention any problems with compilation). "Could not find or load main class" is problem with running `main` method from `Test.class` class. To make it possible OP needs to specify location of package which contains it in `-cp`. All of this is explained in general duplicate. Your answer may still help others regardless if question is closed or not so I don't see reason to not let it stay closed as duplicate. – Pshemo May 22 '17 at 20:41
  • 1
    @Pshemo I was surprised too... On macOS (at least), running `javac`, without the `-d .` option, places the generated `Test.class` in the *current directory* (and not in the expected `com/stackoverflow/q2835505/Test.class` path). This makes tricky to run it using `java`, even when using the full qualified class name. As such, both issues are related... – Paulo Mattos May 22 '17 at 20:48
  • Possibly helpful: https://stackoverflow.com/a/18740577 – Pshemo May 22 '17 at 21:20
  • @Pshemo It doesn't look like a bug at all — its by design! And goes well *beyond* macOS... Check the **-d directory** option info on [Javac man page](https://docs.oracle.com/javase/8/docs/technotes/tools/windows/javac.html#BHCGAJDC). – Paulo Mattos May 22 '17 at 21:21
  • @PauloMattos OK, you are right. Will remove comment about bug since it is simply not true (looks like I rely on IDE too much and forgot about `javac` basics). – Pshemo May 22 '17 at 21:34

1 Answers1

1

You might be running into more than one issue here...

Javac. To be sure, compile your Java app like this:

javac -cp ./jsoup-1.10.2.jar -d . Test.java

the -d option ensures that the compiled class is placed in the corresponding package directory:

com/stackoverflow/q2835505/Test.class

and not on your current directory. Let's check the man page just to be sure (-d option):

Sets the destination directory for class files. The directory must already exist because javac does not create it. If a class is part of a package, then javac puts the class file in a subdirectory that reflects the package name and creates directories as needed.

If the -d option is not specified, then javac puts each class file in the same directory as the source file from which it was generated.

Java. Finally, run it using:

java -cp .:./jsoup-1.10.2.jar com.stackoverflow.q2835505.Test

this runs your app using your current directory (.) and jsoup-1.10.2.jar as your class path. The current directory is mandatory so java finds your Test.class as well as the JSoup jar.


See this nice answer for a lot more information on the java command syntax.

Community
  • 1
  • 1
Paulo Mattos
  • 18,845
  • 10
  • 77
  • 85