Running java program in linux terminal with -class path

I try to run the following program for an hour using the postgresql class

class Test{
  public static void main(String[] args){
        try {
            Class.forName("org.postgresql.Driver");
        } catch (ClassNotFoundException cnfe) {
            System.err.println("Couldn't find Postgresql driver class!");
        }
  }
}

The program was compiled using the javac command, but it is not easy for me to run it using the postgresql path. I have "postgresql-9.0-801.jdbc4.jar" in the same directory as the file, and I tried the following but didn't work

java -classpath ./postgresql-9.0-801.jdbc4.jar Test
java -classpath postgresql-9.0-801.jdbc4.jar Test
java -classpath "postgresql-9.0-801.jdbc4.jar" Test

What am I doing wrong?

Hello!

+3
source share
3 answers

When you specify the class path, you need to make sure that it includes ALL the class files that your application requires, including the ones you create yourself. Assuming Test.class is in the current directory along with the Jar postgres file, you need something like:

java -classpath postgresql-9.0-801.jdbc4.jar:. Test

. Java Glossary.

~

+6

? ClassNotFoundException Test postgres? , , Test .

, , Test.class postgres,

java -classpath .:postgresql-9.0-801.jdbc4.jar Test
+3
command :java -cp .;postgresql-9.0-801.jdbc4.jar Test

both jar and the class are in the same directory from which the command is executed.
Also make your class definition public!

0
source

Source: https://habr.com/ru/post/1782551/


All Articles