-1

I have a simple java application that i've got running in Netbeans, show below. i've added the sqlite-jdbc.jar in Netbeans to access the database but i'd like to know if i can use the .jar from the Command Prompt and if so how? Thanks.

import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import javax.swing.JOptionPane;
import java.sql.*;
import javax.swing.*;

/**
 *
 * @author James
 */
public class TestProj{

    /**
     * @param args the command line arguments
     */


    public static void main(String[] args) {
        // TODO code application logic here
        ConnectDb();

    }
        public static Connection ConnectDb(){

        try
        {
            Class.forName("org.sqlite.JDBC");
            Connection conn =  DriverManager.getConnection("jdbc:sqlite:
                  C:\\Users\\James\\Documents
                             \\NetBeansProjects\\TestProj\\TestProj.sqlite");

            System.out.println("Connection opened successfully!");
            Statement stat = conn.createStatement();

            String sql = "SELECT kE100 FROM customers" +
                   " WHERE phoneNo = 873333333";
            ResultSet rs = stat.executeQuery(sql);

            System.out.println("name = " + rs.getString("kE100"));



            conn.close();
            return conn;
        }catch(Exception e){

            System.out.println("No Connection!");
            return null;

        }

    }
}

C:\Users\James\Documents\NetBeansProjects\TestKamarad\src\testkamarad>javac -cp "C:/Users/James/Desktop/Installers Etc/sqlite-jdbc-3.7.2.jar;" TestKamarad.java

C:\Users\James\Documents\NetBeansProjects\TestKamarad\src\testkamarad>java -cp " C:/Users/James/Desktop/Installers Etc/sqlite-jdbc-3.7.2.jar;" TestKamarad Exception in thread "main" java.lang.NoClassDefFoundError: TestKamarad (wrong na me: testkamarad/TestKamarad) at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(Unknown Source) at java.security.SecureClassLoader.defineClass(Unknown Source) at java.net.URLClassLoader.defineClass(Unknown Source) at java.net.URLClassLoader.access$100(Unknown Source) at java.net.URLClassLoader$1.run(Unknown Source) at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)

  • 1
    Yes, you can. If you google 50 randomly chosen chars from your title you'll get a relevant hit. I'm almost serious. Did you do any research, or run into any problems? – keyser Apr 19 '14 at 23:39
  • Yes i did, and i found many posts that should how to run a .jar file from the command line, like the one below. Which is not what i'm looking for, and i found one's explaining how to use the -cp classpath reference, which seems to work ok when compiling the project but i still can't run the program using -cp – user3552839 Apr 20 '14 at 10:10
  • So, you're getting some error? It wasn't successfully added to the classpath? – keyser Apr 20 '14 at 11:18
  • and done. and a few more characters to make 15 – user3552839 Apr 20 '14 at 11:32
  • Add that to the question. It's _a lot_ more relevant than the code – keyser Apr 20 '14 at 11:33
  • Try the following: `java -cp jar1:jar2:jar3:dir1:. HelloWorld`. Note that last dot, which adds the current directory. – keyser Apr 20 '14 at 11:38
  • please excuse my ignorance, i'm very new to using the command line. the dir1 at the end, is that referring to the directory that contains the .java file? something like this? java -cp " C:/Users/James/Desktop/Installers Etc/sqlite-jdbc-3.7.2.jar;C:Users/James/MyDocuments/NetbeansProjectsTestKamarad/src/testkamarad;." TestKamarad – user3552839 Apr 20 '14 at 12:30
  • Possible duplicate of [trying to run a .jar file in command prompt](https://stackoverflow.com/questions/9822256/trying-to-run-a-jar-file-in-command-prompt) –  Jun 15 '17 at 05:17

1 Answers1

0

How to run a .jar from commandline ? It's pretty simple, just type in:

java -jar foo\bar.jar
kiwixz
  • 1,380
  • 15
  • 23