maven 将jar添加到classPath时未找到主类

slhcrj9b  于 2023-02-18  发布在  Maven
关注(0)|答案(1)|浏览(236)

我不是一个真正的开发人员,但我需要一个工具,我愿意提高自己...请善待我的代码...
我尝试编写一个jar工具,但是每当我用Maven将其打包到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>

我不需要将Jmeter jar放在包中,因为它们在此代码可能运行的系统上可用。我遗漏了什么或做错了什么?当我尝试执行jar时,找不到主类
我尝试了很多调试,唯一注意到的是当我注解manifestEntries部分时,我不再有这个问题,但我缺少Jmeter库...这似乎很正常。

tp5buhyn

tp5buhyn1#

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>
    <groupId>com.github.keydam.addprometheuslistener</groupId>
    <artifactId>addPrometheusListener</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>
    <name>addPrometheusListener</name>
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
<dependencies>
    <!-- https://mvnrepository.com/artifact/org.apache.jmeter/ApacheJMeter_core -->
    <dependency>
        <groupId>org.apache.jmeter</groupId>
        <artifactId>ApacheJMeter_core</artifactId>
        <version>5.5</version>
    </dependency>

    <dependency>
        <groupId>org.apache.jmeter</groupId>
        <artifactId>ApacheJMeter_java</artifactId>
        <version>5.5</version>
    </dependency>

    <dependency>
        <groupId>info.picocli</groupId>
        <artifactId>picocli</artifactId>
        <version>4.7.1</version>
        <scope>compile</scope>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-jar-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>
                </archive>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>3.5.0</version>
            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
<!--
<classpathPrefix>lib/</classpathPrefix>
-->

                        <outputDirectory>${project.build.directory}</outputDirectory>
                        <overWriteReleases>false</overWriteReleases>
                        <overWriteSnapshots>false</overWriteSnapshots>
                        <overWriteIfNewer>true</overWriteIfNewer>
                        <excludeTransitive>true</excludeTransitive>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
</project>

可选:您可以将其删除

<excludeTransitive>true</excludeTransitive>

可选:您可以使用此选项,也可以指向其他目录中的jar文件

<classpathPrefix>lib/</classpathPrefix>

构建

mvn clean package

测验

cd taget
java -jar addPrometheusListener-1.0.jar

输出

Missing required option: '--file=<jmxFile>'
Usage: <main class> -f=<jmxFile>
  -f, --file=<jmxFile>   JMX file

相关问题