View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# How to use classes in .jar?

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

9 replies to this topic

### #1Ripiz  Members

Posted 10 May 2012 - 06:49 AM

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)
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?

P.S. Main is my .class file, which is not inside .jar.

Edited by Ripiz, 10 May 2012 - 06:50 AM.

### #2Ashaman73  Members

Posted 10 May 2012 - 07:02 AM

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.

Ashaman

### #3Ripiz  Members

Posted 10 May 2012 - 07:17 AM

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)
Could not find the main class: Main. Program will exit.
*/


Edited by Ripiz, 10 May 2012 - 07:17 AM.

### #4Antheus  Members

Posted 10 May 2012 - 08:49 AM

Show the source of Main class (Main.java)

Edited by Antheus, 10 May 2012 - 08:49 AM.

### #5Ripiz  Members

Posted 10 May 2012 - 08:59 AM

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.

Edited by Ripiz, 10 May 2012 - 09:05 AM.

### #6MESEnterprisesLLC  Members

Posted 10 May 2012 - 09:29 AM

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

### #7Ripiz  Members

Posted 10 May 2012 - 09:49 AM

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?

Edited by Ripiz, 10 May 2012 - 10:18 AM.

### #8Kyan  Members

Posted 10 May 2012 - 11:17 AM

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

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.

### #9Ashaman73  Members

Posted 10 May 2012 - 11:28 AM

twist - on Windows,

Oopps.. thx for pointing this out

Ashaman

### #10Ripiz  Members

Posted 10 May 2012 - 12:46 PM

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

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.