• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 664
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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=


 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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).
 
Jon Camilleri
Ranch Hand
Posts: 664
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 664
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Jon Camilleri
Ranch Hand
Posts: 664
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!

 
Ranch Hand
Posts: 495
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 664
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic