gpt4 book ai didi

java - 如何从 Mac 终端运行 JDBC 代码?

转载 作者:行者123 更新时间:2023-12-02 13:29:57 36 4
gpt4 key购买 nike

I realize this is a duplicate question, but there was never an answer chosen for the one that I've seen and I've tried what they recommended and it didn't work. I am brand new to compiling and running Java from terminal. I always use eclipse. When using eclipse, I have no problem connecting to my database and running my code. I've made a classpathby clicking on my Java project and then going to Properties then Build Path and added my MySQl jar. However, when trying to run my code from the terminal I get the error

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at Database.createConnection(Database.java:31)
at Database.verifiedMember(Database.java:876)
at Main.main(Main.java:45)

I've compiled my program by doing: javac *.java -cp /Users/Administrator/Desktop/mysql-connector-java-5.1.40/mysql-connector-java-5.1.40-bin.jar

Running it by: java Main -cp /Users/Administrator/Desktop/mysql-connector-java-5.1.40/mysql-connector-java-5.1.40-bin.jar

I've also tried just doing javac *.java and java Main But I get the same results.

What do I need to do?

最佳答案

通过终端执行程序的语法是

java [-选项] 类 [参数...]

因此,您需要在类名之前提供类路径。按照您尝试的方式,-cp 和 jarpath 将被视为程序参数。

它应该如下所示

java -cp /Users/Administrator/Desktop/mysql-connector-java-5.1.40/mysql-connector-java-5.1.40-bin.jar:.: Main

这应该有效

关于java - 如何从 Mac 终端运行 JDBC 代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43219411/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com