C#编写可执行程序(exe)启动Java应用程序

在windows平台上,使用C#语言编写编写可执行程序exe,来启动Java应用程序
原文:http://www.what21.com/sys/view/csharp-net_summary_1456895891476.html

功能说明: 使用C#实现bat脚本一样的功能,即使用C#编写的exe,启动Java应用程序。 Bat地址: /system/os/windows/windows-basis/javaapp_start.html 配置文件:

-TITLE="启动Java程序"
-JAVA_HOME="D:\Apps\Java\jdk1.6.0_26"
-JAVA_OPTS="-Xmx1024M -Xms512M -XX:MaxPermSize=256m"
-JAVA_CMD="java"
-JAVA_JAR="startup.jar"
-JAVA_MAIN="cn.hopegroup.startup.Startup"
-LAUNCH_CLASS="cn.hopegroup.startup.StartupTest.main"
-LAUNCH_ARGS="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"

环境变量设置类:

	using System;
	using System.Collections.Generic;
	using System.IO;
	using System.Text;
 
	namespace LaunchApp
	{
    class JavaEnv
    {
 
        public static string TITLE;
        public static string JAVA_HOME;
        public static string JAVA_OPTS;
        public static string JAVA_CMD;
        public static string JAVA_JAR;
        public static string JAVA_MAIN;
        public static string LAUNCH_CLASS;
        public static string LAUNCH_ARGS;
        public static string CLASSPATH;
 
        /**
         * 初始化
         */
        public static void ini(string launch)
        {
            string strLine;
            try
            {
                FileStream lauhchFile = new FileStream(launch, FileMode.Open);
                StreamReader sr = new StreamReader(lauhchFile);
                strLine = sr.ReadLine();
                while (strLine != null)
                {
                    strLine = strLine.Trim();
                    if (strLine.StartsWith("-TITLE="))
                    {
                        strLine = strLine.Replace("-TITLE=", "");
                        strLine = strLine.Replace("\"", "");
                        TITLE = strLine;
                    }
                    else if (strLine.StartsWith("-JAVA_HOME="))
                    {
                        strLine = strLine.Replace("-JAVA_HOME=", "");
                        strLine = strLine.Replace("\"", "");
                        JAVA_HOME = strLine;
                    }
                    else if (strLine.StartsWith("-JAVA_OPTS="))
                    {
                        strLine = strLine.Replace("-JAVA_OPTS=", "");
                        strLine = strLine.Replace("\"", "");
                        JAVA_OPTS = strLine;
                    }
                    else if (strLine.StartsWith("-JAVA_CMD="))
                    {
                        strLine = strLine.Replace("-JAVA_CMD=", "");
                        strLine = strLine.Replace("\"", "");
                        JAVA_CMD = strLine;
                    }
                    else if (strLine.StartsWith("-JAVA_JAR="))
                    {
                        strLine = strLine.Replace("-JAVA_JAR=", "");
                        strLine = strLine.Replace("\"", "");
                        JAVA_JAR = strLine;
                    }
                    else if (strLine.StartsWith("-JAVA_MAIN="))
                    {
                        strLine = strLine.Replace("-JAVA_MAIN=", "");
                        strLine = strLine.Replace("\"", "");
                        JAVA_MAIN = strLine;
                    }
                    else if (strLine.StartsWith("-LAUNCH_CLASS="))
                    {
                        strLine = strLine.Replace("-LAUNCH_CLASS=", "");
                        strLine = strLine.Replace("\"", "");
                        LAUNCH_CLASS = strLine;
                    }
                    else if (strLine.StartsWith("-LAUNCH_ARGS="))
                    {
                        strLine = strLine.Replace("-LAUNCH_ARGS=", "");
                        strLine = strLine.Replace("\"", "");
                        LAUNCH_ARGS = strLine;
                    }
                    strLine = sr.ReadLine();
                }
                sr.Close();
            }
            catch (IOException e)
            {
                Console.WriteLine("ini config error");
                Console.WriteLine(e.ToString());
                Console.ReadLine();
                return;
            }
            // 如果不存在JAVA_HOME,读取环境变量
            if (JAVA_HOME == null || "".Equals(JAVA_HOME))
            {
                JAVA_HOME = Environment.GetEnvironmentVariable("JAVA_HOME");
            }
            // 设置classpath
            string classpath = ".;" + JAVA_HOME + "\\lib\\dt.jar;" + JAVA_HOME + "\\lib\\tools.jar";
            Environment.SetEnvironmentVariable("CLASSPATH", classpath);
            CLASSPATH = classpath;
        }
          
    }
}

启动Java类:

	using System;
	using System.Collections.Generic;
	 
	using System.Runtime.InteropServices;
	using System.Text;
	 
	namespace LaunchApp
	{
	    class LauchMain
	    {
	        
	 
        static void Main(string[] args)
        {
            try
            {
                // 初始化环境变量
                JavaEnv.ini("launch.ini");
                //
                //Console.WriteLine(JavaEnv.TITLE);
                //Console.WriteLine(JavaEnv.JAVA_HOME);
                //Console.WriteLine(JavaEnv.JAVA_OPTS);
                //Console.WriteLine(JavaEnv.JAVA_CMD);
                //Console.WriteLine(JavaEnv.JAVA_JAR);
                //Console.WriteLine(JavaEnv.JAVA_MAIN);
                //Console.WriteLine(JavaEnv.LAUNCH_CLASS);
                //Console.WriteLine(JavaEnv.LAUNCH_ARGS);
 
                //需要启动的程序名
                string fileName = JavaEnv.JAVA_HOME + "\\bin\\" + JavaEnv.JAVA_CMD;
                //Console.WriteLine(fileName);
                //启动参数
                StringBuilder argStr = new StringBuilder();
                argStr.Append(JavaEnv.JAVA_OPTS);
                argStr.Append(" -classpath  \"");
                argStr.Append(JavaEnv.CLASSPATH);
                argStr.Append("\";");
                argStr.Append(JavaEnv.JAVA_JAR);
                argStr.Append(" ");
                argStr.Append(JavaEnv.JAVA_MAIN);
                argStr.Append(" ");
                argStr.Append(JavaEnv.LAUNCH_CLASS);
                argStr.Append(" \"");
                argStr.Append(JavaEnv.LAUNCH_ARGS);
                argStr.Append("\"");
                //Console.WriteLine(argStr.ToString());
 
                // 调用
                System.Diagnostics.Process process = new System.Diagnostics.Process();
                process.StartInfo.FileName = fileName;
                process.StartInfo.Arguments = argStr.ToString();
                //启动   
                process.Start();
                //判断是否运行结束    
                //if (process.HasExited)
                //{
                //    process.Kill();
                //}
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                Console.ReadLine();
                return;
            }
        }
    }	
}

你可能感兴趣的:(JAVA,JAVA实用)