I'm new to Java and used it before only on Windows using NetBeans, however I got homework to create program that uses PostgreSQL database. It seems there's some issue connecting to database from Windows system, so I have to try Linux.
I've downloaded JDBC4 Connector Driver from http://jdbc.postgresql.org/ and there were no issues compiling and running it in NetBeans (GUI makes things easy), except connection part, however on Linux I have access only to terminal, therefore I have to specify *.jar which contains classes in order to run my program, and I do not know how to do that.
So far I tried:
java Main
/*
org.postgresql.Driver
*/
/*
I assume it cannot find class, so I try to specify .jar file
*/
java -jar postgresql-9.1-902.jdbc4.jar Main
/*
Failed to load Main-Class manifest attribute from
postgresql-9.1-902.jdbc4.jar
*/
java -cp postgresql-9.1-902.jdbc4.jar Main
/*
Exception in thread "main" java.lang.NoClassDefFoundError: Main
Caused by: java.lang.ClassNotFoundException: Main
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:334)
Could not find the main class: Main. Program will exit.
*/
My code:
try {
Class.forName("org.postgresql.Driver");
Connection con = DriverManager.getConnection(url);
} catch (SQLException ex) {
System.out.println(ex.getMessage());
} catch (ClassNotFoundException ex) {
System.out.println(ex.getMessage());
}
Anyone knows how can I run this program correctly?
Thank you in advance.
P.S. Main is my .class file, which is not inside .jar.