ClassNotFoundException upon running JAR, no errors while running in IntelliJ IDEA

拟墨画扇 提交于 2019-12-04 00:51:32

Use java -cp instead of java -jar and put all of you dependencies jars to classpath.

Another way is pack all of dependencies to single jar, that allowing you to run application using java -jar.

EDIT:

In Java *.jar file contains a bulk of classes. When you build your own app, typically, result jar file contains only your classes, but still have to load classes from external libraries you use (so-called dependencies).

It can be done two different ways:

  1. You create a folder for your application, for example, called lib and place your application jar and all dependencies into. Then you run application using java -cp lib:/\* com.company.Main or (thanks @NilsH, I miss this variant) you make MANIFEST.MF file and specify Main-Class and Classpath attributes inside as described here

  2. You use special tool (like maven-dependency-plugin if you use maven for build) to pack all classes, either your own, either external to single jar. You got one huge file and can run it using java -jar cliTest.jar.

Generally, first approach is preferred and using a MANIFEST.MF file is a good form.

Well I should have built the JAR without the sqljdbc4.jar embedded.

The second thing I should have run the command like so :

java -classpath sqljdbc4.jar;cliTest.jar com.company.Main

.. and then all worked!

Well if you are using maven you could use maven-shade-plugin to include dependent jar's in executable jar the snippet of the pom.xml is given below.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.4.3</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">                                      <mainClass>com.main.class.YouMainClass</mainClass>
                                </transformer>
                            </transformers>
                            <filters>
                                <filter>
                                    <artifact>*:*</artifact>
                                    <excludes>
                                        <exclude>META-INF/*.SF</exclude>
                                        <exclude>META-INF/*.DSA</exclude>
                                        <exclude>META-INF/*.RSA</exclude>
                                    </excludes>
                                </filter>
                            </filters>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!