M
mamta81
hi i am trying to compile a java file in linux . The filee is
compiling fine but at run time it is giving the following exception
Exception in thread "main" java.lang.NoClassDefFoundError: tcpServer5
i am using jdk1.5 and the file is running on linux.
How do i set the classpath in linux
My .java file is below
import java.net.*;
import java.io.*;
//Server class.
public class tcpServer5
{
public static void main(String[] args)
{
int port;
ServerSocket server_socket;
port=Integer.parseInt(args[0]);
try{
server_socket=new ServerSocket(port);
System.out.println("Server Waiting for Client on Port
"+server_socket.getLocalPort());
while(true)
{
Socket socket=server_socket.accept();
System.out.println("New Connection
Accepted"+socket.getInetAddress()+":"+socket.getPort());
//FIRE A NEW SERVER THREAD FOR EACH CLIENT.
ServerThread st=new ServerThread(socket);
st.start();
}
}catch(Exception e){System.out.println("Connection Reset");}
}//main ends
}//class ends
//ServerThread
class ServerThread extends Thread
{
Socket s;
public ServerThread(Socket s)
{
this.s=s;
}
public void run()
{
Runtime r=Runtime.getRuntime();
PrintWriter spw=null;
BufferedReader sbr=null;
ChildOutputThread cot=null;
ChildInputThread cit=null;
try{
sbr= new BufferedReader(new
InputStreamReader(s.getInputStream()));
while(!sbr.ready())
Thread.sleep(500);
String command=sbr.readLine();
Process process=r.exec(command);
System.out.println("Process started with this command :"+command);
Thread t=Thread.currentThread();
t.sleep(1000);
int CASE=0;
if(command.endsWith("<inptrial>"))
CASE=1;
if(command.endsWith("<inpfinal>"))
CASE=2;
if(command.endsWith("output.txt"))
CASE=3;
if(command.endsWith("cfoil.RPT"))
CASE=4;
//managing child threads.
switch(CASE){
case 1: try{
cot = new ChildOutputThread(process,new File("inptrial"));
cit = new ChildInputThread(process,new File("output.txt"),cot);
cot.start();
cit.start();
process.waitFor();
}catch(Exception e){e.printStackTrace();}
finally{
try{
if(cit!=null & cot!=null){
cot.CONTINUE=false;
cit.CONTINUE=false;
cit.join();
cot.join();
}
System.out.println("Process terminated:");
System.out.println("completed");
spw=new PrintWriter(new BufferedWriter(new
OutputStreamWriter(s.getOutputStream()) ));
spw.println("completed");
spw.flush();
spw=null;
}catch(Exception e){e.printStackTrace();}
}
break;
case 2: try{
cot = new ChildOutputThread(process,new File("inpfinal"));
cit = new ChildInputThread(process,new File("output.txt"),cot);
cot.start();
cit.start();
process.waitFor();
}catch(Exception e){e.printStackTrace();}
finally{
try{
if(cit!=null & cot!=null){
cot.CONTINUE=false;
cit.CONTINUE=false;
cit.join();
cot.join();
}
System.out.println("Process terminated:");
System.out.println("completed");
spw=new PrintWriter(new BufferedWriter(new
OutputStreamWriter(s.getOutputStream()) ));
spw.println("completed");
spw.flush();
spw=null;
}catch(Exception e){e.printStackTrace();}
}
break;
case 3: try{
BufferedReader br=new BufferedReader(new InputStreamReader(new
FileInputStream(new File("output.txt"))));
spw=new PrintWriter(new BufferedWriter(new
OutputStreamWriter(s.getOutputStream()) ));
String tmp="";
if(br!=null)
while((tmp=br.readLine())!=null){
spw.println(tmp);
spw.flush();
}
if(br!=null)
br.close();
}catch(Exception e){e.printStackTrace();}
finally{spw=null;}
break;
case 4: try{
BufferedReader br=new BufferedReader(new InputStreamReader(new
FileInputStream(new File("/home/payroll/rpts/chq.rpt"))));
//BufferedReader br=new BufferedReader(new
InputStreamReader(new FileReader("/home/payroll/rpts/chq.rpt")));
spw=new PrintWriter(new BufferedWriter(new
OutputStreamWriter(s.getOutputStream())));
String tmp1="";
if(br!=null)
while((tmp1=br.readLine())!=null){
spw.println(tmp1);
spw.flush();
}
if(br!=null){
br.close();
}
}catch(Exception e){e.printStackTrace();}
finally{spw=null;}
break;
default: System.out.println(" command");
}
}catch(Exception e){e.printStackTrace();}
}//run method
}//ServerThread class ends here.
class ChildInputThread extends Thread
{
Process process=null;
BufferedReader cbr;
BufferedReader ErrorReader=null;
boolean CONTINUE = true;
PrintWriter fos=null;
ChildOutputThread cot;//For MONITORING OF THE WRITE FLAG OF THE CHILD
WRITE THREAD.
public ChildInputThread(Process process,File
childOut,ChildOutputThread cot)
{
super("inputthread");
try{
this.process = process;
this.cot=cot;
cbr = new BufferedReader(new
InputStreamReader(process.getInputStream()));
ErrorReader= new BufferedReader(new
InputStreamReader(process.getErrorStream()));
fos=new PrintWriter(new BufferedWriter(new OutputStreamWriter(new
FileOutputStream(childOut))));
fos.println("file opened and writing into file has been working");
fos.flush();
System.out.println("child inputthread initialised.:From"+getName());
} catch(IOException e){e.printStackTrace();}
}
public void run()
{
try{
//System.out.println("child thread run mehod
entereeD:Thread:"+getName());
int writeflag=0;
while(CONTINUE)
{
//sleep(500);
//System.out.println("back form wait:thread"+getName());
if(cbr.ready())
{
while(cbr.ready())
{
//writeflag=1;
char c=(char)cbr.read();
fos.print(c);
if(c=='?')
writeflag=1;
System.out.print(c);
}
//System.out.println("WRITE FLAG"+ writeflag);
}
else
{
fos.flush();
//System.out.println("\nsangameshwar input thread in else
part:"+getName());
try{
if(cot.write==true)
Thread.sleep(1000);
else
if(writeflag==1)
{
cot.complement(this,fos);
writeflag=0;
}
}catch(Exception e){e.printStackTrace();}
}//ELSE PART
}//while
while(cbr.ready())
fos.println(cbr.readLine());
fos.flush();
fos.println("------------------------------Error
messages:----------------------------------");
while(ErrorReader.ready())
fos.println(ErrorReader.readLine());
fos.println("completed");
fos.flush();
fos.close();
//start here.
}catch(Exception e){e.printStackTrace();}
}
}//ChildInputThread ends
class ChildOutputThread extends Thread
{
ChildInputThread cit;
Process process=null;
PrintWriter cpw;
boolean CONTINUE = true;
boolean write = false;
BufferedReader fir=null;
PrintWriter fos=null;
public ChildOutputThread(Process process,File childIn)
{
super("outputthread");
try{
this.process = process;
cpw = new PrintWriter(new BufferedWriter(new
OutputStreamWriter(process.getOutputStream()) ));
fir = new BufferedReader(new InputStreamReader(new
FileInputStream(childIn)));
System.out.println("child outputthread initialised.:");
} catch(IOException e){e.printStackTrace();}
}//CLASS CONSTUCTOR ENDS HERE.
public void run()
{
try{
System.out.println("child thread run mehod
entereeD:Thread:"+getName());
char c=(char)fir.read();
while(CONTINUE)
if(write==true)
{
System.out.println("childoutput thread writing into
buffer:"+getName());
cpw.println(c);
cpw.flush();
fos.println(c);
fos.flush();
System.out.println("from writer--------------------"+c);
complementwrite();
}
else
{
try{
Thread.sleep(1000);
}catch(InterruptedException e){e.printStackTrace();}
}
}catch(IOException e){e.printStackTrace();}
synchronized(this){notify();}
}//run method
public synchronized void complement(ChildInputThread cit,PrintWriter
fos)
{
this.cit=cit;
this.fos=fos;
try{
write = !write;
Thread t=Thread.currentThread();
//System.out.println("thisis from complement method"+t.getName());
//this.notify();comomented just now
wait();
}catch(Exception e){e.printStackTrace();}
}
// Synchronized complement method ends.
public synchronized void complementwrite()
{
try{
write=!write;
notify();
}catch(Exception e){e.printStackTrace();}
}
}
compiling fine but at run time it is giving the following exception
Exception in thread "main" java.lang.NoClassDefFoundError: tcpServer5
i am using jdk1.5 and the file is running on linux.
How do i set the classpath in linux
My .java file is below
import java.net.*;
import java.io.*;
//Server class.
public class tcpServer5
{
public static void main(String[] args)
{
int port;
ServerSocket server_socket;
port=Integer.parseInt(args[0]);
try{
server_socket=new ServerSocket(port);
System.out.println("Server Waiting for Client on Port
"+server_socket.getLocalPort());
while(true)
{
Socket socket=server_socket.accept();
System.out.println("New Connection
Accepted"+socket.getInetAddress()+":"+socket.getPort());
//FIRE A NEW SERVER THREAD FOR EACH CLIENT.
ServerThread st=new ServerThread(socket);
st.start();
}
}catch(Exception e){System.out.println("Connection Reset");}
}//main ends
}//class ends
//ServerThread
class ServerThread extends Thread
{
Socket s;
public ServerThread(Socket s)
{
this.s=s;
}
public void run()
{
Runtime r=Runtime.getRuntime();
PrintWriter spw=null;
BufferedReader sbr=null;
ChildOutputThread cot=null;
ChildInputThread cit=null;
try{
sbr= new BufferedReader(new
InputStreamReader(s.getInputStream()));
while(!sbr.ready())
Thread.sleep(500);
String command=sbr.readLine();
Process process=r.exec(command);
System.out.println("Process started with this command :"+command);
Thread t=Thread.currentThread();
t.sleep(1000);
int CASE=0;
if(command.endsWith("<inptrial>"))
CASE=1;
if(command.endsWith("<inpfinal>"))
CASE=2;
if(command.endsWith("output.txt"))
CASE=3;
if(command.endsWith("cfoil.RPT"))
CASE=4;
//managing child threads.
switch(CASE){
case 1: try{
cot = new ChildOutputThread(process,new File("inptrial"));
cit = new ChildInputThread(process,new File("output.txt"),cot);
cot.start();
cit.start();
process.waitFor();
}catch(Exception e){e.printStackTrace();}
finally{
try{
if(cit!=null & cot!=null){
cot.CONTINUE=false;
cit.CONTINUE=false;
cit.join();
cot.join();
}
System.out.println("Process terminated:");
System.out.println("completed");
spw=new PrintWriter(new BufferedWriter(new
OutputStreamWriter(s.getOutputStream()) ));
spw.println("completed");
spw.flush();
spw=null;
}catch(Exception e){e.printStackTrace();}
}
break;
case 2: try{
cot = new ChildOutputThread(process,new File("inpfinal"));
cit = new ChildInputThread(process,new File("output.txt"),cot);
cot.start();
cit.start();
process.waitFor();
}catch(Exception e){e.printStackTrace();}
finally{
try{
if(cit!=null & cot!=null){
cot.CONTINUE=false;
cit.CONTINUE=false;
cit.join();
cot.join();
}
System.out.println("Process terminated:");
System.out.println("completed");
spw=new PrintWriter(new BufferedWriter(new
OutputStreamWriter(s.getOutputStream()) ));
spw.println("completed");
spw.flush();
spw=null;
}catch(Exception e){e.printStackTrace();}
}
break;
case 3: try{
BufferedReader br=new BufferedReader(new InputStreamReader(new
FileInputStream(new File("output.txt"))));
spw=new PrintWriter(new BufferedWriter(new
OutputStreamWriter(s.getOutputStream()) ));
String tmp="";
if(br!=null)
while((tmp=br.readLine())!=null){
spw.println(tmp);
spw.flush();
}
if(br!=null)
br.close();
}catch(Exception e){e.printStackTrace();}
finally{spw=null;}
break;
case 4: try{
BufferedReader br=new BufferedReader(new InputStreamReader(new
FileInputStream(new File("/home/payroll/rpts/chq.rpt"))));
//BufferedReader br=new BufferedReader(new
InputStreamReader(new FileReader("/home/payroll/rpts/chq.rpt")));
spw=new PrintWriter(new BufferedWriter(new
OutputStreamWriter(s.getOutputStream())));
String tmp1="";
if(br!=null)
while((tmp1=br.readLine())!=null){
spw.println(tmp1);
spw.flush();
}
if(br!=null){
br.close();
}
}catch(Exception e){e.printStackTrace();}
finally{spw=null;}
break;
default: System.out.println(" command");
}
}catch(Exception e){e.printStackTrace();}
}//run method
}//ServerThread class ends here.
class ChildInputThread extends Thread
{
Process process=null;
BufferedReader cbr;
BufferedReader ErrorReader=null;
boolean CONTINUE = true;
PrintWriter fos=null;
ChildOutputThread cot;//For MONITORING OF THE WRITE FLAG OF THE CHILD
WRITE THREAD.
public ChildInputThread(Process process,File
childOut,ChildOutputThread cot)
{
super("inputthread");
try{
this.process = process;
this.cot=cot;
cbr = new BufferedReader(new
InputStreamReader(process.getInputStream()));
ErrorReader= new BufferedReader(new
InputStreamReader(process.getErrorStream()));
fos=new PrintWriter(new BufferedWriter(new OutputStreamWriter(new
FileOutputStream(childOut))));
fos.println("file opened and writing into file has been working");
fos.flush();
System.out.println("child inputthread initialised.:From"+getName());
} catch(IOException e){e.printStackTrace();}
}
public void run()
{
try{
//System.out.println("child thread run mehod
entereeD:Thread:"+getName());
int writeflag=0;
while(CONTINUE)
{
//sleep(500);
//System.out.println("back form wait:thread"+getName());
if(cbr.ready())
{
while(cbr.ready())
{
//writeflag=1;
char c=(char)cbr.read();
fos.print(c);
if(c=='?')
writeflag=1;
System.out.print(c);
}
//System.out.println("WRITE FLAG"+ writeflag);
}
else
{
fos.flush();
//System.out.println("\nsangameshwar input thread in else
part:"+getName());
try{
if(cot.write==true)
Thread.sleep(1000);
else
if(writeflag==1)
{
cot.complement(this,fos);
writeflag=0;
}
}catch(Exception e){e.printStackTrace();}
}//ELSE PART
}//while
while(cbr.ready())
fos.println(cbr.readLine());
fos.flush();
fos.println("------------------------------Error
messages:----------------------------------");
while(ErrorReader.ready())
fos.println(ErrorReader.readLine());
fos.println("completed");
fos.flush();
fos.close();
//start here.
}catch(Exception e){e.printStackTrace();}
}
}//ChildInputThread ends
class ChildOutputThread extends Thread
{
ChildInputThread cit;
Process process=null;
PrintWriter cpw;
boolean CONTINUE = true;
boolean write = false;
BufferedReader fir=null;
PrintWriter fos=null;
public ChildOutputThread(Process process,File childIn)
{
super("outputthread");
try{
this.process = process;
cpw = new PrintWriter(new BufferedWriter(new
OutputStreamWriter(process.getOutputStream()) ));
fir = new BufferedReader(new InputStreamReader(new
FileInputStream(childIn)));
System.out.println("child outputthread initialised.:");
} catch(IOException e){e.printStackTrace();}
}//CLASS CONSTUCTOR ENDS HERE.
public void run()
{
try{
System.out.println("child thread run mehod
entereeD:Thread:"+getName());
char c=(char)fir.read();
while(CONTINUE)
if(write==true)
{
System.out.println("childoutput thread writing into
buffer:"+getName());
cpw.println(c);
cpw.flush();
fos.println(c);
fos.flush();
System.out.println("from writer--------------------"+c);
complementwrite();
}
else
{
try{
Thread.sleep(1000);
}catch(InterruptedException e){e.printStackTrace();}
}
}catch(IOException e){e.printStackTrace();}
synchronized(this){notify();}
}//run method
public synchronized void complement(ChildInputThread cit,PrintWriter
fos)
{
this.cit=cit;
this.fos=fos;
try{
write = !write;
Thread t=Thread.currentThread();
//System.out.println("thisis from complement method"+t.getName());
//this.notify();comomented just now
wait();
}catch(Exception e){e.printStackTrace();}
}
// Synchronized complement method ends.
public synchronized void complementwrite()
{
try{
write=!write;
notify();
}catch(Exception e){e.printStackTrace();}
}
}