0

Issue Resolved !!!

I was reading all db details from a property file that property file i was reading directly by refrering file name in netbeans but in Jar it was not able to find that file. so I used below code:

System.getProperty("user.dir")+"/config.properties"

I have created a MDI project which works absolutely fine from IDE netbeans in debug/ run mode but When I build it and access it. It shows error: No suitable driver found jdbc://postgresql

I verified jdbc jar file for postgreSQL and its available in dist\lib location I have added jar file to library of my project

I tried accessing by Double clicking on myApp.jar build app and also accessed it via command prompt

>java -jar "myApp.jar" 

It's initializing correctly but produces error when its trying to connect the database. I am using 1.7 version of java and 8.1 version of netbeans IDE.

I am using below code to connect :

String Host = "localhost";
String Port = 5432;
String DB = "test";
String dbURL = "jdbc:postgresql://" + Host+ ":" + Port + "/" + DB;
user = "test";
pass = "pass";
try {
    Class.forName("org.postgresql.Driver");
    con = DriverManager.getConnection(dbURL, user, pass);
}catch (ClassNotFoundException | SQLException e) {
    JOptionPane.showMessageDialog(null,e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE); 

}

myApp.jar manifest file have below lines :

 Manifest-Version: 1.0
 Ant-Version: Apache Ant 1.9.4
 Created-By: 1.7.0_71-b14 (Oracle Corporation)
 Class-Path: lib/postgresql-9.2-1003.jdbc3.jar
 X-COMMENT: Main-Class will be added automatically by build
 Main-Class: myapp.FrmMain

I am running it from same original location:
NetBeansProjects\myApp\dist


I don't think this is duplicate of this as per ( Gord Thompson, Jeen Broekstra, Mike Laren, EdChum, pedrouan)

I as solution for that question is to add dependent jar file in library and I have already done that Even I can see dependent jar file in my \dist\lib\ folder as I had added that jdbc in library of netbeans project. Please find below that details of dist directory :

  C:\Users\usernm>cd C:\Users\usernm\Documents\NetBeansProjects\my_App\dist

  C:\Users\usernm\Documents\NetBeansProjects\my_App\dist>dir
   Volume in drive C has no label.

   Directory of C:\Users\usernm\Documents\NetBeansProjects\my_App\dist

  10/07/2016  01:51 PM    <DIR>          .
  10/07/2016  01:51 PM    <DIR>          ..
  10/07/2016  01:51 PM    <DIR>          lib
  10/07/2016  01:51 PM           108,932 my_App.jar
  10/07/2016  01:51 PM             1,323 README.TXT
                 2 File(s)        110,255 bytes
                 3 Dir(s)  121,849,483,264 bytes free

  C:\Users\usernm\Documents\NetBeansProjects\my_App\dist>cd lib

  C:\Users\usernm\Documents\NetBeansProjects\my_App\dist\lib>dir
   Volume in drive C has no label.

   Directory of C:\Users\usernm\Documents\NetBeansProjects\my_App\dist\lib

  10/07/2016  01:51 PM    <DIR>          .
  10/07/2016  01:51 PM    <DIR>          ..
  10/07/2016  01:51 PM           525,205 postgresql-9.2-1003.jdbc3.jar
                1 File(s)        525,205 bytes
                2 Dir(s)  121,848,958,976 bytes free

C:\Users\usernm\Documents\NetBeansProjects\my_App\dist\lib>
Community
  • 1
  • 1
  • you should add jar file to library. – Madhawa Priyashantha Oct 05 '16 at 14:27
  • Thanks @fast, I already added below mentioned jar file to library. I can see that in projects Pane in IDE: postgresql-9.2-1003.jdbc3.jar – Rajesh Mhatre Oct 05 '16 at 14:29
  • when you open up the jar file, can you see the required jar in there? Netbeans manages this for you, but when you package up the jar, it needs to know where the other jar files are coming from. – Ash Oct 05 '16 at 14:33
  • can you show jdbc connecting code.`jdbc:postgresql://host:port/database` – Madhawa Priyashantha Oct 05 '16 at 14:34
  • hi @AshFrench, Yes I can see jar file in projects side bar in IDE under libraries and if I rightclick on jar and select edit it shows below location `C:\Users\username\Documents\NetBeansProjects\Test\build\classes\postgresql-9.2-1003.jdbc3.jar` – Rajesh Mhatre Oct 05 '16 at 14:46
  • Sorry, I meant that in your app.jar, you find the postgres.jar otherwise the your app.jar will never find it, unless you add it to the class path when running it – Ash Oct 05 '16 at 15:00
  • Hi @AshFrench Can you give me steps to do that as this is my first time I am using windows OS and my app jar is at this location `C:\Users\usernm\Documents\NetBeansProjects\myApp\dist\myApp.jar` I can see postgresql-9.2-1‌​003.jdbc3.jar file at `C:\Users\usernm\Documents\NetBeansProjects\myApp\dist\lib\postgresql-9.2-1003.jdbc3.jar` this location – Rajesh Mhatre Oct 05 '16 at 15:04
  • try this `java -classpath C:\Users\usernm\Documents\NetBeansProjects\myApp\dist\lib\* -jar myApp.jar` this will add all of your dependant jar files to the classpath, so when running your app, it will actually have knowledge about where to find these files, netbeans hides all this away with a load of magic and does this for you. – Ash Oct 05 '16 at 15:07
  • Hi @AshFrench I am still getting same error not sure why. It works in IDE netbeans but not after building it :(. Do I need admin right for such things as I am not having admin rights – Rajesh Mhatre Oct 05 '16 at 15:15
  • Hi @AshFrench, In `\dist\lib\*` folder of app I am having only `postgresql-9.2-1003.jdbc3.jar` this file and in `\dist\` directory I have `myApp.jar` and `README.txt` file. do I also need anything else there ? – Rajesh Mhatre Oct 05 '16 at 15:22
  • What is the content of `MANIFEST.MF` in your jar file (and please edit it into your question, don't post as a comment!) By the looks of it you haven't specified the driver jar in the manifest `Class-Path` entry. – Mark Rotteveel Oct 05 '16 at 15:40
  • thanks @MarkRotteveel I have added manifest in my question – Rajesh Mhatre Oct 05 '16 at 19:01
  • Are you running myApp.jar from its original location in `NetBeansProjects\myApp\dist`, or have you copied/moved it to a different location (or a different machine)? – Gord Thompson Oct 05 '16 at 23:01
  • I am running it from same original location: `NetBeansProjects\myApp\dist` – Rajesh Mhatre Oct 06 '16 at 18:16
  • I don't think is the dupliocate of [this]( http://stackoverflow.com/questions/39808031/no-suitable-driver-found-when-running-from-jar), Do I need to ask this question again ? I have added dir content in main question @GordThompson I can see dependent jar file in my \dist\lib\ folder as I had added that jdbc in library of netbeans project already as given in solution but still I am getting error. – Rajesh Mhatre Oct 07 '16 at 08:38

0 Answers0