| | |
Making A Java Compiler... In Java
![]() |
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.
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.
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
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
-Sam Keen, from To Love and Be Loved
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.
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.
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.
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.
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
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.
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.
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.
Java Syntax (Toggle Plain Text)
import javax.swing.*; import javax.swing.event.*; import java.awt.*; import java.awt.event.*; import java.io.*; public class JEdit extends JFrame implements ActionListener { JButton open = new JButton("Open"); JButton save = new JButton("Save"); JButton compile = new JButton("Compile"); JButton run = new JButton("Run"); JButton help = new JButton("Help"); JTextArea code = new JTextArea("",28,65); JTextArea output = new JTextArea("",7,65); File fileS = null; Font cf = new Font("monospaced",Font.PLAIN,12); public JEdit() { super("Java CinnaComp"); setSize(500,710); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); Container contentArea = getContentPane(); contentArea.setBackground(Color.lightGray); FlowLayout flowManager = new FlowLayout(); contentArea.setLayout(flowManager); contentArea.add(save); save.addActionListener(this); contentArea.add(open); open.addActionListener(this); contentArea.add(compile); compile.addActionListener(this); contentArea.add(run); run.addActionListener(this); contentArea.add(help); help.addActionListener(this); JScrollPane scrollPane = new JScrollPane(code, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); contentArea.add(scrollPane); code.setFont(cf); JScrollPane scrollPane2 = new JScrollPane(output, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); contentArea.add(scrollPane2); output.setFont(cf); setContentPane(contentArea); } public void actionPerformed(ActionEvent event) { if(event.getSource() == save) { JFileChooser saveChooser = new JFileChooser(); saveChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); int result = saveChooser.showSaveDialog(this); if(result == JFileChooser.CANCEL_OPTION) return; fileS = saveChooser.getSelectedFile(); if(fileS == null || fileS.getName().equals("")) JOptionPane.showMessageDialog(null, "Invalid File Name", "Invalid File Name", JOptionPane.ERROR_MESSAGE); else { try { FileWriter saveFile = new FileWriter(fileS.getPath()); BufferedWriter bufferS = new BufferedWriter(saveFile); bufferS.write(code.getText()); bufferS.close(); } catch (Exception e1) { } } } if(event.getSource() == open) { JFileChooser openChooser = new JFileChooser(); openChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); int result2 = openChooser.showOpenDialog(this); if(result2 == JFileChooser.CANCEL_OPTION) return; File fileO = openChooser.getSelectedFile(); if(fileO == null || fileO.getName().equals("")) JOptionPane.showMessageDialog(null, "Invalid File Name", "Invalid File Name", JOptionPane.ERROR_MESSAGE); else { try { FileReader openFile = new FileReader(fileO.getPath()); BufferedReader bufferO = new BufferedReader(openFile); String textline = null; while((textline = bufferO.readLine()) != null) { code.append(textline+"\n"); } bufferO.close(); } catch (Exception e2) { } } } if(event.getSource()==compile) { try { Process compile = Runtime.getRuntime().exec("javac -cp " + fileS.getPath()); output.setText(compile.getErrorStream().toString()); } catch(Exception e3) { output.append("\n"+e3.toString()); } } if (event.getSource() == run) { try { Process run = Runtime.getRuntime().exec("java -cp " + fileS.getParent()+fileS.getName()); output.setText(run.getErrorStream().toString()); output.append("\n"+run.getOutputStream().toString()); } catch (Exception e4) { output.append("\n" + e4.toString()); } } } public static void main (String [] args) { new JEdit(); } }
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
http://supportweb.cs.bham.ac.uk/docu...s/ant/ant.html
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.

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.
![]() |
Similar Threads
- Running java command thru java program (Java)
- get path to java compiler (Java)
- Java Compiler (Java)
- java compiler (Java)
Other Threads in the Java Forum
- Previous Thread: Need help for making an address book using Java
- Next Thread: jdbctemplate callablestatement
| Thread Tools | Search this Thread |
-xlint add android api applet application applications array arrays automation bank bi binary blackberry bluetooth chat class clear client code compile compiler component database development digit eclipse equation error event formatingtextintooltipjava fractal freeze functiontesting game gameprogramming givemetehcodez graphics gui health html hyper ide idea image infinite int integer j2me java javame javaprojects jetbrains jni jpanel jtable julia learningresources linux list main map method methods mobile myregfun netbeans nonstatic notdisplaying openjavafx pearl problem program project qt recursion repositories scanner screen scrollbar server set sms sort sorting spamblocker sql sqlserver state storm string superclass swing system thread threads tree variablebinding windows xor






