0

I've got a problem. I have a console application, which is working with SQLite db. User enters password in console, than my app encrypts it and I need to write hash to db. I'm compiling and lauching my app only in ubuntu terminal using javac and java commands, because I use Console object instance and its method readPassword() and can't lauch my app in intellij idea built-in terminal. And I get such exception every time my app trying to use db:

java.lang.ClassNotFoundException: org.sqlite.JDBC at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:264) at com.company.DBHelper.connect(DBHelper.java:12) at com.company.StringOperator.save(StringOperator.java:15) at com.company.PasswordHelper.start(PasswordHelper.java:30) at com.company.Main.main(Main.java:14)

Here's my class forworking with DB:

package com.company;

import java.sql.*;

class DBHelper {
    private static Connection connection;
    private static Statement statement;
    static private final String tableName = "passwords";

    static void connect() throws ClassNotFoundException, SQLException {
        connection = null;
        Class.forName("org.sqlite.JDBC");
        connection = DriverManager.getConnection("jdbc:sqlite:password.db");

        System.out.println("Connected");
    }

    static void createDB() throws ClassNotFoundException, SQLException
    {
        statement = connection.createStatement();
        statement.execute("CREATE TABLE if not exists '" + tableName + "' ('id' INTEGER PRIMARY KEY AUTOINCREMENT, 'hash' text);");
        System.out.println("Table was created or already exists");
    }

    static void writeToDB(String password) throws SQLException
    {
        String query = "INSERT INTO '" + tableName + "' ('hash') VALUES ('" + password + "');";
        statement.execute(query);

        System.out.println("Password was successfully added to DB");
    }

    static void close() throws SQLException
    {

        statement.close();
        connection.close();

        System.out.println("Connection is closed");
    }
}

I'm pretty sure that this code is okay, 'cause I've tested it in another console application, with simple input of data (simply Scanner), so I lauched it in intelliJ built-in console. I just can't realise, how I need to compile it, maybe in some special way using javac or smth else. I have googled this question in a many ways, I have set different properties in intelliJ but it doesn't help.

  • The SQLite driver is not in your runtime classpath. You also don't need the `Class.forName()` anymore. It was for older drivers and it's been years since it's been relevant. – Kayaman May 29 '18 at 17:10
  • thanks, but I've already seen post that I duplicated as you marked, but it still doesn't help me – Anastasiia Tarasova May 30 '18 at 13:50
  • Do you mean you tried what's indicated in the duplicate question and it didn't work for you? – Kayaman May 30 '18 at 15:00
  • yes. exactly I meant it. I've tried all ways to solve it but I stil have same exception – Anastasiia Tarasova May 30 '18 at 15:10
  • Well, a `ClassNotFoundException` is the result of the class not being on the classpath. If you've included the driver jar in the classpath and it's still giving the exception, you don't have the right jar in the classpath. – Kayaman May 30 '18 at 16:32
  • Thanks, I understand what are you talking about. But, actually, setting a classpath is not fully clear for me in this situation. So can I ask you to explain me one more time how to do it? I've read a lot of information about it, but it seems I need more detailed explanation – Anastasiia Tarasova May 30 '18 at 16:39
  • Add the `-cp driver.jar` to the command you use when you're running the program with `java`. Maybe `-cp .;driver.jar` to include the current directory as well (use `:` instead of `;` for separator if you're not on Windows). For example `java -cp .;driver.jar -jar myjar.jar`. – Kayaman May 30 '18 at 16:44
  • Thank you so much. I just created a jar of my application and launched it even without adding a driver and it works! – Anastasiia Tarasova May 31 '18 at 08:14

0 Answers0