当前位置: 动力学知识库 > 问答 > 编程问答 >

postgresql - Running java class with library from cmd

问题描述:

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.

分享给朋友:
您可能感兴趣的文章:
随机阅读: