我正在编写一个简短的实用程序,用于在远程 Oracle 实例上执行一项简短的任务。该实用程序使用 ojdbc6.jar 中的 Oracle jdbc 驱动程序。
Oracle jar 已正确包含在我的类路径中。当我将项目导出到 jar 并使用“java -jar”在命令行上运行 jar 时,我得到:
Exception in thread "main" java.lang.ClassNotFoundException: oracle.jdbc.OracleDriver
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
at java.lang.Class.forName0(Native Method)
将 Oracle jar 移动到与我的可运行 jar 文件相同的目录后,我更改了我的 list 文件以将 Oracle jar 包含在类路径中。这解决了问题,我的 jar 运行良好。
谁能解释为什么在我的类路径中包含 Oracle jar 不能解决问题,而编辑 list 文件却能解决问题?
谢谢!
最佳答案
来自 java -jar
命令的 Sun (Oracle) 文档:
Execute a program encapsulated in a JAR file. The first argument is the name of a JAR file instead of a startup class name. In order for this option to work, the manifest of the JAR file must contain a line of the form Main-Class: classname. Here, classname identifies the class having the public static void main(String[] args) method that serves as your application's starting point. See the Jar tool reference page and the Jar trail of the Java Tutorial for information about working with Jar files and Jar-file manifests. When you use this option, the JAR file is the source of all user classes, and other user class path settings are ignored.
关于java - 为什么编辑 Manifest 文件提供了 CLASSPATH 没有的解决方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8129500/