0

I have converted a spring project from Ant to Maven which has to be executed from Command Line as it required args in main method, dependencies in pom.xml mentioned below

<properties>
        <org.springframework.version>2.5.5</org.springframework.version>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>xerces</groupId>
            <artifactId>xerces</artifactId>
            <version>2.4.0</version>
        </dependency>
        <dependency>
            <groupId>com.ibm.mq</groupId>
            <artifactId>com.ibm.mq.allclient</artifactId>
            <version>9.2.2.0</version>
        </dependency>
    </dependencies>

Previously it was created by Ant build, hence libraries are imported in lib folder and if we look at the final-project jar after build, we could see all the necessary jars would be present in that main Jar (myApp.jar) file, so I could execute that jar using java -jar myApp.jar and it worked well!

But in Maven, we have defied all the dependencies in pom.xml and after clean & build done, if I run my myApp.jar using the same command, it is saying below issues

Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: com/ibm/mq/MQException
        at java.lang.Class.getDeclaredMethods0(Native Method)
        at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
        at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
        at java.lang.Class.getMethod0(Class.java:3018)
        at java.lang.Class.getMethod(Class.java:1784)
        at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
        at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: com.ibm.mq.MQException
        at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        ... 7 more

Because those dependency jars are not present in myApp.jar, So, Is there anyway I can run my program? or I cannot go with Maven build tool for CLI kind of project since dependency jars wont be available in our main .jar?

RenceAbi
  • 522
  • 2
  • 11
  • 26
  • You've missed some IBM MQ dependencies. Look into https://stackoverflow.com/questions/13881594/java-lang-noclassdeffounderror-com-ibm-mq-mqexception https://mvnrepository.com/artifact/com.ibm.mq Your ant script works because you have web spehere system properties installed, maven don't use them when building classpath. – Victor Gubin Apr 28 '22 at 13:21
  • You can build a fat jar. https://jenkov.com/tutorials/maven/maven-build-fat-jar.html – Oliver Apr 28 '22 at 13:59
  • Thanks @Oliver, I could understand and implement `maven-assembly-plugin` to add dependency in my output jar – RenceAbi May 02 '22 at 11:02
  • What don't you understand? Look at the first code snippet on the page I linked. You could literally copy+paste the plugin code to your POM and run `mvn clean package`. – Oliver May 02 '22 at 16:25
  • Yea, I mean, I do understand, thanks for that link, request you to post in answer so that I can accept @Oliver – RenceAbi May 03 '22 at 01:16

0 Answers0