0

I have a Baseloader.java class which makes use of Postgresql database, and I have a postgresql42.jar file which handles the jdbc stuff.

Baseloader.java

 import java.sql.Connection;
 import java.sql.DriverManager;

public class Baseloader 
{
    public static void main(String[] args)
    {
        System.out.println("Hello, sir!");
    Connection c = null;
      try {
         Class.forName("org.postgresql.Driver");
         c = DriverManager
            .getConnection("jdbc:postgresql://localhost:5432/my_base",
            "postgres", "123");
        System.out.println("Done, sir!");
      } catch (Exception e) {
         e.printStackTrace();
         System.err.println(e.getClass().getName()+": "+e.getMessage());
         System.exit(0);
      }
      System.out.println("Opened database successfully");

   }
       }

So, when I compile and run this code through Netbeans (the postgresql.jar was included using GUI into Libraries folder) everything goes OK, the output is

Hello, sir!
Done, sir!
Opened database successfully

But when I try to run the compiled class file from cmd I get an error. I placed Baseloader.class and postgresql42.jar into the same folder and cd'ed into that folder in cmd. Then I use the java -classpath postgresql42.jar Baserunner command which was proposed on one of the answers here, but it gava me Could not find or load main class Baseloader error.

If I type just java Baseloader it gives Exception in thread "main" java.lang.UnsupportedClassVersionError.

What I'm doing wrong and how to fix it?

EDIT: I've changed the java version in cmd: now java -version gives 1.8.0_73'.Then I checked which version Netbeans used: System.out.println(System.getProperty("java.version")) it also gave 1.8.0_73.

In environmental variables the JAVA_HOME is C:\Program Files\Java\jdk1.8.0_73, while System.out.println(System.getProperty("java.home")) gives C:\Program Files\Java\jdk1.8.0_73\jre. Can that difference be the case?

EDIT2: Changed JAVA_HOME to C:\Program Files\Java\jdk1.8.0_73\jre with no result. Also javac -version gives the same newest 1.8.0_73 now.

parsecer
  • 4,758
  • 13
  • 71
  • 140
  • 1
    Make sure your java has the same (or later) version as javac used by Netbeans to compile your class file. And I think you should use "java -cp .:postgresql42.jar Baserunner" instead. – rsutormin Sep 17 '16 at 00:15
  • 1
    The jdk you're using with netbeans is different than the one on your command line: Duplicate of: http://stackoverflow.com/questions/10382929/how-to-fix-java-lang-unsupportedclassversionerror-unsupported-major-minor-versi – blue Sep 17 '16 at 00:17
  • (@rsutormin) if this is Windows, as 'cmd' suggests, classpath delimiter is semicolon. – dave_thompson_085 Sep 17 '16 at 06:01
  • @blue, @rsutormin, I checked - there was Java 1.7 on cmd, so I changed the Path veriable, now `java -version` gives 1.8, but I still can't run the class from cmd.. – parsecer Sep 17 '16 at 15:50

0 Answers0