简体   繁体   中英

Program runs fine when I run in VSCode, but the package the maven extension builds doesn't run

Background: I am using VS Code and the VS Code Java extension pack (which includes a maven extension) to write a project that uses htmlunit. Currently, I'm just using someone else's code just to see if I can get my environment working. I am a total noob to maven and VS Code and a semi-noob to Java.

When I run my program using the VS Code run tab, it runs as expected*. When I use the maven package command to build an executable jar, the jar builds fine but when I run it with java -jar ___.jar I get an error:

Exception in thread "main" java.lang.NoClassDefFoundError: com/gargoylesoftware/htmlunit/FailingHttpStatusCodeException
        at reliant.Main.main(Main.java:44)
Caused by: java.lang.ClassNotFoundException: com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
        ... 1 more 

Here is the code that uses htmlunit:

package reliant; //I know now this is not how package naming conventions work, but I don't think it causes a problem
import java.io.IOException;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException; //the import that seems to be buggy
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

public class RedditClient {
    
    private final WebClient CLIENT = new WebClient(BrowserVersion.CHROME);
    private final String username;
    private char[] password;

    public RedditClient(String username, char[] password) {
        this.username = username;
        this.password = password;
     
        CLIENT.getCookieManager().setCookiesEnabled(true);
    }

    public void checkLogin() {
        System.out.println(username + ", " + new String(password));
    }

    public void login() {
        String loginURL = "https://www.reddit.com/login";

        try {
            HtmlPage loginPage = CLIENT.getPage(loginURL);

            HtmlForm loginForm = loginPage.getFirstByXPath("//form[@id='login-form']");
            //System.out.println(loginPage.getWebResponse().getContentAsString());
            loginForm.getInputByName("user").setValueAttribute(username);
            loginForm.getInputByName("passwd").setValueAttribute(new String(password));

            loginForm.getElementsByTagName("button").get(0).click();

        } catch (FailingHttpStatusCodeException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } 
    }

    public String getHTML(String url) {
        try {
            return CLIENT.getPage(url).getWebResponse().getContentAsString();
        } catch (FailingHttpStatusCodeException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

    public void close() {
        CLIENT.close();
    }
}

And here is line 44 of Main.java:

RedditClient c = new RedditClient(username, password);

This seems to look right to me, I added htmlunit to my maven dependencies. I can show you a screeenshot of the maven dependencies if you want, but I omitted it for now since it's kind of long/hard to read.

Here is the pom file:

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>reliant</groupId>
  <artifactId>redditlogin</artifactId>
  <version>2.0</version>

  <name>redditlogin</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
  </properties>

  <dependencies>

    <dependency>
      <groupId>htmlunit</groupId>
      <artifactId>htmlunit</artifactId>
      <version>1.14</version>
    </dependency>


   <dependency>
     <groupId>org.jsoup</groupId>
     <artifactId>jsoup</artifactId>
     <version>1.13.1</version>
   </dependency>


    <dependency>
      <groupId>net.sourceforge.htmlunit</groupId>
      <artifactId>htmlunit</artifactId>
      <version>2.41.0</version>
    </dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
          <configuration>
            <archive>
              <manifest>
                <addClasspath>true</addClasspath>
                  <mainClass>reliant.Main</mainClass>
              </manifest>
            </archive>
          </configuration>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
        <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>3.7.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-project-info-reports-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

The fact that the jar file errors out but running in VS Code proceeds as expected* makes no sense to me whatsoever.

Also, on an unrelated note, why is a "class not found" error a run-time error? Why is maven able to successfully build the package?

*I say "as expected" because there is a bug in the code I'm using, but it is a very-specific runtime error. Basically, the code I'm using was designed to log in to reddit before reddit got the OAuth thing so now the line that tries to get the login form returns null and that breaks the rest of it. The point is, I see no reason for why running in VS Code vs running the jar should have any different outcomes. I don't even know where to begin with something like this.

Thank you for help, please let me know if there is anything I can add to make this question easier to answer.

Ok so turns out this was pretty silly. Basically, the classpath listed my dependencies (because of how I set up the pom) but that doesn't mean that it specified the path to those dependencies. In fact, it didn't specify a path at all, so whenever java tried to run the jar, it looked for the dependencies in the current directory. I know that because when I put the dependencies in the same directory as the jar, it worked. The better solution here would be to modify the pom to add a prefix to the classpath so that the java knows where to find the dependencies. Unfortunately, my dependencies are in different places so that's not really an option for me; instead, I'm going to try to find out how to make a fat/uber jar.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM