CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    May 2012
    Posts
    8

    how to run a java program..when the program using jar

    hi..

    i successfully compiled the java program with
    Code:
    javac -classpath "D:\Libraries\SQLite_DAL.jar" JavaApplication_KMF_Test.java
    ..

    if i am using like

    Code:
    C:\Users\Srihari\Documents\NetBeansProjects\JavaApplication_KMF_Test\src>java -c
    lasspath D:\Libraries\SQLite_DAL.jar JavaApplication_KMF_Test
    Exception in thread "main" java.lang.NoClassDefFoundError: JavaApplication_KMF_T
    est
    Caused by: java.lang.ClassNotFoundException: JavaApplication_KMF_Test
            at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
            at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    Could not find the main class: JavaApplication_KMF_Test.  Program will exit.
    but i don't know how to run the program in windows command prompt....

    my program uses sqlitejdbc-v056.jar(driver) and SQLite_DAL.jar(it returns connectionstring .. etc..)... then how to run program...

    sqlitejdbc-v056.jar(driver) and SQLite_DAL.jar(it returns connectionstring .. etc..) both are in same directory "D:\Libraries\"

    and JavaApplication_KMF_Test.class is in "C:\Users\Srihari\Documents\NetBeansProjects\JavaApplication_KMF_Test\src\JavaApplication_KMF_Test.class"
    Last edited by miriyalasrihari; May 17th, 2012 at 07:39 AM.

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: how to run a java program..when the program using jar

    but i don't know how to run the program in windows command prompt....
    You need to add both jars to the classpath or use a wildcard to include the whole directory and you also need to add the root directory of the package for the JavaApplication_KMF_Test.class.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    May 2012
    Posts
    8

    Re: how to run a java program..when the program using jar

    thanks for the replay...

    i've following package.

    Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package SQLite_DAL;
    
    import java.sql.*;
    
    /**
     *
     * @author Srihari
     */
    public class SQLite_Database {
    
        public String Data() {
            return "Srihari";
        }
    
        public Connection GetConnection(String db_Path)
                throws ClassNotFoundException {
            Connection connection = null;
            try {
    
                Class.forName("org.sqlite.JDBC");
                connection = DriverManager.getConnection("jdbc:sqlite:" + db_Path);
    
            } catch (SQLException e) {
            }
    
            return connection;
    
        }
    
        public String GetEmployeeData(Connection connection, String query)
                throws SQLException {
            String data = "";
            Statement statement = connection.createStatement();
            ResultSet rs = statement.executeQuery(query);
    
            while (rs.next()) {
                data += rs.getString(1) + ",";
            }
            rs.close();
            statement.close();
            return data;
        }
    }

    i created a jar file using
    Code:
    jar cvf SQLite_DAL.jar SQLite_DAL_DirectoryName
    sqlitejdbc-v056.jar(driver) and SQLite_DAL.jar(it returns connectionstring .. etc..) both are in same directory "D:\Libraries\"

    and added as a library for another project(JavaApplication_KMF_Test)

    JavaApplication_KMF_Test project contains following class

    Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    import SQLite_DAL.SQLite_Database;
    import java.sql.Connection;
    import java.sql.SQLException;
    
    /**
     *
     * @author Srihari
     */
    public class JavaApplication_KMF_Test {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            // TODO code application logic here
            SQLite_Database objSQLite_Database = new SQLite_Database();
            String name = objSQLite_Database.Data();
            System.out.println(name);
            Connection connection = null;
            try {
                connection = objSQLite_Database.GetConnection("D:\\SQLiteDB\\EMPLOYEE.db");
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                System.out.println(e.getMessage());
            }
            try {
                String employeeData = objSQLite_Database.GetEmployeeData(
                        connection, "SELECT EMPNAME FROM EMPLOYEEDETAILS");
                System.out.println(employeeData);
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                System.out.println(e.getMessage());
            }
        }
    }
    path for above class was "C:\Users\Srihari\Documents\NetBeansProjects\JavaApplication_KMF_Test\src\"


    now i am trying to execute from windows command prompt

    Compiling

    Code:
    C:\Users\Srihari\Documents\NetBeansProjects\JavaApplication_KMF_Test\src>java -c
    lasspath "D:\Libraries\SQLite_DAL.jar;D:\Libraries\sqlitejdbc-v056.jar" SQLite_D
    AL.JavaApplication_KMF_Test
    succeed...

    Run

    now i am getting

    Code:
    C:\Users\Srihari\Documents\NetBeansProjects\JavaApplication_KMF_Test\src>java -c
    lasspath "D:\Libraries\SQLite_DAL.jar;D:\Libraries\sqlitejdbc-v056.jar" SQLite_D
    AL.JavaApplication_KMF_Test
    Exception in thread "main" java.lang.NoClassDefFoundError: SQLite_DAL/JavaApplic
    ation_KMF_Test
    Caused by: java.lang.ClassNotFoundException: SQLite_DAL.JavaApplication_KMF_Test
    
            at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
            at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    Could not find the main class: SQLite_DAL.JavaApplication_KMF_Test.  Program wil
    l exit.
    
    C:\Users\Srihari\Documents\NetBeansProjects\JavaApplication_KMF_Test\src>

    please help me..

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: how to run a java program..when the program using jar

    Why have you used "SQLite_DAL.JavaApplication_KMF_Test", JavaApplication_KMF_Test isn't an inner class of SQLite_DAL.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  5. #5
    Join Date
    May 2012
    Posts
    8

    Re: how to run a java program..when the program using jar

    it's not an inner class of SQLite_DAL,
    actually i don't know how to execute that one.. i am trying to get some solution so i tried like this.. but no use...

  6. #6
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: how to run a java program..when the program using jar

    Code:
    java -classpath D:\Libraries\SQLite_DAL.jar;D:\Libraries\sqlitejdbc-v056.jar;. JavaApplication_KMF_Test
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  7. #7
    Join Date
    May 2012
    Posts
    8

    Re: how to run a java program..when the program using jar

    thanks a lot...( i got it)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured