1
votes
  1. Maven builds properly

    # mvn archetype:generate -DgroupId=com.help.idea -DartifactId=client -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 -DinteractiveMode=false

    # mvn install:install-file -Dfile=rs2xml.jar -DgroupId=net.proteanit.sql -DartifactId=rs2xml -Dversion=1.0 -Dpackaging=jar

    # mvn package

  2. But while running the jar it gives following error

    java -jar target/client-1.0-SNAPSHOT.jar
    
    Exception in thread "main" java.lang.NoClassDefFoundError: org/bouncycastle/jce/provider/BouncyCastleProvider
        at com.help.idea.authen.ClientMain.main(ClientMain.java:76)
    Caused by: java.lang.ClassNotFoundException: org.bouncycastle.jce.provider.BouncyCastleProvider
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
    

I have following pom.xml

<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.help.idea</groupId>
  <artifactId>client</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>client</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

<!-- https://mvnrepository.com/artifact/com.jgoodies/jgoodies-common -->
<dependency>
    <groupId>com.jgoodies</groupId>
    <artifactId>jgoodies-common</artifactId>
    <version>1.8.1</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.jgoodies/forms -->
<dependency>
    <groupId>com.jgoodies</groupId>
    <artifactId>forms</artifactId>
    <version>1.2.1</version>
</dependency>



<!-- https://mvnrepository.com/artifact/org.bouncycastle/bcprov-jdk15on -->
<dependency>
    <groupId>org.bouncycastle</groupId>
    <artifactId>bcprov-jdk15on</artifactId>
    <version>1.65</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-dbutils/commons-dbutils -->
<dependency>
    <groupId>commons-dbutils</groupId>
    <artifactId>commons-dbutils</artifactId>
    <version>1.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/xml-apis/xml-apis -->
<dependency>
  <build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->

        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->

        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>

        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
          <configuration>
                <release>11</release>
          </configuration>
        </plugin>

        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>

        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
            <configuration>
        <archive>
          <manifest>
            <addClasspath>true</addClasspath>
            <classpathPrefix>lib/</classpathPrefix>
            <mainClass>com.help.idea.authen.ClientMain</mainClass>
          </manifest>
        </archive>
      </configuration>
        </plugin>

        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>

        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
        <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->

        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>3.7.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-project-info-reports-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>

        </plugins>
    </pluginManagement>
  </build>
</project>


Tried many suggestion from google but could not get it right. Thanks in advance.

2
can you add the full pom, or at least the context around the dependenciesDarren Forsythe
How is your jar packaged before being run through the command line?tmarwen
Can you add scope to your dependencies. This should help you.<scope>provided</scope>Kunal Vohra
updated pom.xml and the steps i followed.Knowledge

2 Answers

0
votes

welcome to StackOverflow. 👋 The error message tells you that your class ClientMain can't access the class BouncyCastleProvider. A likely cause for this is that the Java Virtual Machine (JVM) that you launched doesn't see the JAR that contains that class. Such JARs would have to be mentioned with the --class-path option.

Looking at your launch command, you can see that there's no class path being specified. One way to fix this, is to enumerate all your direct and transitive dependencies with the --class-path option (although that's a lot of work).

On the other hand, it is possible that this project created a so-called fat JAR, which contains all dependencies. That one, you could launch with just such a short command. Have a look into the target folder and see whether there's another JAR that you can use to launch. Probably something with -jar-with-dependencies in its name (don't launch anything with sources or javadoc in their name, that's pointless).

If this doesn't fix your problem, please follow Darren's comment and show us the full pom, so we can see the entire context.

0
votes

I appreciate all the responses. I need to learn a lot. you may please refine my answer. I now created a directory "src/main/resources" and put org/bouncycastle/ in there. Now things are working as expected. But things should have worked directly with maven build.