| Код | import java.awt.*; import java.awt.event.*; import java.io.File; import java.io.FileOutputStream; import java.io.PrintWriter; import java.io.IOException; import java.lang.reflect.Method; import java.lang.reflect.InvocationTargetException; import java.util.Date;
/** * Program that lets you run a short segment of Java code. * * @author Shawn Silverman */ public class JavaStatement extends Frame implements ActionListener { /** * Main entry point. */ public static void main(String[] args) { // Create the frame and set it to close on exit
Frame f = new JavaStatement(); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { Window w = e.getWindow(); w.hide(); w.dispose(); System.exit(0); } });
f.pack(); f.show(); }
/** The compiler object. */ private com.sun.tools.javac.Main javac = new com.sun.tools.javac.Main();
private TextArea text; private Button runBtn;
/** * Create a new JavaStatement window. */ public JavaStatement() { super("Java Statement");
// Create the components
text = new TextArea(5, 45); text.setFont(new Font("Monospaced", Font.PLAIN, 12));
runBtn = new Button("Run!"); runBtn.addActionListener(this);
// Lay them out
this.add(text, BorderLayout.CENTER); Panel p = new Panel(); p.add(runBtn); this.add(p, BorderLayout.SOUTH);
this.setBackground(SystemColor.control); }
/** * Shows a message to the user. * * @param msg the message */ private void showMsg(String msg) { System.err.println(msg); }
/** * Compiles and runs the short code segment. * * @throws IOException if there was an error creating the source file. */ private void doRun() throws IOException { // Create a temp. file
File file = File.createTempFile("jav", ".java", new File(System.getProperty("user.dir")));
// Set the file to be deleted on exit
file.deleteOnExit();
// Get the file name and extract a class name from it
String filename = file.getName(); String classname = filename.substring(0, filename.length()-5);
// Output the source
PrintWriter out = new PrintWriter(new FileOutputStream(file)); out.println("/**"); out.println(" * Source created on " + new Date()); out.println(" */"); out.println("public class " + classname + " {"); out.println(" public static void main(String[] args) throws Exception {");
// Your short code segment
out.print(" "); out.println(text.getText());
out.println(" }"); out.println("}");
// Flush and close the stream
out.flush(); out.close();
// Compile
String[] args = new String[] { "-d", System.getProperty("user.dir"), filename }; int status = javac.compile(args);
// Run
switch (status) { case 0: // OK // Make the class file temporary as well
new File(file.getParent(), classname + ".class").deleteOnExit();
try { // Try to access the class and run its main method
Class clazz = Class.forName(classname); Method main = clazz.getMethod("main", new Class[] { String[].class }); main.invoke(null, new Object[] { new String[0] }); } catch (InvocationTargetException ex) { // Exception in the main method that we just tried to run
showMsg("Exception in main: " + ex.getTargetException()); ex.getTargetException().printStackTrace(); } catch (Exception ex) { showMsg(ex.toString()); } break; case 1: showMsg("Compile status: ERROR"); break; case 2: showMsg("Compile status: CMDERR"); break; case 3: showMsg("Compile status: SYSERR"); break; case 4: showMsg("Compile status: ABNORMAL"); break; default: showMsg("Compile status: Unknown exit status"); } }
/** * Listens to actions from the run button. */ public void actionPerformed(ActionEvent e) { Object source = e.getSource();
if (runBtn == source) { // Disable the run button while we're running
runBtn.setEnabled(false); try { doRun(); } catch (IOException ex) { ex.printStackTrace(); } runBtn.setEnabled(true); } } }
| |