public void actionPerformed(ActionEvent e)

{

compile();

}

};

JMenuItem item3 = file.add(action3);

opt.add(item3);

r
un=new JMenu("Run");

run.setMnemonic('R');

Action action4 = new AbstractAction("Run")

{


public void actionPerformed(ActionEvent e)


{


run();

}


};

61


JMenuItem item4 = file.add(action4);

opt.add(item4); help=new JMenu("Help");

jm.add(help); open=new JMenu("open
");


Action action1 = new AbstractAction("Open")


{


public void actionPerformed(ActionEvent e)

{ open(); } };

JMenuItem item1 = file.add(action1);


file.add(item1); exit=new JMenu("Exit");

Action action2 = new AbstractAction("Exit")


{

public void actionPerfor
med(ActionEvent e)


{


System.exit(0); } };


JMenuItem item2 = file.add(action2);



file.add(item2);

jm.setBounds(5, 0, 880, 30);

frame.add(jm);

frame.setResizable(false);


frame.setVisible(true);

jta1.addMouseListener(this);


jta1.addMouseMotionListener(th
is);

jtp1.addMouseListener(this); jtp2.addMouseListener(this);

}


public void mouseClicked(MouseEvent ew)


{

if(ew.getSource()==jta1)

{

jl3.setText("Line
-
No: "+Integer.toString(getCurrentLineNumber()));

}

else if(ew.getSource()==jtp2)

{


if(jtp1.isShowing())


{

frame.remove(jtp1);


jtp2.setBounds(10, 40, 670, 560);

jl1.setBounds(500, 535, 670, 150);


jl2.setBounds(550, 535, 670, 150);

jl3.setBounds(600, 535, 670, 150);


jta2.addMouseMotionListener(this);

jl3.setText("Line
-
No: "+Integer.toString(getCurrentLineNumbe
r()));


}

else { frame.add(jtp1);


jtp2.setBounds(10, 450, 670, 150);

jl1.setBounds(500, 380, 670, 150);


jl2.setBounds(550, 380, 670, 150);


jl3.setBounds(600, 380, 670, 150);

jta2.removeMouseMotionListener(this);


} }

else if(ew.getSource()==jtp1)

62



{


if(jtp2.i
sShowing())


{ frame.remove(jtp2);


frame.remove(jtp3); jtp1.setBounds(10, 40, 870, 560);

jl1.setBounds(500, 535, 670, 150);

jl2.setBounds(550, 535, 670, 150);


jl3.setBounds(600, 535, 670, 150);


}

else { frame.add(jtp2); frame.add(jtp3);

jtp1.setBounds
(10, 40, 670, 400);


jl1.setBounds(500, 380, 670, 150);


jl2.setBounds(550, 380, 670, 150);

jl3.setBounds(600, 380, 670, 150); } } }


public void mouseEntered(MouseEvent ew)


{} public void mouseExited(MouseEvent ew) {}

public void mousePressed(MouseEvent ew)
{}


public void mouseReleased(MouseEvent ew) {}

public void mouseMoved(MouseEvent e)

{ jl1.setText("X
-
: "+Integer.toString(e.getX()));


jl2.setText("Y
-
: "+Integer.toString(e.getY())); }


public void mouseDragged(MouseEvent e) {}


public void actionPerformed(Ac
tionEvent ae) {}


public void open()


{


JFileChooser fileChooser = new JFileChooser();

fileChooser.addChoosableFileFilter(new MyFilter());

int select=fileChooser.showOpenDialog(frame);


if(select==JFileChooser.APPROVE_OPTION) {


File file=fileChooser.getSelect
edFile();


String filename=fileChooser.getSelectedFile().getName();

try { FileInputStream fis=new FileInputStream(file);

int n=fis.available(); byte dat[]=new byte[n];

fis.read(dat); String data=new String(dat);


jtp1.setTitleAt(0, filename);


jta1.setText(da
ta); opt.setEnabled(true); }

catch(Exception e)


{ e.printStackTrace(); } }


}

public int getCurrentLineNumber()


{


int line;


int caretPosition = jta1.getCaretPosition();


Element root = jta1.getDocument().getDefaultRootElement();


line = root.getElementI
ndex(caretPosition) + 1; return line;

}

public void compile() { try { jtp2.setTitleAt(0,"Compile");


String ta1=jta1.getText().trim();

StringBuffer sb=new StringBuffer(ta1);


int id1=ta1.indexOf("public class");

int id2=ta1.indexOf(" ",id1+13);

63


String name=t
a1.substring(id1, id2).trim();


StringTokenizer st3=new StringTokenizer(name,"
\
n");


System.out.println(st3.countTokens());

String word=st3.nextToken().toString().trim(); System.out.println(word+"*");

StringTokenizer st4=new StringTokenizer(word," "); System
.out.println(st4.countTokens());

st4.nextToken(); st4.nextToken();

pureFile=st4.nextToken().toString().trim();

className=pureFile+".java"; //System.out.println(st4.nextElement().toString().trim()+"*");
FileOutputStream f=new FileOutputStream(className);


f.
write(ta1.getBytes());

f.close();


f2=new File(className); f3=new File(pureFile+".class");


System.out.println(f2.getAbsolutePath());

String path=f2.getAbsolutePath(); int a1=path.indexOf("
\
\
");

int a2=path.lastIndexOf("
\
\
");


System.out.println(a1+" "+a2);

S
tring colan=path.substring(0, a1).trim();

String location=path.substring(0, a2+1).trim();

System.out.println(colan);


System.out.println(location);

compiler(className); }




catch (Exception err)


{ err.printStackTrace();

} //option=JOptionPane.showInputD
ialog(null,"Enter Destination System
Name","Destination",1).toString(); //System.out.println(option); // jta2.setText(line); }

public void run() { jtp2.setTitleAt(0,"Run");

runer(pureFile); }


public static void main(String args[])

{


try

{

UIManager.setLook
AndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");


}

catch (Exception e) { } new Client(); }

public void compiler(String name)

{ try { jta2.setText("");


jta2.append("Compilation Started.....
\
n");


jta2.append("Proceed.....
\
n");


jta2.setForegroun
d(Color.blue);


String callAndArgs= "cmd /c compile.bat "+name; Process p =Runtime.getRuntime().exec(callAndArgs);

BufferedReader br=new BufferedReader(new InputStreamReader(p.getInputStream()));

String str=br.readLine();

while(str!=null)


{

System.out.print
ln(str);

str=br.readLine(); }


File f1 = new File("error.txt");

FileReader fr = new FileReader(f1);


BufferedReader br1 = new BufferedReader(fr);

64


StringBuffer sb1 = new StringBuffer();


String eachLine = br1.readLine();

while (eachLine != null)

{

jta2.setFore
ground(Color.RED);

sb1.append(eachLine);

sb1.append("
\
n");

eachLine= br1.readLine(); }


jta2.append(sb1.toString());

//input.close(); if(f1.length()==0)


{

jta2.append("Compiled Successfully........
\
n");


jta2.append("Done........"); } } catch(Exception

e)


{ e.printStackTrace(); } }


public void runer(String name)


{


try { jta3.setText("");

String callAndArgs= "cmd /c run.bat "+name;

Process p =Runtime.getRuntime().exec(callAndArgs);

BufferedReader br=new BufferedReader(new InputStreamReader(p.getInputStre
am())); String
str=br.readLine(); while(str!=null)


{

System.out.println(str); str=br.readLine();


}


File f1 = new File("output.txt");


FileReader fr = new FileReader(f1);


BufferedReader br1 = new BufferedReader(fr);


StringBuffer sb1 = new StringBuffer();

Str
ing eachLine = br1.readLine();


while (eachLine != null)

{


sb1.append(eachLine);


sb1.append("
\
n");

eachLine = br1.readLine();

}

String sp=sb1.toString();

StringBuffer st=new StringBuffer(sp);

System.out.println(st.length()); int indx=sp.indexOf(">"); int r=
1;


while(indx !=
-
1)

{


System.out.println(Integer.toString(indx)+"*");

System.out.println(st);

st.insert(indx+r, "
\
n");


indx=sp.indexOf(">",indx+1);


r++;

System.out.println(Integer.toString(indx)+"#");


}

jta2.setText(st.toString()); f2.deleteOnExit()
;


f3.deleteOnExit(); }


catch(Exception e)

{ e.printStackTrace(); } } }

65


class MyFilter extends javax.swing.filechooser.FileFilter

{


public boolean accept(File file)

{

return file.isDirectory() || file.getName().endsWith(".java");

}

public String getDescript
ion()


{ return "*.java"; } }



/* Test Frame Design */


import javax.swing.*;


import java.awt.*;

public class Test {


public Test() { JFrame f = new JFrame("Test");

f.setLayout(new FlowLayout());


JTextField jt1 = new JTextField(25);


f.add(jt1); f.setVisi
ble(true);


f.setSize(200,200);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


}

public static void main(String args[])

{

new Test();


}


}









OUTPUT:


D:
\

Java
\
jdk1.5.0_03
\
bin>javac Client.java

D:
\

Java
\
jdk1.5.0_03
\
bin>javac Test.java

D:
\

Java
\
jdk1.5.0_
03
\
bin>java Test

D:
\

Java
\
jdk1.5.0_03
\
bin>java Client







66







Result:



Thus the above program was
executed and

verified successfully.