This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Mac OS and the fly likes classpath and jdbc Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Engineering » Mac OS
Bookmark "classpath and jdbc" Watch "classpath and jdbc" New topic
Author

classpath and jdbc

Pauline McNamara
Sheriff

Joined: Jan 19, 2001
Posts: 4012
    
    6
Does your first customer win a toaster? Oh yippee, please send it to me at pauline@javaranch.com.
I've just installed mySQL and Connector/J. I was able to play with mySQL successfully, but have had less luck accessing the db via jdbc.
I expanded (with Stuffit Expander) mysql-connector-java-3.0.8-stable.zip at the root of the hard disk. There is now a folder called mysql-connector-java-#491A3Folder.
The jar file, mysql-connector-java-3.0.8-stable-bin.jar, is located one folder down at /mysql-connector-java-#491A3Folder/mysql-connector-java-3.0.8-stable/mysql-connector-java-3.0.8-stable-bin.jar
The java file I'm testing with looks like this:

With the jar in the classpath, I can compile Test.java ok, but running it gives me a class not found exception with the Driver class:
could not load JDBC driver: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
I suspect some kind of classpath problem...

Thanks,
Pauline
[ June 21, 2003: Message edited by: Pauline McNamara ]
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
You might try putting the jar file in the java extensions folder, located in /Library/Java/Extensions I believe.


Rob
SCJP 1.4
michael bradly
Ranch Hand

Joined: Oct 06, 2000
Posts: 112
I'm still working my way through the intricities of jdbc, and even though I have the driver in my $PATH, thusfar the only way I've gotten my programs to work is by java -classpath </usr/local/lib/mysql/mysql-connector...etc...> <progName>. It's annoying , however until I find a simpler means, it works.
Regards, Michael
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61448
    
  67

Hey Michael... yeah PATH won't do anything for you. That's the path that the shell uses to look for command programs; Java ignores it.
Java will look on the path defined by CLASSPATH, as well as in well-known folders (as Rob suggested) for class files -- in addition to those you specify on the command line.
hth,
bear


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
michael bradly
Ranch Hand

Joined: Oct 06, 2000
Posts: 112
Presently I'm experimenting with tcsh. I have set the pathes in my .tcshrc file. Shouldn't this make the pathes available to Java?
Regardless, I'll try what Rob suggested since I'm getting tired of typing out the driver...
thanks, Michael
Originally posted by Bear Bibeault:
Hey Michael... yeah PATH won't do anything for you. That's the path that the shell uses to look for command programs; Java ignores it.
Java will look on the path defined by CLASSPATH, as well as in well-known folders (as Rob suggested) for class files -- in addition to those you specify on the command line.
hth,
bear

[ June 22, 2003: Message edited by: michael bradly ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61448
    
  67

But how did you set the classpath in your login script? Remember PATH isn't the right environment variable to use...
bear
michael bradly
Ranch Hand

Joined: Oct 06, 2000
Posts: 112
I've done a bit of research on it and understand what the difference is now..
I've made a few modifications... now back to figuring out tomcat
Originally posted by Bear Bibeault:
But how did you set the classpath in your login script? Remember PATH isn't the right environment variable to use...
bear
Pauline McNamara
Sheriff

Joined: Jan 19, 2001
Posts: 4012
    
    6
Originally posted by Rob Ross:
You might try putting the jar file in the java extensions folder, located in /Library/Java/Extensions I believe.

Thanks Rob, this seemed to help. (Though I wonder why just having it in the classpath didn't work.) That is, I'm getting a different type of error now.
Now there seems to be a question of permissions...
java.sql.SQLException: General error, message from server: "Access denied for user: '@localhost' to database 'soup'
I'm thinking that this is where a GRANT bla bla comes in, but am confused about the user '@localhost' - no user name there?

Thanks in advance,
Pauline
Pauline McNamara
Sheriff

Joined: Jan 19, 2001
Posts: 4012
    
    6
For whatever it's worth, I figured out the access denied problem, it's not really mac specific.
I had set permissions on the database, but didn't specify a userid in the DriverManager.getConnection() call. Used getConnection( url , userid, userpassword ) and got in.
Pauline McNamara
Sheriff

Joined: Jan 19, 2001
Posts: 4012
    
    6
Going back to my original problem with the class not found exception...
Is there any reason that putting the connector .jar file in the classpath wouldn't work whereas putting it in /Library/Java/Extensions does?
Any advantages one way or the other?
Should I be concerned, or just stick with what worked?
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
The extensions folder is a well-known place that all java JVMs look for libraries; this folder is actually always implicitly in your classpath, that's why this works. Different types of libraries might warrant being placed here, but keep in mind they're globally available to anyone using that JVM; so if you have different versions of the libs or you want to restrict access to a specific user, putting them in that folder might not be the best solution.
You didn't include your actual script to launch your program; can you do that so I can check it? Also, can you diagram out your folder structure for me, like

etc? Also, I don't know if it's an issue but those directory names sure look funky to me. What's with the -#491A3 in the name?
Pauline McNamara
Sheriff

Joined: Jan 19, 2001
Posts: 4012
    
    6
Thanks Rob.
Yeah, Stuffit Expander had chopped off part of the name, too, I'll check what it's called when it gets unzipped from Windows at work, uh, Monday. That's also when I'll be able to get back to you with that other stuff, the weekend is quickly approaching and I'm giving my tibook a couple days off.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61448
    
  67

Just a note: When Stuffit Expander annoys me (often) I resort to using the java jar command to manipulate zip files.
hth,
bear
Pauline McNamara
Sheriff

Joined: Jan 19, 2001
Posts: 4012
    
    6
Ooh, that's definitely a weird folder name. I'll have to try again using jar (thanks Bear) or see if ZipIt can handle it more cleanly...
Pauline McNamara
Sheriff

Joined: Jan 19, 2001
Posts: 4012
    
    6
That was it, got it working by just unzipping with jar and replacing that goofy folder. Thanks.

Next question..
When using jar I ended up waiting longer than necessary, I think, because it didn't give me the command line again; I did a ctrl + . to stop it, then noticed the folder had been created (it might have already been there, don't know). Is this the normal procedure?
Oh, yeah, and when do I get my toaster?
[ June 30, 2003: Message edited by: Pauline McNamara ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61448
    
  67

Hmmm, no I've never had a problem with the jar command not terminating. Does this happen every time you use it?
bear
Pauline McNamara
Sheriff

Joined: Jan 19, 2001
Posts: 4012
    
    6
Yes, er, that is, the one and only time I used it. I'll try to reproduce it.
...
Just used it for a different zip file, and it didn't terminate and didn't unzip either...
Is my syntax off?
jar -x path/to/file.zip
...
Back again, after cd'ing to the directory the file is in and using
jar xf file.zip
This terminated right away, pulled out the files that were in the .zip, but didn't create a folder - the files showed up in the same directory as the zip file.
So if I create the folder myself and do the jar from there I'm all set (almost as good as Windows yippee
[ July 01, 2003: Message edited by: Pauline McNamara ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61448
    
  67

Ah yes, as you found out, if you leave off the 'f', the jar command (like many other unix commands) will wait for you to enter the input at the console.
As for not creating the folder hierarchy, that's not a problem I've had. If you use 'jar tf myfile.zip' it will show you what is in the zip file so you can check if the folders are as you expect within the file.
Using 'jar xvf myfile.zip' to unpack will show a list of folders/files as it is unpacking.
hth,
bear
[ July 01, 2003: Message edited by: Bear Bibeault ]
Pauline McNamara
Sheriff

Joined: Jan 19, 2001
Posts: 4012
    
    6
thanks, Bear.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: classpath and jdbc