Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ClassNotFoundException when using external jar

I'm using IntelliJ Idea. I've built my application and created it as a .jar file. This program uses an external .jar file for its database driver.

When I run the program from the IDE, it works fine. When I try to run my .jar file outside of the IDE, it reports the following exception:

Exception in thread "main" java.lang.NoClassDefFoundError: com/microsoft/sqlserver/jdbc/SQLServerException
        at ca.vdts.dbupdate.Main.main(Main.java:10)
Caused by: java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerException
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)

The directory of the sqljdbc42.jar file is in the manifest. I'm on Windows, and I'd like to run this by clicking on the '.jar' file. The application .jar file and the sqljdbc42.jar files are both in the same directory. On the command line, executing...

C:\Users\admin\IdeaProjects\DBUpdate\out\artifacts\DBUpdate>java -classpath .\sqljdbc42.jar;DBUpdate.jar -jar DBUpdate.jar

... results in the same error:

Exception in thread "main" java.lang.NoClassDefFoundError: com/microsoft/sqlserver/jdbc/SQLServerException
        at ca.vdts.dbupdate.Main.main(Main.java:10)
Caused by: java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerException
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 1 more
like image 908
Michael Teasdale Avatar asked Oct 02 '15 17:10

Michael Teasdale


People also ask

What are some common examples of classnotfoundexception in Java?

Trying to load the JDBC drivers using Class.forName and not adding the jar file in the path of the class is one of the common examples of ClassNotFoundExceptionin Java. Consider the below program to demonstrate ClassNotFoundException in Java: The output of the above program is as shown in the snapshot below:

How to resolve a Java classnotfoundexception in MySQL?

Since the MySQL JDBC driver JAR file is not present in the classpath, running the above code leads to a java.lang.ClassNotFoundException: To fix the Java exception, the mysql-connector JAR should be included in the application classpath. The following steps should be followed to resolve a ClassNotFoundException in Java:

What is classnotfoundexception class_name at location?

java.lang.ClassNotFoundException:Class_name at location When a class loader is not able to find the class in the path of the class specified while the application is trying to load the class, that is when the ClassNotFoundExceptionoccurs in Java.

What is a JNI classnotfoundexception?

ClassNotFoundException (String): A new ClassNotFoundException is constructed, which includes the current trace of the stack and the message in detail, specified. ClassNotFoundException (IntPtr, JniHandleOwnership): During the creation of JNI objects, while managing their representations, this constructor is used, and the runtime calls it.


1 Answers

Try running your application with:

java -classpath sqljdbc42.jar:Application.jar -jar Application.jar

Replace the : with a ; under Windows.

like image 178
Tripp Kinetics Avatar answered Oct 08 '22 11:10

Tripp Kinetics