Sign in to follow this  

How to use classes in .jar?

This topic is 2042 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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)
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.
*/
[/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?
Thank you in advance.

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

Share this post


Link to post
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.

Share this post


Link to post
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)
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.
*/
[/CODE] Edited by Ripiz

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
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]

Share this post


Link to post
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 :(

Share this post


Link to post
Share on other sites

This topic is 2042 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this