在C#点网络应用程序中执行jar文件 [英] executing jar file within a C# dot net application

查看:131
本文介绍了在C#点网络应用程序中执行jar文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  1. 我想从C#.net应用程序中执行jar文件。jar文件是一个控制台应用程序。 jar文件存在于与C#应用程序相同的目录中。


我想要检查jdk是否安装在C#.net应用程序中。解决方案

C#方法

  Process :: Start(program,argumentString)

可用于启动任意应用程序,因此如果您知道从命令行启动Java应用程序的命令,



Java命令将是

  java -jar< jarfile> {and maybe< mainClass>} 

所以这里的问题是





  • 在jar文件中实现主类的名称是什么?如果交付jarfile的人是友好的,那么JAR的manifest指定主类的名称,所以你不需要这个参数。

  • 如果Java已经安装得很好,那么它应该已经在你的PATH上了。从命令行尝试。我认为预先安装java是合理的,所以在你的c#应用程序中,我只是假设Java是可用的,并尝试启动它,然后捕获任何失败与代码如:

      catch(Win32Exception e)
    {
    if(e.NativeErrorCode == ERROR_FILE_NOT_FOUND)
    {
    Console.WriteLine (e.Message +。检查路径。
    }


    1. I want to execute a jar file from within the C#.net application.The jar file is a console application. The jar file is present in the same directory as the C# application. 2. I want to check if jdk is installed frm within C#.net applcation How can i do that?

    解决方案

    The c# method

    Process::Start(program, argumentString)
    

    Can be used to start arbitrary applications, so if you know the command to start Java app from the command line then you can deduce the arguments to Process:Start().

    The Java command will be

    java -jar <jarfile> {and maybe <mainClass>}
    

    So the issues here are

    1. is java available, is it on your path?
    2. what is the path of the jarfile
    3. what is the name of the class in the jar file implementing main? If the folks delivering the jarfile have been friendly then the JAR's manifest specifies the name of the main class so you won't need that argument. Otherwise, we'd hope it was documented somewhere.

    If Java has been installed nicely then it should already be on your PATH. Try it from the command line. I think it's reasonable to pre-req that java be installed, so in your c# app I would just assume that Java is available and attempt to start it, then catch any failure with code such as:

      catch (Win32Exception e)
                {
                    if(e.NativeErrorCode == ERROR_FILE_NOT_FOUND)
                    {
                        Console.WriteLine(e.Message + ". Check the path.");
                    } 
    

    etc.

    这篇关于在C#点网络应用程序中执行jar文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆