Developed a Maven project in NetBeans .
I am trying to run it with a JAR command

java -cp MyProject-0.1.0.jar ru.com.myproject.Main

Where

ru.com.myproject.Main

there is a main class, because despite the fact that I indicated the main class in
Project properties> Run> Main class it is still not specified in the manifest ...

I get the error:

Exception in thread "main" java.lang.NoClassDefFoundError: com/thetransactioncompany/jsonrpc2/JSONRPC2Parser at ru.com.myproject.<clinit>(Main.java:28) Caused by: java.lang.ClassNotFoundException: com.thetransactioncompany.jsonrpc2.JSONRPC2Parser at java.net.URLClassLoader$1.run(URLClassLoader.java:366) at java.net.URLClassLoader$1.run(URLClassLoader.java:355) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:354) at java.lang.ClassLoader.loadClass(ClassLoader.java:423) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) at java.lang.ClassLoader.loadClass(ClassLoader.java:356) ... 1 more 

Contents of the pom file:

 <?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>ru.com</groupId> <artifactId>MyProject</artifactId> <version>0.1.0</version> <packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>com.thetransactioncompany</groupId> <artifactId>jsonrpc2-base</artifactId> <version>1.38</version> </dependency> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.1</version> <type>jar</type> </dependency> <dependency> <groupId>org.apache.maven.surefire</groupId> <artifactId>maven-surefire-common</artifactId> <version>2.17</version> <type>jar</type> </dependency> </dependencies> </project> 

So how do you build the Maven project in NetBeans so that the manifest file is configured correctly: was the main class and dependencies correctly registered?

Or how to run the assembled Maven project?

1 answer 1

Java cannot find dependencies, in particular jsonrpc2. You can either add their jar files to CLASSPATH, or pack them into one jar. The first can be done both manually and with the help of the maven-dependency-plugin :

 <project> ... <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>2.10</version> <executions> <execution> <id>copy-dependencies</id> <phase>package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/lib</outputDirectory> <overWriteReleases>false</overWriteReleases> <overWriteSnapshots>false</overWriteSnapshots> <overWriteIfNewer>true</overWriteIfNewer> </configuration> </execution> </executions> </plugin> </plugins> </build> ... </project> 

The second using the maven-assembly-plugin :

 <project> ... <build> <plugins> <plugin> <artifactId>maven-assembly-plugin</artifactId> <configuration> <archive> <manifest> <mainClass>fully.qualified.MainClass</mainClass> </manifest> </archive> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> </plugin> </plugins> </build> ... </project> 
  • Here I am interested in how to do this? After all, there is no manifest file in Maven projects - it is generated ... How can I not configure it? - t1nk