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

JAVA 专栏收录该内容
1 篇文章 0 订阅

在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;
            }
        }
    }	
}
  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值