java setting classpath

5.5k views Asked by At

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

There are 3 answers

0
Ian Roberts On BEST ANSWER
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
0
Dan D. On

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

1
sx3 On

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)