java - 为什么编辑 Manifest 文件提供了 CLASSPATH 没有的解决方案

标签 java classpath manifest

我正在编写一个简短的实用程序,用于在远程 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/

相关文章:

java - 如何设置java2ws类路径?

java - 有条件 Thymeleaf#aggregates.sum

c# - 在 VB6 list 中包含 .NET 程序集?

java - 如何在 java list 类路径中使用正则表达式?

Java - 创建以字符串为名称的类的实例

java - 格式化 TextWatcher android

java - 如何在没有 JDK 的情况下从 tools.jar 使用 JavaCompiler

java - 如何引用Hadoop上hdfs中存储的依赖项(jar)?

命令行中的 Java NoClassDefFoundError

delphi - 替换 .manifest 文件的命令行工具