0

I just compiled a java program in the console. Now I want to run it. I needs jdbc driver for postgresql and mysql. Therefore I need to include the corresponding jars.

I did the follwing

java -Xmx512m -cp ".;/path/to/sql/jars" main.Main rc

When doing so I get the follwoing error:

Exception in thread "main" java.lang.NoClassDefFoundError: main/Main
Caused by: java.lang.ClassNotFoundException: main.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)
Could not find the main class: main.Main. Program will exit.

When ignoring the classpath option like this

java -Xmx512m  main.Main rc

I get the follwing error

java.lang.ClassNotFoundException: org.postgresql.Driver
    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.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:186)
    at database.PostgresQL.getConnection(Unknown Source)
    at database.PostgresQL.loadIndexFromDatabase(Unknown Source)
    at main.Main.readDataFromDatabase(Unknown Source)
    at main.Main.main(Unknown Source)

So, the program can be run correctly. My question: How to I tell the program to lookup the jars correctly in the console?

3 Answers 3

6
java -Xmx512m -cp ".;/path/to/sql/jars" main.Main rc

First, the path separator is only ; on Windows, it's : on other platforms. Second, if /path/to/sql/jars is a directory containing JAR files then you need to add a /* to the end and use single quotes rather than double:

java -Xmx512m -cp '.:/path/to/sql/jars/*' main.Main rc

The java command treats path/* in the classpath as equivalent to path/some-jar.jar:path/another-jar.jar enumerating all the JAR files in the directory, but you have to protect the * from expansion by the shell (hence the single quotes).

If you are on Windows then the same trick works but with semicolons, backslashes and double quotes:

java -Xmx512m -cp ".;C:\path\to\sql\jars\*" main.Main rc
Sign up to request clarification or add additional context in comments.

Comments

1

The separator for the classpath entries is ; on Windows and : on Linux. According to the path it seems like you are on Linux.

Comments

-2

Simply Copy the PostgreSQL and MySQL connector jars in to jdk.x.x_xx\jre\lib\ext and jre7\lib\ext folders (You can find them in the installation directory of both PostgreSQL and MySQL,

ex:-for MySQL the connector.jar is in MySQL\MySQL Connector J folder)

1 Comment

This is not to be recommended, lib/ext is for extensions that need to be available to all Java processes on a given JRE, not for libraries that are needed by one specific program. You can get into a horrible classloader mess by putting JARs in ext that weren't designed to work there, and it makes it impossible to have two different Java programs using different versions of the same library.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.