Followers 0

# How to use classes in .jar?

## 9 posts in this topic

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 [url="http://jdbc.postgresql.org/"]http://jdbc.postgresql.org/[/url] 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:
[CODE]
java Main
/*
org.postgresql.Driver
*/
/*
I assume it cannot find class, so I try to specify .jar file
*/
[/CODE]
[CODE]
java -jar postgresql-9.1-902.jdbc4.jar Main
/*
Failed to load Main-Class manifest attribute from
postgresql-9.1-902.jdbc4.jar
*/
[/CODE]
[CODE]
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)
Could not find the main class: Main. Program will exit.
*/
[/CODE]

My code:
[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());
}
[/CODE]

Anyone knows how can I run this program correctly?

P.S. Main is my .class file, which is not inside .jar. Edited by Ripiz
0

##### Share on other sites
When calling java from your directory where the Main.class exists(no package), try something like this:
[CODE]
java -cp.;postgresql-9.1-902.jdbc4.jar Main
[/CODE]
The '.' will include the current directory in the classpath.
1

##### Share on other sites
[CODE]
java -cp .;postgresql-9.1-902.jdbc4.jar Main
[/CODE]
Just throws how to use java (list of command line options) and in the end [i]postgresql-9.1-902.jdbc4.jar: Command not found.[/i]

I guess it failed to parse command.

With quotes result is similar to previous attempts
[CODE]
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)
Could not find the main class: Main. Program will exit.
*/
[/CODE] Edited by Ripiz
0

##### Share on other sites
Show the source of Main class (Main.java) Edited by Antheus
0

##### Share on other sites
It's in the original post. If you wish everything then here it is:

[CODE]
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());
}
}
}
[/CODE]
When I use [i]java Main[/i] 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. Edited by Ripiz
0

##### Share on other sites
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.
0

##### Share on other sites
[quote name='MESEnterprisesLLC' timestamp='1336663775' post='4939016']
You need to specify the .jar/.zip files on the command line or in your CLASSPATH environment variable...[/quote]
This is university's computer, therefore I cannot modify environment variable and have to stick only to command line.

[quote name='MESEnterprisesLLC' timestamp='1336663775' post='4939016']
you now just need to do the same when running on the command line.[/quote]
I've been trying to do this, with no luck so far.

[quote name='MESEnterprisesLLC' timestamp='1336663775' post='4939016']
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[/quote]

So far it's only my [i]Main[/i] class (default package) and [i]postgresql-9.1-902.jdbc4.jar[/i], both of which are in the same folder.
I've tried tried [i]java -cp postgresql-9.1-902.jdbc4.jar Main[/i] 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. Edited by Ripiz
0

##### Share on other sites
[quote]Just throws how to use java (list of command line options) and in the end [i]postgresql-9.1-902.jdbc4.jar: Command not found.[/quote][/i]
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.:

[i]java -classpath .:postogresql-9.1-902.jdbc4.jar Main[/i]

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

##### Share on other sites
[quote name='Kyan' timestamp='1336670273' post='4939051']
twist - on Windows,
[/quote]
Oopps.. thx for pointing this out [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]
0

##### Share on other sites
[quote name='Kyan' timestamp='1336670273' post='4939051']
but on Linux you need a full colon, e.g.:
[i]java -classpath .:postogresql-9.1-902.jdbc4.jar Main[/i]
[/quote]

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

Thank you!

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

## Create an account

Register a new account