gpt4 book ai didi

java - Maven安装和运行JAR文件会产生各种编译错误

转载 作者:行者123 更新时间:2023-12-01 18:49:29 24 4
gpt4 key购买 nike

当使用 mvn install 创建 jar 文件并使用 java -jar App.jar 运行该 jar 时,我在运行时遇到各种不同的编译错误。这些错误在每次安装后都会发生变化。一些例子:

Exception in thread "main" java.lang.Error: Unresolved compilation problems:
ByteOrder cannot be resolved
ByteOrder cannot be resolved
ByteOrder cannot be resolved
ByteOrder cannot be resolved

at com.aidan.core.requestreply.ByteRequest.generateUniqueID(ByteRequest.java:49)
at com.aidan.core.requestreply.ByteRequest.fire(ByteRequest.java:105)
at com.aidan.core.requestreply.Request.fire(Request.java:22)
at com.aidan.core.util.DistCall.getSecretKey(DistCall.java:27)
at com.aidan.core.App.main(App.java:13)
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
StringUtils cannot be resolved

at com.aidan.core.App.main(App.java:19)

我不明白为什么这些编译错误不会使 mvn install 命令失败,也不明白为什么每次安装时错误消息都不同。

pom.xml

<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.aidan.core</groupId>
<artifactId>App</artifactId>
<packaging>jar</packaging>
<version>1.0</version>
<name>App</name>
<url>http://maven.apache.org</url>
<repositories>
<repository>
<id>maven-repository</id>
<url>https://mvnrepository.com/</url>
</repository>
</repositories>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<!-- protobuf paths -->
<protobuf.input.directory>${project.basedir}/src/main/proto</protobuf.input.directory>
<protobuf.output.directory>${project.basedir}/src/main/java</protobuf.output.directory>

<!-- library versions -->
<build-helper-maven-plugin.version>1.9.1</build-helper-maven-plugin.version>
<maven-antrun-plugin.version>1.8</maven-antrun-plugin.version>
<maven-dependency-plugin.version>2.10</maven-dependency-plugin.version>
<maven-shade-plugin.version>2.4.2</maven-shade-plugin.version>
<os-maven-plugin.version>1.4.1.Final</os-maven-plugin.version>
<protobuf.version>2.6.1</protobuf.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/com.google.protobuf/protobuf-java -->
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>${protobuf.version}</version>
</dependency>
</dependencies>

<build>
<finalName>App</finalName>
<extensions>
<!-- provides os.detected.classifier (i.e. linux-x86_64, osx-x86_64) property -->
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>${os-maven-plugin.version}</version>
</extension>
</extensions>
<plugins>

<!-- Set a JDK compiler level -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
</configuration>
</plugin>

<!-- Make this jar executable -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<!-- Jar file entry point -->
<mainClass>com.aidan.core.App</mainClass>
<addClasspath>true</addClasspath>
<classpathPrefix>lib</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>${maven-dependency-plugin.version}</version>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>lib</outputDirectory>
</configuration>
</execution>
<execution>
<id>copy-protoc</id>
<phase>generate-sources</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.google.protobuf</groupId>
<artifactId>protoc</artifactId>
<version>${protobuf.version}</version>
<classifier>${os.detected.classifier}</classifier>
<type>exe</type>
<overWrite>true</overWrite>
<outputDirectory>${project.build.directory}</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>

<!-- compile proto buffer files using copied protoc binary -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>${maven-antrun-plugin.version}</version>
<executions>
<execution>
<id>exec-protoc</id>
<phase>generate-sources</phase>
<configuration>
<target>
<property name="protoc.filename" value="protoc-${protobuf.version}-${os.detected.classifier}.exe" />
<property name="protoc.filepath" value="${project.build.directory}/${protoc.filename}" />
<chmod file="${protoc.filepath}" perm="ugo+rx" />
<mkdir dir="${protobuf.output.directory}" />
<path id="protobuf.input.filepaths.path">
<fileset dir="${protobuf.input.directory}">
<include name="**/*.proto" />
</fileset>
</path>
<pathconvert pathsep=" " property="protobuf.input.filepaths" refid="protobuf.input.filepaths.path" />
<exec executable="${protoc.filepath}" failonerror="true">
<arg value="-I" />
<arg value="${protobuf.input.directory}" />
<arg value="--java_out" />
<arg value="${protobuf.output.directory}" />
<arg line="${protobuf.input.filepaths}" />
</exec>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>

<!-- add generated proto buffer classes into the package -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>${build-helper-maven-plugin.version}</version>
<executions>
<execution>
<id>add-classes</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${protobuf.output.directory}</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>

</plugins>
</build>
</project>

生成文件

clean:
rm -rf target
rm -rf lib
rm App.jar
mvn clean

jar:
mvn clean install dependency:copy-dependencies -DskipTests
cp target/App.jar App.jar

test:
mvn clean test -q

run:
java -jar App.jar

go:
echo "[COMPILING]"
$(MAKE) jar
echo "Done!\n\n[RUNNING TESTS]"
$(MAKE) test
echo "Done!\n\n[RUNNING PROGRAM]"
$(MAKE) run

我正在运行 make go 来进行完整编译并运行 jar 文件。

最佳答案

> Exception in thread "main" java.lang.Error: Unresolved compilation problem:
StringUtils cannot be resolved
at com.aidan.core.App.main(App.java:19

StringUtils 类无法解析。是第三方库还是您自己的自定义类?

您需要确保您的 POM 中定义了所有必需的依赖项

关于java - Maven安装和运行JAR文件会产生各种编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59761648/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com