wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes java -jar [error] unable to access jarfile postgresql-8.4-702.jdbc.3.jar Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "java -jar [error] unable to access jarfile postgresql-8.4-702.jdbc.3.jar" Watch "java -jar [error] unable to access jarfile postgresql-8.4-702.jdbc.3.jar" New topic
Author

java -jar [error] unable to access jarfile postgresql-8.4-702.jdbc.3.jar

Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 660

I cannot seem to load the jar files publlished by Postgre, and, as shown in the second part of this question, I have, thus a problem Java not being able to load and connect the database driver, cannot connect.

1. Using java -jar to load the .jar file
C:\Program Files\PostgreSQL>cd pgJDBC
C:\Program Files\PostgreSQL\pgJDBC>dir
Volume in drive C has no label.
Volume Serial Number is D80F-8634

Directory of C:\Program Files\PostgreSQL\pgJDBC

09/07/2011 15:42 <DIR> .
09/07/2011 15:42 <DIR> ..
03/04/2011 23:22 502,118 postgresql-8.4-702.jdbc3.jar
03/04/2011 23:22 539,510 postgresql-8.4-702.jdbc4.jar
09/07/2011 15:42 <DIR> scripts
09/07/2011 15:42 5,759,102 uninstall-pgjdbc.exe
3 File(s) 6,800,730 bytes
3 Dir(s) 37,261,746,176 bytes free

C:\Program Files\PostgreSQL\pgJDBC>java -jar postgresql-8.4-702.jdbc.3.jar
Unable to access jarfile postgresql-8.4-702.jdbc.3.jar

C:\Program Files\PostgreSQL\pgJDBC>java -jar postgresql-8.4-702.jdbc.4.jar
Unable to access jarfile postgresql-8.4-702.jdbc.4.jar

C:\Program Files\PostgreSQL\pgJDBC>echo %CLASSPATH%
.;.;C:\PROGRA~1\JMF21~1.1E\lib\sound.jar;C:\PROGRA~1\JMF21~1.1E\lib\jmf.jar;C:\P
ROGRA~1\JMF21~1.1E\lib;C:\Program Files\Java\external_jars\junit4.9b2\junit4.9b2
\junit-4.9b2.jar;C:\Program Files\PostgreSQL\pgJDBC;

2. No suitable driver found for jdbc:postgresql
Upon logging to SQL shell (PostGreSQL command line for managing the database) the following is being displayed:
Server [localhost]:
Database [postgres]:
Port [5432]:
Username [postgres]:
psql (9.0.4)
WARNING: Console code page (437) differs from Windows code page (1252)
8-bit characters might not work correctly. See psql reference
page "Notes for Windows users" for details.
Type "help" for help.

Where do I find the mentioned article?

2. When running a test program, sourced from Core Java Volume 2 (7th Edition), the following error is being displayed:
java.sql.SQLException: No suitable driver found for jdbc:postgresql:COREJAVA;cre
ate=true
at java.sql.DriverManager.getConnection(DriverManager.java:602)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
at TestDB.getConnection(TestDB.java:82)
at TestDB.runTest(TestDB.java:43)
at TestDB.main(TestDB.java:20)

In order to enable JDK to connect to the library files, I copied over postgresql-8.4-702.jdbc3.jar, and,
postgresql-8.4-702.jdbc4.jar to C:\Program Files\Java\jre6\lib\ext.

It was assumed that the file naming indicates type 3 and type 4 respectively, and, I was intending to use type 4, since it is more efficient to use a library that translates Java to the database language for Postgre:

postgresql-8.4-702.jdbc3.jar
A type 3 driver is a pure Java client library that uses a database-independent protocol to communicate database requests to a server component, which then translates the requests into a database-specific protocol. This can simplify deployment since the database-dependent code is located only on the server.

postgresql-8.4-702.jdbc4.jar

A type 4 driver is a pure Java library that translates JDBC requests directly to a database-specific protocol.



database.properties
dbc.drivers=org.postgresql.Driver
jdbc.url=jdbc:postgresql:COREJAVA;create=true
jdbc.username=postgre
jdbc.password=



Jon
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14116
    
  16

Jon Camilleri wrote: Directory of C:\Program Files\PostgreSQL\pgJDBC

09/07/2011 15:42 <DIR> .
09/07/2011 15:42 <DIR> ..
03/04/2011 23:22 502,118 postgresql-8.4-702.jdbc3.jar
03/04/2011 23:22 539,510 postgresql-8.4-702.jdbc4.jar
09/07/2011 15:42 <DIR> scripts
09/07/2011 15:42 5,759,102 uninstall-pgjdbc.exe
3 File(s) 6,800,730 bytes
3 Dir(s) 37,261,746,176 bytes free

C:\Program Files\PostgreSQL\pgJDBC>java -jar postgresql-8.4-702.jdbc.3.jar
Unable to access jarfile postgresql-8.4-702.jdbc.3.jar

C:\Program Files\PostgreSQL\pgJDBC>java -jar postgresql-8.4-702.jdbc.4.jar
Unable to access jarfile postgresql-8.4-702.jdbc.4.jar

Look closely at the filenames you typed. You made a mistake.

Also, JDBC drivers are most likely not executable JAR files that you can run with the java -jar command (why are you trying to do that?). Just put the JAR file in your classpath when you run your own program that needs it. Do not put these JAR files in your jre6\lib\ext directory.

Run your program with a command like this:

java -cp postgresql-8.4-702.jdbc4.jar;. TestDB

The 3 and 4 in the names of the JAR files most likely do not have anything to do with JDBC Type 3 and 4 drivers. It is most likely a driver for JDBC version 3 (Java 5 and older) or JDBC version 4 (Java 6 and newer).


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 660

Jesper de Jong wrote:Directory of C:\Program Files\PostgreSQL\pgJDBC
Look closely at the filenames you typed. You made a mistake.

Noted, sorry, but in any case I am getting the following error now:

C:\Program Files\PostgreSQL\pgJDBC>java -jar postgresql-8.4-702.jdbc3.jar
Failed to load Main-Class manifest attribute from
postgresql-8.4-702.jdbc3.jar


C:\Program Files\PostgreSQL\pgJDBC>java -jar postgresql-8.4-702.jdbc4.jar
Failed to load Main-Class manifest attribute from
postgresql-8.4-702.jdbc4.jar


Jesper de Jong wrote:
Also, JDBC drivers are most likely not executable JAR files that you can run with the java -jar command (why are you trying to do that?). Just put the JAR file in your classpath when you run your own program that needs it. Do not put these JAR files in your jre6\lib\ext directory.

Run your program with a command like this:

java -cp postgresql-8.4-702.jdbc4.jar;. TestDB

The 3 and 4 in the names of the JAR files most likely do not have anything to do with JDBC Type 3 and 4 drivers. It is most likely a driver for JDBC version 3 (Java 5 and older) or JDBC version 4 (Java 6 and newer).


Thanks but I don't read any good news:


C:\Program Files\PostgreSQL\pgJDBC>echo %CLASSPATH%
.;.;C:\PROGRA~1\JMF21~1.1E\lib\sound.jar;C:\PROGRA~1\JMF21~1.1E\lib\jmf.jar;C:\P
ROGRA~1\JMF21~1.1E\lib;C:\Program Files\Java\external_jars\junit4.9b2\junit4.9b2
\junit-4.9b2.jar;C:\Program Files\PostgreSQL\pgJDBC;

... change directory to...

>cd TestDB
>javac TestDB.java

>java -cp postgresql-8.4-702.jdbc4.jar; .TestDB
Exception in thread "main" java.lang.NoClassDefFoundError: /TestDB
Caused by: java.lang.ClassNotFoundException: .TestDB
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: .TestDB. Program will exit.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14116
    
  16

Jon Camilleri wrote:Yep, sorry, but in any case I am getting the following error now:

C:\Program Files\PostgreSQL\pgJDBC>java -jar postgresql-8.4-702.jdbc3.jar
Failed to load Main-Class manifest attribute from
postgresql-8.4-702.jdbc3.jar


Do not try to run the JAR files with java -jar. These JAR files are not executable JAR files that you can run like this.

Jon Camilleri wrote:
Thanks but I don't read any good news:
...
>java -cp postgresql-8.4-702.jdbc4.jar; .TestDB
Exception in thread "main" java.lang.NoClassDefFoundError: /TestDB
Caused by: java.lang.ClassNotFoundException: .TestDB

Type the command exactly as I wrote:

java -cp postgresql-8.4-702.jdbc4.jar;. TestDB

That's java, -cp, name of the jar, semi-colon, dot, without spaces between the jar filename, semi-colon and dot, then a space, then the classname.

Please make an effort to understand what the commands you are typing and the error messages mean.
Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 660

Jesper de Jong wrote:
java -cp postgresql-8.4-702.jdbc4.jar;. TestDB

That's java, -cp, name of the jar, semi-colon, dot, without spaces between the jar filename, semi-colon and dot, then a space, then the classname.

Please make an effort to understand what the commands you are typing and the error messages mean.


Yep, sorry, but in any case I am getting the following error now:

I understand that I am asking java to do a class search of directories and zip/jar files, however, even though my eyes might be sore, I am getting an error, having tried it with a different program now:

>echo %CLASSPATH%
.;.;C:\PROGRA~1\JMF21~1.1E\lib\sound.jar;C:\PROGRA~1\JMF21~1.1E\lib\jmf.jar;C:\P
ROGRA~1\JMF21~1.1E\lib;C:\Program Files\Java\external_jars\junit4.9b2\junit4.9b2
\junit-4.9b2.jar;C:\Program Files\PostgreSQL\pgJDBC;

>java -cp postgresql-8.4-702.jdbc4.jar; .TestDB
Exception in thread "main" java.lang.NoClassDefFoundError: /TestDB
Caused by: java.lang.ClassNotFoundException: .TestDB
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: .TestDB. Program will exit.

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41621
    
  55
You made the exact same mistake as before - the one that Jesper told you how to fix; please pay attention to every single detail of what he wrote.


Ping & DNS - my free Android networking tools app
Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 660

Ulf Dittmer wrote:You made the exact same mistake as before - the one that Jesper told you how to fix; please pay attention to every single detail of what he wrote.


Thanks it's working now

>edit database.properties

>java -cp postgresql-8.4-702.jdbc4.jar;. TestDB
Hello, World!

john price
Ranch Hand

Joined: Feb 24, 2011
Posts: 495

Make a metafile (with a .mf extension) :
BlahBlahBlah.mf (The extra space NEEDS to be there) :

Cmd Prompt (or whatever you use) :


Then, you can run the java -jar command on NameofJar.jar.

Hope this helps,
cc11rocks


“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” (Mosher's Law of Software Engineering)
“If debugging is the process of removing bugs, then programming must be the process of putting them in.” (Edsger Dijkstra)
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14116
    
  16

John, ok, that is what you need to do to make an executable JAR file.

But Jon is trying to run a JDBC driver library JAR as if it is an executable JAR file. Trying to do that makes no sense.
Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 660

Jesper de Jong wrote:John, ok, that is what you need to do to make an executable JAR file.

But Jon is trying to run a JDBC driver library JAR as if it is an executable JAR file. Trying to do that makes no sense.


Excuse me, I was only following a book example; it would have been appreciated if you explained the reason for using the java -jar command. I was only trying to "tell Java that the JAR to use to run TestDB.class is the driver (postgresql-8.4-702.jdbc4.jar) provided by PostGre.

Why is a manifest file required for a driver anyway?

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14116
    
  16

Jon Camilleri wrote:Excuse me, I was only following a book example; it would have been appreciated if you explained the reason for using the java -jar command. I was only trying to "tell Java that the JAR to use to run TestDB.class is the driver (postgresql-8.4-702.jdbc4.jar) provided by PostGre.

Trying to run the JDBC driver library JAR as if it is an executable JAR (using the java -jar command) is not how you tell Java that this is the JDBC driver to use for your program. I'm sure the book did not tell you to do this. What you should do is put the JAR in your classpath, as I've explained in one of my posts above. Don't run commands without understanding what you're doing - that leads to cargo cult programming, where you're typing in magical commands that you don't understand, believing that the computer will understand what you mean.

Lookup the documentation of the java command. It explains exactly what the -jar option does.

Jon Camilleri wrote:Why is a manifest file required for a driver anyway?

It isn't and it doesn't have a manifest file.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: java -jar [error] unable to access jarfile postgresql-8.4-702.jdbc.3.jar