简体   繁体   English

Oracle JDBC 项目与 Maven:在 Z581D6381F3F35E4F9D4F7720 命令行中运行良好

[英]Oracle JDBC project with Maven: runs fine in IDE but ClassNotFoundException in command-line

I currently have a class project that consists in making a software in Java using JavaFX and Oracle JDBC. I currently have a class project that consists in making a software in Java using JavaFX and Oracle JDBC. In order to make the project run on the school's computers, we are asked to make it using Java 1.8 and ojdbc6.jar.为了使项目在学校的计算机上运行,我们被要求使用 Java 1.8 和 ojdbc6.jar 来制作它。

I use Maven for this project, and normally I would just have to install ojdbc6 locally, but I need it to work on the teacher's computer, who has the jar file under /opt/oracle/ojdbc6.jar.我在这个项目中使用了 Maven,通常我只需要在本地安装 ojdbc6,但我需要它在老师的计算机上工作,老师的计算机上有 jar 文件,位于 /opt/oracle/ojdbc69998995FCBF4432 下Thus, I added a copy of this ojdbc6.jar inside my project directory, and I imported the dependency in pom.xml the following way:因此,我在我的项目目录中添加了这个ojdbc6.jar的副本,并通过以下方式在pom.xml中导入了依赖项:

<dependency>
    <groupId>com.oracle</groupId>
    <artifactId>ojdbc6</artifactId>
    <version>12.1.0.1.0</version>
    <scope>system</scope>
    <systemPath>${basedir}/ojdbc6.jar</systemPath>
</dependency>

This way, the teacher will be able to compile the project because all it needs is already there.这样,老师就可以编译项目,因为它所需要的一切都已经存在了。

I understood from other posts that this wasn't a good solution, but as the teacher won't install the jar file locally, I don't really know what else to do.我从其他帖子中了解到这不是一个好的解决方案,但是由于老师不会在本地安装 jar 文件,我真的不知道该怎么办。

I am using the following lines to make my program connect to the database:我正在使用以下几行使我的程序连接到数据库:

try {
    Class.forName("oracle.jdbc.OracleDriver");
    connection = DriverManager.getConnection(url, user, passwd);
}
catch /* etc */

The problem is: this works just fine inside IntelliJ, I can make requests to the database and get data, etc. However, when I try to run the project using command-line interface command:问题是:这在 IntelliJ 中工作得很好,我可以向数据库发出请求并获取数据等。但是,当我尝试使用命令行界面命令运行项目时:

mvn clean compile jfx:run

It works fine until I make an action that uses the database, and I get the following exception:在我执行使用数据库的操作之前它工作正常,并且出现以下异常:

java.lang.ClassNotFoundException: oracle.jdbc.OracleDriver
        at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:264)
        at fr.ensimag.equipe3.model.DAO.ConnectionDB.connect(ConnectionDB.java:34)
        at fr.ensimag.equipe3.model.DAO.ConnectionDB.<init>(ConnectionDB.java:29)
        at fr.ensimag.equipe3.model.DAO.ConnectionDB.<clinit>(ConnectionDB.java:12)
        at fr.ensimag.equipe3.model.DAO.UserDAO.get(UserDAO.java:32)
        at fr.ensimag.equipe3.controller.LoginController.connexion(LoginController.java:28)
        at fr.ensimag.equipe3.controller.LoginController.onEnter(LoginController.java:49)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
        at sun.reflect.GeneratedMethodAccessor9.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
        at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
        at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
        at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
        at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
        at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
        at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
        at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
        at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
        at javafx.event.Event.fireEvent(Event.java:198)
        at javafx.scene.Node.fireEvent(Node.java:8411)
        at com.sun.javafx.scene.control.behavior.TextFieldBehavior.fire(TextFieldBehavior.java:179)
        at com.sun.javafx.scene.control.behavior.TextInputControlBehavior.callAction(TextInputControlBehavior.java:178)
        at com.sun.javafx.scene.control.behavior.BehaviorBase.callActionForEvent(BehaviorBase.java:218)
        at com.sun.javafx.scene.control.behavior.TextInputControlBehavior.callActionForEvent(TextInputControlBehavior.java:127)
        at com.sun.javafx.scene.control.behavior.BehaviorBase.lambda$new$74(BehaviorBase
.java:135)
        at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
        at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
        at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
        at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
        at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
        at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
        at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
        at javafx.event.Event.fireEvent(Event.java:198)
        at javafx.scene.Scene$KeyHandler.process(Scene.java:3964)
        at javafx.scene.Scene$KeyHandler.access$1800(Scene.java:3910)
        at javafx.scene.Scene.impl_processKeyEvent(Scene.java:2040)
        at javafx.scene.Scene$ScenePeerListener.keyEvent(Scene.java:2501)
        at com.sun.javafx.tk.quantum.GlassViewEventHandler$KeyEventNotification.run(GlassViewEventHandler.java:217)
        at com.sun.javafx.tk.quantum.GlassViewEventHandler$KeyEventNotification.run(GlassViewEventHandler.java:149)
        at java.security.AccessController.doPrivileged(Native Method)
        at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleKeyEvent$352(GlassViewEventHandler.java:248)
        at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
        at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleKeyEvent(GlassViewEventHandler.java:247)
        at com.sun.glass.ui.View.handleKeyEvent(View.java:546)
        at com.sun.glass.ui.View.notifyKey(View.java:966)
        at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
        at com.sun.glass.ui.gtk.GtkApplication.lambda$null$48(GtkApplication.java:139)
        at java.lang.Thread.run(Thread.java:748)

So it looks like Java can't find my ojdbc6.jar file... I tried to display the classpath used by Maven using:所以看起来 Java 找不到我的ojdbc6.jar文件...我试图显示 Maven 使用的类路径:

mvn dependency:build-classpath

and it turns out ojdbc6.jar is in the classpath, so I really can't understand why the same program works in IntelliJ but not in command-line interface: I would have thought that both the IDE and mvn jfx:run would run the same commands in order to launch the program.事实证明ojdbc6.jar类路径中,所以我真的不明白为什么同一个程序在 IntelliJ 中有效,但在命令行界面中无效:我原以为 IDE 和mvn jfx:run都会运行相同的命令以启动程序。

Thank you for your time if you read this post, I hope I have been clear enough and don't hesitate to tell me if I missed another post that would solve my problem.如果您阅读这篇文章,感谢您抽出宝贵时间,我希望我已经足够清楚,如果我错过了另一篇可以解决我问题的帖子,请随时告诉我。

Oracle JDBC drivers are on Maven central repos now. Oracle JDBC 驱动程序现在在 Maven 中央存储库上。

For Java8 / Oracle 12 you should use:对于 Java8 / Oracle 12,您应该使用:

<dependency>
    <groupId>com.oracle.jdbc</groupId>
    <artifactId>ojdbc8</artifactId>
    <version>12.2.0.1</version>
</dependency>

The correct GAV for 12.1.0.1 is given below.下面给出了 12.1.0.1 的正确 GAV。 Refer to the blog for more details on the versions and the files available.有关可用版本和文件的更多详细信息,请参阅博客

<dependency>
    <groupId>com.oracle.database.jdbc</groupId>
    <artifactId>ojdbc6</artifactId>
    <version>12.1.0.1</version>
</dependency>

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

相关问题 在命令行上运行时出现ClassNotFoundException,但在IntelliJ IDEA上运行良好 - ClassNotFoundException when running on command line but runs fine on IntelliJ IDEA eclipse-maven项目-NoClassDefFoundError从eclipse运行,但从命令行运行良好 - eclipse-maven project - NoClassDefFoundError running from eclipse but runs fine from command line Maven项目在命令行中运行,但不在Eclipse中运行 - Maven project runs in command line but not in Eclipse 从命令行运行Netbeans maven项目? - Run Netbeans maven project from command-line? Maven项目可以在IntelliJ中很好地构建,但不能使用maven命令行 - Maven project builds fine in IntelliJ, but not with maven command line Maven和安装命令行程序 - Maven and installing a command-line program exec-maven-plugin无法找到或加载主类,但输出在命令行上运行良好 - exec-maven-plugin could not find or load main class but output runs fine on the command line Maven 命令行执行错误 ClassNotFoundException - Maven command line execution error ClassNotFoundException Maven项目可以通过maven命令行很好地构建,但是不能使用IntelliJ IDEA 11.1.1 - Maven project builds fine from maven command line, but not with IntelliJ IDEA 11.1.1 Maven 异常 NoClassDefFoundError ClassNotFoundException 但 Maven 依赖存在命令行 - Maven Exception NoClassDefFoundError ClassNotFoundException but Maven Dependency Exists Command Line
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM