运行RAM数据采集Jar包时出现Could not find or load main class错误的求助
Let's break down the issues you're facing step by step—there are a few overlapping problems here that are causing your errors:
1. Jar File Name & Path Mismatch
First, look at the error when you tried using -jar:
Error: Unable to access jarfile /home/ubuntu/RAMDataGenerator/target/RAMDataGenerator-1.0.0-SNAPSHOT-jar-with-dependencies.jar
But your pom.xml clearly defines:
<artifactId>RAMGenerator</artifactId><version>0.0.1-SNAPSHOT</version>
This means Maven should generate a jar named RAMGenerator-0.0.1-SNAPSHOT-jar-with-dependencies.jar (not RAMDataGenerator-1.0.0-...). You’re either:
- Running the wrong jar file (verify the actual filename in your target folder with
ls ~/RAMDataGenerator/target/), or - You modified the artifactId/version in your pom but didn’t re-run
mvn packageto rebuild the jar with the correct name.
This mismatch is also likely why your -cp command failed earlier—if the jar path you specified doesn’t exist, Java can’t find the data.App class inside it.
2. Conflicting Java Command Arguments
You tried combining -jar and -cp, which doesn’t work:
java -jar -cp ~/...jar data.App
The -jar flag tells Java to use the manifest file inside the jar to handle the classpath and main class, so it completely ignores any -cp or -classpath arguments you provide. You have two valid options to run your app:
Option A: Use -jar (Recommended)
Since your maven-assembly-plugin already configures <mainClass>data.App</mainClass> in the jar’s manifest, just run the correct jar file:
java -jar ~/RAMDataGenerator/target/RAMGenerator-0.0.1-SNAPSHOT-jar-with-dependencies.jar
Java will automatically locate and run the main class defined in the manifest.
Option B: Use -cp Without -jar
If you want to explicitly specify the main class, use -cp and omit -jar:
java -cp ~/RAMDataGenerator/target/RAMGenerator-0.0.1-SNAPSHOT-jar-with-dependencies.jar data.App
Just make sure the jar file exists with the exact name you’re referencing.
3. Inconsistent Java Version Configuration
Your pom has conflicting Java version settings:
- The
maven-compiler-pluginis set to<source>7</source>and<target>7</target> - But the properties section uses
<maven.compiler.source>1.8</maven.compiler.source>and<maven.compiler.target>1.8</maven.compiler.target>
This could lead to unexpected compilation issues down the line. Pick one version (preferably Java 8 or higher, since Lombok 1.18.12 works best with Java 8+) and align both settings. For example:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>${maven.compiler.source}</source> <target>${maven.compiler.target}</target> </configuration> </plugin>
Step-by-Step Fix
- Verify the jar filename in your target directory:
ls ~/RAMDataGenerator/target/ - If the filename doesn’t match your pom’s configuration, run
mvn clean packageto rebuild the jar with the correct name. - Use either the
-jaror-cpcommand as shown above, using the exact jar filename from step 1. - Fix the Java version mismatch in your pom to avoid future compilation problems.
I suspect the jar filename mismatch is the root cause here—once you use the correct Maven-generated jar name, the main class should load without issues, especially since you confirmed data/App.class exists in the jar.
内容的提问来源于stack exchange,提问作者PiotrKulesza

