Aaron Aaron - 1 month ago 12
Java Question

How will I include a Java jar file when compiling a Java class and running the Java file from the command console?

I have a small Java file interacting with a postgresql database,
so I've downloaded the drivers and in my file I import

org.postgresql.Driver
.

In the command console I type
javac Myfilename.java
;
then it compiles I run
java Myfilename
and it throws an error saying it can not find the
org.postgresql.Driver
files.

So how do I import the jar when I run the file or when I compile the file not sure when the import should take place?

Answer

Compile:

javac -cp ".:postresql.jar" -d . MyFileName.java

Run:

java -cp ".:postresql.jar" MyFileName