Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By Tolls

Thread: ClassNotFoundException

  1. #1
    naveen516 is offline Member
    Join Date
    Dec 2011
    Posts
    6
    Rep Power
    0

    Default ClassNotFoundException

    Java Code:
    import java.sql.*;
    public class Example1
    {
    	public static void main(String args[])
    	{
    		try
    		{	
    			 String s1=null;
    			 String s2=null;
    			 String s4=null;
    			 Class.forName("oracle.jdbc.driver.OracleDriver");
    			 Connection con=DriverManager.getConnection("oracle:jdbc:thin:@localhost:1521:employee","system","sanjay16");
    			 Statement stmt=con.createStatement();
    			 ResultSet rs=stmt.executeQuery("select * from employee where empid=101");
    			while(rs.next())
    			{
    				s1=getString(1);
    				s2=getString(2);
    				s4=getString(4);
    			}
    			System.out.println(" Name is "+s2);
    			System.out.println(" Employee id is "+s1);
    			System.out.println(" Sallery is "+s4);
    			
    			}
    		catch(Exception e)
    		{
    			e.printStackTrace();
    		}
    	}
    
    	private static String getString(int i) {
    		// TODO Auto-generated method stub
    		return null;
    	}
    
    }
    This is my code, i am getting ClassNotFoundException, i thought that thereis a problem in classpath setting.
    Can u plz tell me the ways of classpath setting in thin driver.

    We have to include ojdbc14.jar, so give me theways to set the classpath
    Last edited by pbrockway2; 12-10-2011 at 10:00 AM. Reason: code tags added

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: ClassNotFoundException

    The required jar file needs to be put somewhere where you can refer to it when you compile and run your code. For this simple example you might put it in the same folder that you have Example1.java

    Then you can compile and run with the following (cd to the directory containing the code first)

    Java Code:
    javac -cp .;ojdbc14.jar Example1.java
    java -cp .;ojdbc14.jar Example1
    The thing following -cp is the classpath. If you are on *nix use : rather than ; as shown (which is for Windows). The dot means "look for classes in the current directory" and "ojdbc14.jar" means "look for classes in the ojdbc14.jar archive". You can specify any number of locations this way, each one separated by ; (or :).

    -----

    I have added code tags to your post. The thing is when you post code put [code] and [/code] at the end so that formatting is preserved.

  3. #3
    Join Date
    Dec 2011
    Posts
    1
    Rep Power
    0

    Default Re: ClassNotFoundException

    If you have oracle Database, go to oracle install folder and search for ojdbc14.jar file.
    If you found that, copy the jar file location and Right click on the MyComputer Icon---->properties----->Advanced tab---->Environment Variables----->in the user variables click on new button---->variable name:CLASSPATH
    variable value:.;past that url here; (ex: variable value=,;C:\oraclexe\app\oracle\product\10.2.0\serv er\jdbc\lib;)
    ----->ok--->ok---->ok



    this is the permenent path setting no need to add classpath every time.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,945
    Rep Power
    19

    Default Re: ClassNotFoundException

    Quote Originally Posted by pbrockway2 View Post
    The required jar file needs to be put somewhere where you can refer to it when you compile and run your code. For this simple example you might put it in the same folder that you have Example1.java
    Just a nitpick, but in this case you don't actually need it for the compiling, just at runtime, since the oracle code is not referenced directly, just via a String.

    Quote Originally Posted by RameshPadamati View Post
    If you have oracle Database, go to oracle install folder and search for ojdbc14.jar file.
    If you found that, copy the jar file location and Right click on the MyComputer Icon---->properties----->Advanced tab---->Environment Variables----->in the user variables click on new button---->variable name:CLASSPATH
    variable value:.;past that url here; (ex: variable value=,;C:\oraclexe\app\oracle\product\10.2.0\serv er\jdbc\lib;)
    ----->ok--->ok---->ok



    this is the permenent path setting no need to add classpath every time.
    And this is just wrong.
    You do not muck with the environment variables.
    It is not the correct way to handle a java projects classpath.
    You use the method given by pbrockway2, or via your IDE on a per project basis.
    pbrockway2 likes this.

Similar Threads

  1. ClassNotFoundException...
    By providence in forum JDBC
    Replies: 2
    Last Post: 02-02-2011, 06:45 PM
  2. ClassNotFoundException
    By meprasobh in forum New To Java
    Replies: 2
    Last Post: 10-17-2010, 03:32 PM
  3. ClassNotFoundException
    By lgpublic in forum Advanced Java
    Replies: 5
    Last Post: 04-23-2010, 03:05 PM
  4. ClassNotFoundException
    By Edward in forum New To Java
    Replies: 2
    Last Post: 06-29-2009, 07:08 PM
  5. ClassNotFoundException JSP
    By TDMaster in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 03-27-2009, 07:41 AM

Posting Permissions

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