【问题标题】:Java program won't run properly after compilingJava程序编译后无法正常运行
【发布时间】:2020-01-27 05:06:50
【问题描述】:

我是编程新手,我使用互联网上可以找到的所有资源制作我的第一个程序。所以我使用 NetBeans 11.2 用 Maven 应用程序制作了 Java。当我遵守我的程序时,我终于明白了这一点,现在我被卡住了。我试图从命令提示符运行它并且问题开始了。首先我必须处理NoClassDefFoundError: AbsoluteLayout 问题。我不得不通过将整个项目中的布局更改为NullLayout 来修复它。最终程序启动后,我得到了另一个错误NoClassDefFoundError: com/mchange/v2/c3p0/ComboPooledDataSource。几天来,我试图找到答案,但仍然没有运气。尝试了每个建议的步骤。

  • 检查了 pom.xml 文件 - 列出了依赖项。
  • NetBeans 中已检查的库 - 包括的库
  • 为 NetBeans 重新配置 Maven。
  • 使用不同的选项多次清理和重建项目(只需构建、清理和构建,然后继续)

看起来由于某种原因依赖项不想加载。
谁能帮帮我。

谢谢。

<?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>ews.ykotov</groupId>
    <artifactId>TechInfo</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>
    <repositories>
        <repository>
            <id>unknown-jars-temp-repo</id>
            <name>A temporary repository created by NetBeans for libraries and jars it could not identify. Please replace the dependencies in this repository with correct ones and delete this repository.</name>
            <url>file:${project.basedir}/lib</url>
        </repository>
    </repositories>
    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.18</version>
            <exclusions>
                <exclusion>
                    <groupId>com.google.protobuf</groupId>
                    <artifactId>protobuf-java</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>mchange-commons-java</artifactId>
            <version>0.2.19</version>
        </dependency>
        <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.5.5</version>
        </dependency>
        <dependency>
            <groupId>org.swinglabs.swingx</groupId>
            <artifactId>swingx-all</artifactId>
            <version>1.6.5-1</version>
        </dependency>
        <dependency>
            <groupId>com.toedter</groupId>
            <artifactId>jcalendar</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>org.netbeans.external</groupId>
            <artifactId>AbsoluteLayout</artifactId>
            <version>RELEASE112</version>
        </dependency>
    </dependencies>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>13</maven.compiler.source>
        <maven.compiler.target>13</maven.compiler.target>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.2.0</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>techinfo1.MainPage</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

命令提示错误:

【问题讨论】:

  • 将包含ComboPooledDataSource 的jar 添加到您的类路径中。 Maven 负责构建部分。
  • 添加到系统环境 CLASSPATH 仍然没有帮助。
  • 试试java -cp ...jar
  • 错误:无法找到或加载主类 TechInfo-1.0.jar 原因:java.lang.ClassNotFoundException:TechInfo-1.0.jar
  • @Yuri,要运行您的代码,您不仅需要自己的代码,还需要所有您定义为依赖项的 JAR。您可以使用 maven-dependency-plugin 创建包含所有依赖项 JAR 的 lib 目录。然后确保使用 java -cp "TechInfo.jar;lib/*" techinfo1.MainPage 将该库包含在您的类路径中

标签: java maven dependencies


【解决方案1】:

感谢@Simon 终于解决了这个问题并进入下一阶段。我还必须使用这篇文章 (https://crunchify.com/how-to-create-build-java-project-including-all-dependencies-using-maven-maven-resources-maven-dependency-maven-jar-plugin-tutorial/) 修改我的 pom.xml 文件构建部分。也许有人会觉得它有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多