954,545 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Error while executing

The following is a piece of code for performing linear search:

import java.io.*;

class search
{
      String str;
      int key,size,seaArr[];
	  
	  public void getdata()
	  {
	         System.out.print("Enter how many data you want to enter : ");
			 System.out.flush();
			 try{
			     BufferedReader obj=new BufferedReader(new InputStreamReader(System.in));
			     str=obj.readLine();
				 size=Integer.parseInt(str);
				 seaArr=new int[size];
				 for(int i=0;i<size;i++)
				   {
				       System.out.print("Enter element at "+(i+1)+"th position  :  ");
					   System.out.flush();
					   str=obj.readLine();
					   seaArr[i]=Integer.parseInt(str);
				   }
			    }
			catch(Exception e)  {}
	  }
			     
	  public int LinSrch()
	  {
	        System.out.println("=====LINEAR SEARCH=====\n");
			getdata();
			System.out.print("\nEnter Search Key : ");
			System.out.flush();
			try{
			  BufferedReader obj=new BufferedReader(new InputStreamReader(System.in));
			  str=obj.readLine();
			  key=Integer.parseInt(str);
				for(int i=0;i<size;i++)
					{
					      if(seaArr[i]==key)
					           return(i+1);
					 }
				 }
			 catch(Exception e) {}
			 return(0);
	    }	
}

class  linSea
{
	public static void main(String args[]) 
	{
           search o1 = new search();
		   int result;
		   result=o1.LinSrch();
		   if(result==0)
		         System.out.println("\nSearch Not Found");
			else
			     System.out.println("\nSearch is Located at "+result+" Position");
	}
}

But whenever i execute the program I get the following error:

Exception in thread "main" java.lang.NoClassDefFoundError: linSea

where linSea is the name of the file.
Please help me solve this query of mine.
Thanks

jigvesh
Light Poster
44 posts since Oct 2004
Reputation Points: 10
Solved Threads: 0
 

that one class was not needed:

import java.io.*;

public class search
{
      String str;
      int key,size,seaArr[];
	  
	  public void getdata()
	  {
	         System.out.print("Enter how many data you want to 

enter : ");
			 System.out.flush();
			 try{
			     BufferedReader obj=new 

BufferedReader(new InputStreamReader(System.in));
			     str=obj.readLine();
				 size=Integer.parseInt(str);
				 seaArr=new int[size];
				 for(int i=0;i<size;i++)
				   {
				       System.out.print("Enter 

element at "+(i+1)+"th position  :  ");
					   System.out.flush();
					   str=obj.readLine();
					   

seaArr[i]=Integer.parseInt(str);
				   }
			    }
			catch(Exception e)  {}
	  }
			     
	  public int LinSrch()
	  {
	        System.out.println("=====LINEAR SEARCH=====\n");
			getdata();
			System.out.print("\nEnter Search Key : ");
			System.out.flush();
			try{
			  BufferedReader obj=new BufferedReader(new 

InputStreamReader(System.in));
			  str=obj.readLine();
			  key=Integer.parseInt(str);
				for(int i=0;i<size;i++)
					{
					      if(seaArr[i]==key)
					           return(i+1);
					 }
				 }
			 catch(Exception e) {}
			 return(0);
	    }	
	public static void main(String args[]) 
	{
           search o1 = new search();
		   int result;
		   result=o1.LinSrch();
		   if(result==0)
		         System.out.println("\nSearch Not Found");
			else
			     System.out.println("\nSearch is Located 

at "+result+" Position");
	}
}
server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 

that one class was not needed:

import java.io.*;

public class search
{
      String str;
      int key,size,seaArr[];
	  
	  public void getdata()
	  {
	         System.out.print("Enter how many data you want to 

enter : ");
			 System.out.flush();
			 try{
			     BufferedReader obj=new 

BufferedReader(new InputStreamReader(System.in));
			     str=obj.readLine();
				 size=Integer.parseInt(str);
				 seaArr=new int[size];
				 for(int i=0;i<size;i++)
				   {
				       System.out.print("Enter 

element at "+(i+1)+"th position  :  ");
					   System.out.flush();
					   str=obj.readLine();
					   

seaArr[i]=Integer.parseInt(str);
				   }
			    }
			catch(Exception e)  {}
	  }
			     
	  public int LinSrch()
	  {
	        System.out.println("=====LINEAR SEARCH=====\n");
			getdata();
			System.out.print("\nEnter Search Key : ");
			System.out.flush();
			try{
			  BufferedReader obj=new BufferedReader(new 

InputStreamReader(System.in));
			  str=obj.readLine();
			  key=Integer.parseInt(str);
				for(int i=0;i<size;i++)
					{
					      if(seaArr[i]==key)
					           return(i+1);
					 }
				 }
			 catch(Exception e) {}
			 return(0);
	    }	
	public static void main(String args[]) 
	{
           search o1 = new search();
		   int result;
		   result=o1.LinSrch();
		   if(result==0)
		         System.out.println("\nSearch Not Found");
			else
			     System.out.println("\nSearch is Located 

at "+result+" Position");
	}
}


Thanks but the error is not yet resolved. this time i got the following error:

Exception in thread "main" java.lang.NoClassDefFoundError: search
jigvesh
Light Poster
44 posts since Oct 2004
Reputation Points: 10
Solved Threads: 0
 

Ok, it worked for me, so this tells me it's your enviroment variables. So why don't you post your classpath and path variables, and I'll see what I can do..

You can usually put this in front of you classpath and it wili work:

.;%classpath%.;.;

Also make sure your classpath variable is pointing to tools.jar

server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 

Check your class definition.

Your main class MUST be public (though some runtime versions (especially older ones) may have permitted default access that's no longer the case and was never recommended).

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

Ok, it worked for me, so this tells me it's your enviroment variables. So why don't you post your classpath and path variables, and I'll see what I can do..

You can usually put this in front of you classpath and it wili work:

.;%classpath%.;.;

Also make sure your classpath variable is pointing to tools.jar


Thanks a tonne, but I realized where I was wrong. Actually I had specified the wrong path in the autoexec.bat and it had to be updated. That's it. Thanks anyway to both of you!

jigvesh
Light Poster
44 posts since Oct 2004
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You