You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Maven打包Jar添加JMeter依赖后出现主类找不到问题求助

问题:Maven打包Jar后找不到主类,添加JMeter依赖路径出错

我不是专业开发者,但需要开发一个Jar工具,也愿意提升自己,请包容我的代码。我尝试开发一个Jar工具,但使用Maven打包时,始终找不到主类,不清楚问题出在哪里。我做了很多调试,发现注释manifestEntries部分后就不会出现这个问题,但会缺失JMeter库,这也符合预期,求助解决办法。我不需要将JMeter的Jar包打入自身包中,因为运行环境会有这些依赖。

我的Java代码

package com.github.keydam.addprometheuslistener;

import java.io.File;
import java.io.IOException;

import org.apache.jmeter.engine.StandardJMeterEngine;
import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jorphan.collections.HashTree;

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;

@Command
public class addPrometheusListener implements Runnable {
    @Option(names = { "-f", "--file" }, required = true, description = "JMX file") 
    public static File jmxFile;

    public void run() {
        // Test de l'existence du fichier
        boolean jmxFileExists = jmxFile.exists();

        if (jmxFileExists) {
            // Positionnement de JMETER_HOME
            String jmeterHome = "C:\\Users\\xxxxxx\\Documents\\Outils\\Jmeter\\5.5";

            // Initialisation de Jmeter
            try {
                StandardJMeterEngine jmeter = new StandardJMeterEngine();
                JMeterUtils.setJMeterHome(jmeterHome);
                JMeterUtils.loadJMeterProperties(jmeterHome + "/bin/jmeter.properties");
                SaveService.loadProperties();
            } catch (IOException e) {
                System.out.println(e);
            }

            // Chargement du fichier
            try {
                HashTree testPlanTree = SaveService.loadTree(jmxFile);
            } catch (IOException e) {
                System.out.println(e);
            }

        } else {
            System.out.println("Error : file '" + jmxFile + "' does not exist");
        }
    }

    public static void main(String[] args) {
        int exitCode = new CommandLine(new addPrometheusListener()).execute(args);
        System.exit(exitCode); 
    }
}

我的pom.xml文件

<?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>
  <!--<packaging>jar</packaging>-->

  <groupId>com.github.keydam.addprometheuslistener</groupId>
  <artifactId>addPrometheusListener</artifactId>
  <version>1.0</version>
  <name>addPrometheusListener</name>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>8</maven.compiler.source>
    <maven.compiler.target>8</maven.compiler.target>
    <jmeter.version>5.5</jmeter.version>
    <jmeter.path>C:/Users/xxxxxx/Documents/Outils/Jmeter/5.5</jmeter.path>
  </properties>

  <dependencies>
    <dependency>
        <groupId>org.apache.jmeter</groupId>
        <artifactId>ApacheJMeter_core</artifactId>
        <version>${jmeter.version}</version>
        <scope>system</scope>
        <systemPath>${jmeter.path}/lib/ext/ApacheJMeter_core.jar</systemPath>
    </dependency>
    <dependency>
        <groupId>org.apache.jmeter</groupId>
        <artifactId>ApacheJMeter_java</artifactId>
        <version>${jmeter.version}</version>
        <scope>system</scope>
        <systemPath>${jmeter.path}/lib/ext/ApacheJMeter_java.jar</systemPath>
    </dependency>
    <dependency>
        <groupId>org.apache</groupId>
        <artifactId>jorphan</artifactId>
        <version>${jmeter.version}</version>
        <scope>system</scope>
        <systemPath>${jmeter.path}/lib/jorphan.jar</systemPath>
    </dependency>
    <dependency>
        <groupId>info.picocli</groupId>
        <artifactId>picocli</artifactId>
        <version>4.7.1</version>
        <scope>compile</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>3.3.0</version>
        <configuration>
          <archive>
            <manifest>
              <addClasspath>true</addClasspath>
              <mainClass>com.github.keydam.addprometheuslistener.addPrometheusListener</mainClass>
            </manifest>
            <manifestEntries>
              <Class-Path>${jmeter.path}</Class-Path>
            </manifestEntries>
          </archive>
        </configuration>
        <executions>
          <execution>
            <id>default-jar</id>
            <phase>none</phase>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>3.4.2</version>
        <configuration>
          <archive>
            <manifest>
              <addClasspath>true</addClasspath>
              <mainClass>com.github.keydam.addprometheuslistener.addPrometheusListener</mainClass>
            </manifest>
            <manifestEntries>
              <Class-Path>${jmeter.path}/lib/ext/ApacheJMeter_core.jar ${jmeter.path}/lib/ext/ApacheJMeter_java.jar ${jmeter.path}/lib/jorphan.jar</Class-Path>
            </manifestEntries>
          </archive>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
        </configuration>
        <executions>
          <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

解决办法

1. 修复Manifest中Class-Path的路径格式

Windows系统下,绝对路径在Manifest的Class-Path中必须遵循URL格式,以file:///开头(三个斜杠),否则JVM无法正确识别路径,进而触发类加载异常(包括找不到主类)。

修改maven-assembly-plugin中的manifestEntries配置:

<manifestEntries>
  <Class-Path>file:///${jmeter.path}/lib/ext/ApacheJMeter_core.jar file:///${jmeter.path}/lib/ext/ApacheJMeter_java.jar file:///${jmeter.path}/lib/jorphan.jar</Class-Path>
</manifestEntries>

2. 调整maven-jar-plugin的配置(可选)

你当前禁用了默认Jar的生成(default-jar的phase设为none),如果只需要assembly生成的Jar,可保留该设置;若需要生成普通Jar,删除该executions配置即可。

3. 验证打包后的Manifest文件

执行mvn clean package打包后,解压生成的Jar,查看META-INF/MANIFEST.MF,确认:

  • Main-Class字段正确指向com.github.keydam.addprometheuslistener.addPrometheusListener
  • Class-Path字段的JMeter路径格式正确,示例:
    Class-Path: file:///C:/Users/xxxxxx/Documents/Outils/Jmeter/5.5/lib/ext/ApacheJMeter_core.jar file:///C:/Users/xxxxxx/Documents/Outils/Jmeter/5.5/lib/ext/ApacheJMeter_java.jar file:///C:/Users/xxxxxx/Documents/Outils/Jmeter/5.5/lib/jorphan.jar
    

4. 运行Jar的替代方案(若路径问题仍存在)

如果不想在Manifest中硬编码绝对路径,可在运行时通过-cp参数指定类路径,这种方式更灵活:

java -cp "your-jar-file.jar;C:/Users/xxxxxx/Documents/Outils/Jmeter/5.5/lib/ext/*;C:/Users/xxxxxx/Documents/Outils/Jmeter/5.5/lib/*" com.github.keydam.addprometheuslistener.addPrometheusListener -f your-jmx-file.jmx

内容的提问来源于stack exchange,提问作者Keydam

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 02:20:40