繁体   English   中英

当我在 VSCode 中运行时程序运行良好,但是 package maven 扩展构建不运行

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

背景:我正在使用 VS Code 和 VS Code Java 扩展包(其中包括 maven 扩展)来编写一个使用 htmlunit 的项目。 目前,我只是在使用别人的代码来看看我是否可以让我的环境正常工作。 我是 maven 和 VS Code 的菜鸟,是 Java 的半菜鸟。

当我使用 VS Code 运行选项卡运行程序时,它按预期运行*。 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 

下面是使用 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();
    }
}

这是 Main.java 的第 44 行:

RedditClient c = new RedditClient(username, password);

这对我来说似乎是正确的,我将 htmlunit 添加到我的 maven 依赖项中。 如果您愿意,我可以向您展示 maven 依赖项的屏幕截图,但我暂时省略了它,因为它有点长/难以阅读。

这是pom文件:

<?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>

jar 文件出错但在 VS Code 中运行按预期进行的事实对我来说毫无意义。

另外,在不相关的说明中,为什么“找不到类”错误是运行时错误? 为什么 maven 能够成功构建 package?

*我说“如预期”是因为我正在使用的代码中有一个错误,但这是一个非常具体的运行时错误。 基本上,我使用的代码旨在在 reddit 获得 OAuth 之前登录 reddit,所以现在尝试获取登录表单的行返回 null 并打破了 Z65E8800B5C6800AAD896F888B2A 它。 关键是,我看不出为什么在 VS Code 中运行与在 jar 中运行会有任何不同的结果。 我什至不知道从哪里开始这样的事情。

感谢您的帮助,如果我可以添加任何内容以使这个问题更容易回答,请告诉我。

好吧,事实证明这很愚蠢。 基本上,类路径列出了我的依赖项(因为我如何设置 pom),但这并不意味着它指定了这些依赖项的路径。 事实上,它根本没有指定路径,所以每当 java 尝试运行 jar 时,它都会在当前目录中查找依赖项。 我知道这是因为当我将依赖项放在与 jar 相同的目录中时,它起作用了。 这里更好的解决方案是修改 pom 以向类路径添加前缀,以便 java 知道在哪里可以找到依赖项。 不幸的是,我的依赖项在不同的地方,所以这对我来说不是一个真正的选择; 相反,我将尝试找出如何制作胖/超级 jar。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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