加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

简易的Java编译器

(2010-09-28 12:52:41)
标签:

杂谈

分类: c#学习

最近学校上Java课,还是初期,用控制台运行,太麻烦了,我就用c#写了一个编译器,当然前提就是你必须安装和配置好jdk。

code:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.IO;

using System.Diagnostics;

 

namespace Java编译器

{

    public partial class Form1 Form

    {

        public Form1()

        {

            InitializeComponent();

        }

 

        private string txtFileName "";//存放文件名;

        private string txtfilename "";//存放文件路径;

 

        private void button1_Click(object sender, EventArgs e)

        {

            if (txtfilename != "")

            {

 

                richTextBox1.SaveFile(txtfilename,

 

                RichTextBoxStreamType.PlainText);

 

            }

            else

            {

                SaveFileDialog savefile new SaveFileDialog();

 

                savefile.Filter "Java文件|*.java";

 

                if (savefile.ShowDialog() == DialogResult.OK)

                {

 

                    richTextBox1.SaveFile(savefile.FileName,

 

                    RichTextBoxStreamType.PlainText);

 

                }

 

                try

                {

 

                    FileInfo fileInfo new FileInfo(savefile.FileName);

 

                    txtfilename savefile.FileName;

 

                    txtFileName fileInfo.Name;

 

 

                }

 

                catch

                {

 

                    return;

 

                }

            }

        }

 

        private void button2_Click(object sender, EventArgs e)

        {

            string str1 "cd /d txtfilename.Replace(@"\" txtFileName, "");

            string str2 "javac txtFileName;

            string str cmd(str1, str2);

            richTextBox2.Text str;

        }

 

        public string cmd(string str1, string str2)

        {

            Process new Process();

            p.StartInfo.FileName "cmd.exe";//要执行的程序名称

            p.StartInfo.UseShellExecute false;

            p.StartInfo.RedirectStandardInput true;//可能接受来自调用程序的输入信息

            p.StartInfo.RedirectStandardOutput true;//由调用程序获取输出信息

            p.StartInfo.RedirectStandardError true;

 

            p.StartInfo.CreateNoWindow true;//不显示程序窗口

            p.Start();//启动程序

            //CMD窗口发送输入信息:

            p.StandardInput.WriteLine(str1);

            p.StandardInput.WriteLine(str2);

            //p.WaitForExit();

            p.StandardInput.WriteLine("exit");

            //获取CMD窗口的输出信息:

            string stt p.StandardError.ReadToEnd();

            if (stt == "")

            {

                stt "编译成功";

            }

            //p.StandardInput.WriteLine("exit");

            // p.WaitForExit();

            p.Close();

            return stt;

        }

 

        public string cmd1(string str1, string str2)

        {

            Process new Process();

            p.StartInfo.FileName "cmd.exe";//要执行的程序名称

            p.StartInfo.UseShellExecute false;

            p.StartInfo.RedirectStandardInput true;//可能接受来自调用程序的输入信息

            p.StartInfo.RedirectStandardOutput true;//由调用程序获取输出信息

            p.StartInfo.RedirectStandardError true;

 

            p.StartInfo.CreateNoWindow true;//不显示程序窗口

            p.Start();//启动程序

            //CMD窗口发送输入信息:

            p.StandardInput.WriteLine(str1);

            p.StandardInput.WriteLine(str2);

            //p.WaitForExit();

            p.StandardInput.WriteLine("exit");

            //获取CMD窗口的输出信息:

            string stt p.StandardOutput.ReadToEnd();

            //p.StandardInput.WriteLine("exit");

            // p.WaitForExit();

            p.Close();

            return stt;

        }

 

        private void button3_Click(object sender, EventArgs e)

        {

            string str1 "cd /d txtfilename.Replace(@"\" txtFileName, "");

            string str2 "java txtFileName.Replace(".java", "");

            string str cmd1(str1, str2);

            int str.IndexOf(str2, 0);

            string s;

            if (txtfilename.Replace(@"\" txtFileName, "").Length == 2)

            {

                str.Substring(i str2.Length).Replace(txtfilename.Replace(@"\" txtFileName, "") @"\>exit", "");

            }

            else

            {

                str.Substring(i str2.Length).Replace(txtfilename.Replace(@"\" txtFileName, "") @">exit", "");

            }

            string show "--------运行结果-----------";

            richTextBox2.Text show s;

        }

 

        private void button4_Click(object sender, EventArgs e)

        {

            OpenFileDialog openfile new OpenFileDialog();

 

            openfile.Filter "Java文件|*.java";

 

            if (openfile.ShowDialog() == DialogResult.OK)

            {

 

                richTextBox1.LoadFile(openfile.FileName,

 

                RichTextBoxStreamType.PlainText);

 

            }

 

            try

            {

 

                FileInfo fileInfo new FileInfo(openfile.FileName);

 

                txtfilename openfile.FileName;

 

                txtFileName fileInfo.Name;

            }

 

            catch

            {

 

                return;

 

            }//加入trycatch为了防止未保存或未打开直接叉掉对话框所带来的错误;

 

        }

 

        private void Form1_Load(object sender, EventArgs e)

        {

            textBox1.Text "在用这个小工具之前先安装和配置好JDK,记得先保存再编译运行。        ——cjfl1234";

        }

    }

}

 

 http://s7/middle/673ccb5bx914aac91f596&690

 

http://s7/middle/673ccb5bx914aad1a8b36&690

 

http://s9/middle/673ccb5bx914aad81de48&690

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有