Wednesday, January 8, 2014

How to run jar file in Linux

Goal :

 1. Most of production environments provides only terminal accesses. 
 2. Most of time there are no Graphical User Interfaces.
 3. So developers have to provide solutions  based on tiny console access. 
 4. Most of production environments are remotely located and support
     engineers  have to access them via slow network as well.


Prerequisites 

1. Install java and javac under Linux environment
2. Install Oracle under Linux environment
3. Download Oracle JDBC jar file from maven repository or oracle site.
    Ex - ojdbc14.jar
                            
   http://mvnrepository.com/artifact/ojdbc/ojdbc
   http://www.oracle.com

Method

1. Create a simple project that connect to database and close the connection.
2. Export the project in to simple jar file.             
3. During this project we have to link Oracle JDBC driver jar file with the Project jar file.
4. Run the project jar file with making link to Oracle JDBC driver.


Step 1 : For the people who interested to write it from the scratch.
       
Navigate to Desktop by following command.
      cd ~/Desktop
Create folder called "testing" in your current location.
       mkdir testing
Change your directory to "testing"
       cd testing
Create folder called "javaunderlinux" inside folder called "testing"
       mkdir javaunderlinux
       
                       
Step 2 : Create a class called "OracleTime.java" inside folder called "javaunderlinux"
         
Use vi editor to create and save a java file.
        vi OracleTime.java

Write your OracleTime.java file as following manner.
                           

package testing.javaunderlinux;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

class OracleTime{
               

     public static void main(String[] str){
       
        Connection dbConnection =  null;
       Statement statement = null;
       ResultSet rs = null;
                
          try {
                 
   Class.forName("oracle.jdbc.driver.OracleDriver");
           
        dbConnection =  DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","sys", "sys123");                                statement = dbConnection.createStatement();
                    rs=  statement.executeQuery("SELECT TO_CHAR(SYSDATE,'hh24:mi:ss') AS TIMENOW FROM DUAL");
                    while(rs.next()){
                         System.out.println("Oracle Time : "+rs.getString("TIMENOW"));
                     } 

                     rs.close();           
                     statement.close();
                    dbConnection.close();

 
           }catch ( Exception ex ){
                    ex.printStackTrace();
           }
    }

}

 

Save and exit from OracleTime.java

Step 3  : Time to compile OracleTime.java
       
Go to the command prompt and change your directory to Desktop
      cd ~/Desktop
Then compile the code.
      javac testing/javaunderlinux/OracleTime.java

Now it will create OracleTime.class inside folder called "javaunderlinux"

Step 4  : Creating jar file
       
Go to the command prompt and change your directory to Desktop
      cd ~/Desktop
Create jar file using following command.
      jar -cvf oracletime.jar testing/javaunderlinux/*
           
         
Step 5  : Time to run the jar file.

Check whether Oracle Database is up and running by following command.
      telnet localhost 1521

run the following command by linking both ojdbc14.jar and oracletime.jar
java -cp ojdbc14.jar:oracletime.jar testing.javaunderlinux.OracleTime


Step 6  : Time to see the result 

The result will similar to following one.
     Oracle Time : 16:45:22         


No comments:

Post a Comment