Issue
I have Java maven project that read Json file and get some data. But I can not run my program by Linux Terminal.
package name: my
classname: StartProgram
OS Ubuntu 20.04.2.064
Maven 3.6.3
Java 14.0.2
I did next steps:
mvn clean install
cd target
java -cp jsonReader-0.0.1-SNAPSHOT.jar my.StartProgram
the output:
Error: Could not find or load main class my.StartProgram
Caused by: java.lang.ClassNotFoundException: my.StartProgram
Part of my pom.xml:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>my.StartProgram</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
How I can fix it? Thank you in advance.
Solution
Simply way in project directory:
mvn exec:java -Dexec.mainClass="StartProgram"
Another way:
Your project depends on another external library, so when you use java -cp ...
you mast provide all of them.
you can build dependency list by (need run once once when your pom chanegs):
mvn dependency:build-classpath -Dmdep.outputFile=cp.txt
and then you can run your program by:
java -cp `cat cp.txt`:target/jsonReader-1.0-SNAPSHOT.jar StartProgram
Answered By - Slawomir Jaranowski
Answer Checked By - Marilyn (JavaFixing Volunteer)