0

I'm using java JDBC to connect to SQLite on my Mac. The official tutorial is using windows. However I'm using a Mac. Below is the command I ran in the terminal at connect folder. This the command provided in the tutorial. https://www.sqlitetutorial.net/sqlite-java/sqlite-jdbc-driver/

java -classpath ".;sqlite-jdbc-3.27.2.1.jar" net.sqlitetutorial.Connect

Below is my file hierarchy.

╭─  ~/sqlite ········································at 17:27:22
╰─ tree
.
├── db
│   └── chinook.db
└── java
    └── connect
        ├── net
        │   └── sqlitetutorial
        │       ├── Connect.class
        │       └── Connect.java
        └── sqlite-jdbc-3.27.2.1.jar

5 directories, 4 files

Blow is the Connect.java the tutorial provided, I only changeString url = to my own path in my computer.

package net.sqlitetutorial;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 *
 * @author sqlitetutorial.net
 */
public class Connect {
     /**
     * Connect to a sample database
     */
    public static void connect() {
        Connection conn = null;
        try {
            // db parameters
            String url = "jdbc:sqlite:/Users/anasiangangster/sqlite/db/chinook.db";
            // create a connection to the database
            conn = DriverManager.getConnection(url);

            System.out.println("Connection to SQLite has been established.");

        } catch (SQLException e) {
            System.out.println(e.getMessage());
        } finally {
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException ex) {
                System.out.println(ex.getMessage());
            }
        }
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        connect();
    }
}

The original from the tutorial

String url = "jdbc:sqlite:C:/sqlite/db/chinook.db";

Anyway, when I ran the command(I provided in front) at connect folder. I get this error message.

Error: Could not find or load main class net.sqlitetutorial.Connect

Even though, I follow the troubleshooting method the tutorial provided. I'm not be able to solve it. Please help me if can. Thank you so much.

Mat
  • 202,337
  • 40
  • 393
  • 406
aobo yang
  • 5
  • 2
  • https://stackoverflow.com/questions/18093928/what-does-could-not-find-or-load-main-class-mean – Mat Nov 26 '19 at 12:47

1 Answers1

0

The classpath separator is the colon (":"), not the semicolon. It's only a semicolon on windows, which is probably why the tutorial uses that. So, just update the classpath param to: -cp .:sqlite-jdbc-3.27.2.1jar :)

NB: sidenote, but, sqlite is a weird fit for java. something like h2 makes a lot more sense. SQLite gives virtually no benefits: You still need to ship an SQLite engine and launch it from within the process if you want it to feel lightweight, OR you need to explicitly ask the user to install the thing, at which point you might as well ask them to install a full featured DB like postgres. H2 is in-process, does not require you to ship one executable for each and every os/architecture combination you intend to support, and does not require messing about with trying to make the host OS launch native apps. It 'just works'. You should use sqlite only if you have a specific need for precisely sqlite and nothing else will do (for example, you're on android, which provides sqlite for you, or, you're trying to inspect or change firefox data storage, which are sqlite dbs).

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72
  • OMG, I literally stuck here for most a week!! Thank you so much Sir. I'm doing this with my school project. None of us in the group is very familiar with android programming, so we decided using Flask in python for our backend server. – aobo yang Nov 26 '19 at 13:24