简体   繁体   English

使用外部jar时ClassNotFoundException

[英]ClassNotFoundException when using external jar

I'm using IntelliJ Idea . 我正在使用IntelliJ Idea I've built my application and created it as a .jar file. 我已经构建了我的应用程序并将其创建为.jar文件。 This program uses an external .jar file for its database driver. 该程序使用外部.jar文件作为其数据库驱动程序。

When I run the program from the IDE, it works fine. 当我从IDE运行程序时,它工作正常。 When I try to run my .jar file outside of the IDE, it reports the following exception: 当我尝试在IDE外部运行.jar文件时,它会报告以下异常:

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. sqljdbc42.jar文件的目录位于清单中。 I'm on Windows, and I'd like to run this by clicking on the '.jar' file. 我在Windows上,我想点击“.jar”文件来运行它。 The application .jar file and the sqljdbc42.jar files are both in the same directory. 应用程序.jar文件和sqljdbc42.jar文件都在同一目录中。 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

Try running your application with: 尝试运行您的应用程序:

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

Replace the : with a ; 替换:; under Windows. 在Windows下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM