How to use classes in .jar?

Started by
8 comments, last by Ripiz 11 years, 11 months ago
Hello,

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.
Advertisement
When calling java from your directory where the Main.class exists(no package), try something like this:

java -cp.;postgresql-9.1-902.jdbc4.jar Main

The '.' will include the current directory in the classpath.

java -cp .;postgresql-9.1-902.jdbc4.jar Main

Just throws how to use java (list of command line options) and in the end postgresql-9.1-902.jdbc4.jar: Command not found.

I guess it failed to parse command.

With quotes result is similar to previous attempts

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.
*/
Show the source of Main class (Main.java)
It's in the original post. If you wish everything then here it is:


import java.sql.*;
public class Main {
public static void main(String[] args) {
try {
Class.forName("org.postgresql.Driver");
Connection con = DriverManager.getConnection(url_removed);
} catch (SQLException ex) {
System.out.println(ex.getMessage());
} catch (ClassNotFoundException ex) {
System.out.println(ex.getMessage());
}
}
}

When I use java Main it throws ClassNotFoundException, as it cannot find class org.postgresql.Driver, meaning I have to tell where it's located (the .jar file), but I don't know how to do that.
Hello -

You need to specify the .jar/.zip files on the command line or in your CLASSPATH environment variable... Best thing to do is add the 3rd party paths to the environment variable and just worry about defining your project's class paths on the command line (otherwise the string after the -cp can get quite long.) Another option is to export the project into a runnable executable file, which basically pulls all the jars that the project depends on into a single file, so that the JVM knows where they are.

The reason it works in NetBeans, is that you told the project where to find all the jars that it depends on - you now just need to do the same when running on the command line. Without knowing where you put the files (your project's classes and 3rd party jars) on your system, I can't give you the exact line that would work - sort of like the JVM (if you don't tell it where to look, it can't start to look [it will not search the entire system and all external paths that the system references.])

Hope that helps, if not points you in the right direction.

Good luck.
Michael Suess
MES Enterprises, LLC
http://mesenterprisesllc.com

You need to specify the .jar/.zip files on the command line or in your CLASSPATH environment variable...

This is university's computer, therefore I cannot modify environment variable and have to stick only to command line.


you now just need to do the same when running on the command line.

I've been trying to do this, with no luck so far.


Without knowing where you put the files (your project's classes and 3rd party jars) on your system, I can't give you the exact line that would work


So far it's only my Main class (default package) and postgresql-9.1-902.jdbc4.jar, both of which are in the same folder.
I've tried tried java -cp postgresql-9.1-902.jdbc4.jar Main but it didn't work. Ashaman73 suggested to include . to classpath, but that changed nothing.

Could you please show the proper way to do this?

Thank you for reply.
Just throws how to use java (list of command line options) and in the end postgresql-9.1-902.jdbc4.jar: Command not found.[/quote]
Ashaman's answer is indeed correct but with a minor punctuational twist - on Windows, classpath paths are separated by a semi-colon but on Linux you need a full colon, e.g.:

java -classpath .:postogresql-9.1-902.jdbc4.jar Main

I downloaded the Postogre JDBC library and compiled it against your Main, so it should work fine.

twist - on Windows,

Oopps.. thx for pointing this out smile.png

but on Linux you need a full colon, e.g.:
java -classpath .:postogresql-9.1-902.jdbc4.jar Main


That was the problem? D:
It's a shame :(

Thank you!

P.S. Appears Windows was not a problem, still cannot connect :(

This topic is closed to new replies.

Advertisement