-1

I'm using Netbeans 8.1 and maven, the project is as simple as connecting to a postgre DB and run a single query.

When I run the project from Netbeans, the System.out.println message I set tells me that the connection to the db went successful, passing by the message in the below code "PostgreSQL JDBC Driver Registered!".

When the same project is ran in the Windows 7 prompt, like:

java -jar myproject.jar

The

        try {
            Class.forName("org.postgresql.Driver");
        } 
        catch (ClassNotFoundException e) {
            System.out.println("No PostgreSQL JDBC Driver found");
            System.exit(0);
        }
System.out.println("PostgreSQL JDBC Driver Registered!");

Jumps to the catch{}.

I can't explain why. Is somebody able to help me?

EDIT: If we could please stop marking this as duplicate, that'd be great. It may be argument for the classpath, but I do not want to use classpaths. Another application I am using, a variant of this one, simply run without modification to the classpath and does exactly the same Db connection.

There must be a way to achieve it, may be using the pom.xml or something else that I do not know

EDIT2: I'm quite sure that I edited this before, explaining that I found the way to have the program working without adding classpaths to the Windows command. the pom.xml was the key to configure the application. And no, the "duplicate answer" did not help me this time.

aPugLife
  • 989
  • 2
  • 14
  • 25

2 Answers2

1

The reason it does not work is you forgot to set the classpath in your command. The PostgreSQL driver comes in jar. So you must pass the path of this jar location. Since you are using, the jars are located in ~/.m2/repository.

So go in this directory and search for for the driver, and pass -cp to command line with the driver location as a parameter :

java -cp $LOCATION_POSTGRESQL_DRIVER -jar myproject.jar
Dimitri
  • 8,122
  • 19
  • 71
  • 128
  • This makes sense, but I am not a developer and I only need to compile this java for a linux server I'm working on. So excuse me if I do not know much of classpath. Supposing I add the classpath on windows, pointing to the location of this jar, should I do the same on the server? Isn't there a way to just have the application running without adding paths? – aPugLife Aug 11 '16 at 10:33
0

On Windows 7, however, it is a little more complicated than that. The problem is that, if you simply run cmd and try to start or stop a service, you will most probably get this error message: System error 5 has occurred. Access is denied.

To overcome this, you need to start a "DOS box" as Administrator since, to the best of my knowledge, one cannot run a single command as Administrator inside the command prompt window opened under an account with fewer rights (there are no su and sudo commands in Windows).

One option is to click the "Start" button, type cmd into the "Search programs and files" box and hit Ctrl+Shift+Enter instead of just Enter. This will start a "DOS box" with administrative rights. Now, in this DOS box, you can run one of these commands (replacing the "9.1" with whatever your actual version of PostgreSQL is): NET START postgresql-9.1 (for the 32-bit version) NET STOP postgresql-9.1 (for the 32-bit version) NET START postgresql-x64-9.1 (for the 64-bit version) NET STOP postgresql-x64-9.1 (for the 64-bit version) to start and stop PostgreSQL.

imtiyaz
  • 92
  • 2
  • 12
  • Actually, it all would have been easier if: open start and type cmd, right click -> run as administrator. AND right click on application bar -> task manager -> services -> start/stop postgre. Anyway, I'm already running an administrator console and the postgres service is running too, I have PGAdmin opened and working on the DB. I'm quite sure it is an issue with Windows or its prompt, but I can't yet find it! Appreciate anyway your help (: – aPugLife Aug 11 '16 at 10:20