简体   繁体   中英

Java: Setting up classpath with Derby

Apparently,this is a very common problem, but I'm just not understanding any of the solutions that I'm finding online. It seems like most of what I'm able to find online is incomplete or irrelevant.

I'm trying to run an example from "Java for Dummies," chapter 17, listing 17-1 (which can be found here , but the code itself isn't the issue).

It uses the Derby jdbc driver, with these strings:

 org.apache.derby.jdbc.EmbeddedDriver
 jdbc:derby:AccountDatabase;create=true

Unfortunately, the book only describes how to use it in Eclipse. I can't get Eclipse to compile anything , so I'm trying to do it with a text editor and compiling through bash. javac doesn't throw any errors, but java throws the error

java.lang.ClassNotFoundException: org.apache.derby.jdbc.EmbeddedDriver

Apparently, this is very common, and I'm seeing a lot of solutions listed to set the classpath to Derby.jar using the -classpath option (not sure if that's for javac or java or both), but I have had zero luck in finding out where this jar file actually is. (I'm using Ubuntu, if that's relevant.)

This page is often referenced, but the /usr/local directory doesn't have any directory with jdk in it.

I feel like I'm missing something really basic.

Update: It might be best if I copy the commands I'm using in bash actually are. This is the script that I'm using, partly based on what Bryan Pendleton wrote below:

javac -cp derbyrun.jar:. CreateTable.java
java -cp derbyrun.jar:. CreateTable

This is what throws the "ClassNotFoundException."

Also, if it's relevant, "locate derbyrun" returns nothing.

I was finally able to figure out the solution, and it's worth noting that Bryan's answer did help.

I was able to find the derby.jar file using the Catfish search program for Linux (Caja's search function couldn't find it, so I know to not use that anymore). It was in the /usr/share/java directory. These were the commands that I put in the bash script that didn't throw any errors and created the database:

javac CreateTable.java
java -cp /usr/share/java/derby.jar:. CreateTable

I'm guessing that for programs that I'd want to share, I'd want to include the derby.jar in the jar file. I'll cross that bridge when I come to it-- Right now, I just wanted to know how to use the classpath.

The EmbeddedDriver is found in derby.jar , so simply put derby.jar in your CLASSPATH.

Derby has a number of jars, for various different configurations (embedded, client-server, command-line tools, network server, etc.). Since it is often confusing to remember which set of jars goes with which configuration, there is an umbrella jar file named derbyrun.jar , put derbyrun.jar into your CLASSPATH and it will include the jars for all of those configurations, so you don't have to keep swapping your CLASSPATH back and forth.

For an alternate tutorial about the basic aspects of Derby, try: https://db.apache.org/derby/docs/10.12/getstart/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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