是否可以从命令行运行应用程序并引用.jar文件?

时间:2014-04-19 23:38:14

标签: java jar command-prompt sqlitejdbc

我有一个简单的java应用程序,我已经在Netbeans中运行,如下所示。我已经在Netbeans中添加了sqlite-jdbc.jar来访问数据库,但是我想知道我是否可以使用命令提示符中的.jar,如果是这样的话?感谢。

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 线程" main"中的例外情况java.lang.NoClassDefFoundError:TestKamarad(错误的na 我: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)         在java.net.URLClassLoader.access $ 100(未知来源)         在java.net.URLClassLoader $ 1.run(未知来源)         在java.net.URLClassLoader $ 1.run(未知来源)         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)         在sun.launcher.LauncherHelper.checkAndLoadMain(未知来源)

1 个答案:

答案 0 :(得分:0)

如何从命令行运行.jar?这很简单,只需键入:

java -jar foo\bar.jar