Making A Java Compiler... In Java

Reply

Join Date: Aug 2004
Posts: 350
Reputation: Ghost is an unknown quantity at this point 
Solved Threads: 2
Ghost's Avatar
Ghost Ghost is offline Offline
Posting Whiz

Making A Java Compiler... In Java

 
0
  #1
Aug 4th, 2005
Hi,

Is it possible to create a Java compiler? Could you make a Notepad-like application, save the code as a .java file, compile it into a .class file, and run it? Basically, I want to combine Notepad and Command Prompt. I just need help on the compiling part (and listing the errors, if any).

Thanks in advanced for your help.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 45
Reputation: cheenu78 is an unknown quantity at this point 
Solved Threads: 0
cheenu78's Avatar
cheenu78 cheenu78 is offline Offline
Light Poster

Re: Making A Java Compiler... In Java

 
0
  #2
Aug 4th, 2005
hi,
I had once made a full fledge editor for java in java. All i did was to include the classpath while issuing the javac command.

I used
Process process=Runtime.getRuntime().exec("javac -cp <java file>");
then I used err stream and output stream to read the output.
The same i used for java command as well.

regards
srinivas
We come to love not by finding a perfect person, but by learning to see an imperfect person perfectly.

-Sam Keen, from To Love and Be Loved
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,144
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Making A Java Compiler... In Java

 
0
  #3
Aug 4th, 2005
To run a class you don't need to launch the java executable.
Create a classloader and use that to fork off the class in-process

You can also do that to compile classes. Simply hook into Ant which is a Java application and can compile Java classes for you (by hooking into the JDK).
Examining the Ant source should be interesting (I've not yet done so) to see how they do it in a platform independent way.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 350
Reputation: Ghost is an unknown quantity at this point 
Solved Threads: 2
Ghost's Avatar
Ghost Ghost is offline Offline
Posting Whiz

Re: Making A Java Compiler... In Java

 
0
  #4
Aug 4th, 2005
thanks guys!

jwenting - WHOA!!!!!!!! You're approach is really confusing! I think I'll stick with the other method but thanks for your reply.

cheenu78 - thanks! can u explain how to get the error messages a little more. Thanks again.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,144
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Making A Java Compiler... In Java

 
0
  #5
Aug 4th, 2005
confusing? It's what I'd do. It's platform independent which is important as I work on several operating systems at the same time (Windows, 2 versions of Linux, and AIX, and I hope to add a Mac sometime next year).
And calling Ant should be rather easy. probably easier in fact than capturing the commandline output.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 350
Reputation: Ghost is an unknown quantity at this point 
Solved Threads: 2
Ghost's Avatar
Ghost Ghost is offline Offline
Posting Whiz

Re: Making A Java Compiler... In Java

 
0
  #6
Aug 4th, 2005
would you mind explaining how to use Ant then??

Thank you very much for your help.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,144
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Making A Java Compiler... In Java

 
0
  #7
Aug 4th, 2005
Ant is a tool similar to make (which is for C/C++ mainly).
It's written completely in Java so can easily integrate with Java applications.

There's books written about it, I'm barely scratching the surface by using it from the commandline to compile and package my projects.
Maybe next year when I may have time to do more I'm going to really dive into it

http://ant.apache.org
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 350
Reputation: Ghost is an unknown quantity at this point 
Solved Threads: 2
Ghost's Avatar
Ghost Ghost is offline Offline
Posting Whiz

Re: Making A Java Compiler... In Java

 
0
  #8
Aug 4th, 2005
Hi!

I started to make the compiler but I have some problems. First, it won't compile. Second, I can't get the error messages/output to appear. My code is below. Thanks in advanced for your help.

  1. import javax.swing.*;
  2. import javax.swing.event.*;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import java.io.*;
  6.  
  7. public class JEdit extends JFrame implements ActionListener
  8. {
  9.  
  10. JButton open = new JButton("Open");
  11. JButton save = new JButton("Save");
  12. JButton compile = new JButton("Compile");
  13. JButton run = new JButton("Run");
  14. JButton help = new JButton("Help");
  15.  
  16. JTextArea code = new JTextArea("",28,65);
  17. JTextArea output = new JTextArea("",7,65);
  18.  
  19. File fileS = null;
  20.  
  21. Font cf = new Font("monospaced",Font.PLAIN,12);
  22.  
  23. public JEdit()
  24. {
  25. super("Java CinnaComp");
  26. setSize(500,710);
  27. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  28. setVisible(true);
  29.  
  30. Container contentArea = getContentPane();
  31. contentArea.setBackground(Color.lightGray);
  32.  
  33. FlowLayout flowManager = new FlowLayout();
  34. contentArea.setLayout(flowManager);
  35.  
  36. contentArea.add(save);
  37. save.addActionListener(this);
  38. contentArea.add(open);
  39. open.addActionListener(this);
  40. contentArea.add(compile);
  41. compile.addActionListener(this);
  42. contentArea.add(run);
  43. run.addActionListener(this);
  44. contentArea.add(help);
  45. help.addActionListener(this);
  46. JScrollPane scrollPane = new JScrollPane(code,
  47. JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
  48. JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
  49. contentArea.add(scrollPane);
  50. code.setFont(cf);
  51. JScrollPane scrollPane2 = new JScrollPane(output,
  52. JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
  53. JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
  54. contentArea.add(scrollPane2);
  55. output.setFont(cf);
  56.  
  57.  
  58. setContentPane(contentArea);
  59. }
  60.  
  61. public void actionPerformed(ActionEvent event)
  62. {
  63. if(event.getSource() == save)
  64. {
  65. JFileChooser saveChooser = new JFileChooser();
  66. saveChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
  67. int result = saveChooser.showSaveDialog(this);
  68. if(result == JFileChooser.CANCEL_OPTION)
  69. return;
  70. fileS = saveChooser.getSelectedFile();
  71. if(fileS == null || fileS.getName().equals(""))
  72. JOptionPane.showMessageDialog(null, "Invalid File Name",
  73. "Invalid File Name", JOptionPane.ERROR_MESSAGE);
  74. else
  75. {
  76. try
  77. {
  78. FileWriter saveFile = new FileWriter(fileS.getPath());
  79. BufferedWriter bufferS = new BufferedWriter(saveFile);
  80. bufferS.write(code.getText());
  81. bufferS.close();
  82. }
  83. catch (Exception e1)
  84. {
  85. }
  86. }
  87. }
  88.  
  89. if(event.getSource() == open)
  90. {
  91. JFileChooser openChooser = new JFileChooser();
  92. openChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
  93. int result2 = openChooser.showOpenDialog(this);
  94. if(result2 == JFileChooser.CANCEL_OPTION)
  95. return;
  96. File fileO = openChooser.getSelectedFile();
  97. if(fileO == null || fileO.getName().equals(""))
  98. JOptionPane.showMessageDialog(null, "Invalid File Name",
  99. "Invalid File Name", JOptionPane.ERROR_MESSAGE);
  100. else
  101. {
  102. try
  103. {
  104. FileReader openFile = new FileReader(fileO.getPath());
  105. BufferedReader bufferO = new BufferedReader(openFile);
  106. String textline = null;
  107. while((textline = bufferO.readLine()) != null)
  108. {
  109. code.append(textline+"\n");
  110. }
  111. bufferO.close();
  112. }
  113. catch (Exception e2)
  114. {
  115. }
  116. }
  117. }
  118.  
  119. if(event.getSource()==compile)
  120. {
  121. try
  122. {
  123. Process compile = Runtime.getRuntime().exec("javac -cp " +
  124. fileS.getPath());
  125. output.setText(compile.getErrorStream().toString());
  126. }
  127. catch(Exception e3)
  128. {
  129. output.append("\n"+e3.toString());
  130. }
  131. }
  132.  
  133. if (event.getSource() == run) {
  134. try {
  135. Process run = Runtime.getRuntime().exec("java -cp " +
  136. fileS.getParent()+fileS.getName());
  137. output.setText(run.getErrorStream().toString());
  138. output.append("\n"+run.getOutputStream().toString());
  139. }
  140. catch (Exception e4) {
  141. output.append("\n" + e4.toString());
  142. }
  143. }
  144.  
  145. }
  146.  
  147.  
  148. public static void main (String [] args)
  149. {
  150. new JEdit();
  151. }
  152. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 58
Reputation: JeffHeaton is an unknown quantity at this point 
Solved Threads: 0
JeffHeaton's Avatar
JeffHeaton JeffHeaton is offline Offline
Junior Poster in Training

Re: Making A Java Compiler... In Java

 
0
  #9
Aug 4th, 2005
Just using ant is not too hard, you run the "ant" command from a directory that has a build.xml file. Constructing the build.xml file to do what you want is a little more complex. There are many tutorials to do that. Like this one

http://supportweb.cs.bham.ac.uk/docu...s/ant/ant.html
Jeff Heaton
Check out my Java Neural Networks
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,144
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Making A Java Compiler... In Java

 
0
  #10
Aug 5th, 2005
The trick is building the xml file to have ant do tricks like run unit tests, deploy your application to a J2EE server, and things like that

And of course integrating Ant inside another application is a bit more involved than just calling the command from the prompt.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC