• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Ripiz

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

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.
1

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
0

Share this post


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

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
0

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.
0

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
0

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.
2

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]
0

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 :(
0

Share this post


Link to post
Share on other sites

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  
Followers 0