简体   繁体   English

在C#dot net应用程序中执行jar文件

[英]executing jar file within a C# dot net application

  1. I want to execute a jar file from within the C#.net application.The jar file is a console application. 我想从C#.net应用程序中执行一个jar文件.jar文件是一个控制台应用程序。 The jar file is present in the same directory as the C# application. jar文件与C#应用程序位于同一目录中。 2. I want to check if jdk is installed frm within C#.net applcation How can i do that? 2.我想检查是否在C#中安装了jdk .net applcation我该怎么做?

The c# method c#方法

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(). 可用于启动任意应用程序,因此如果您知道从命令行启动Java应用程序的命令,则可以推断出Process:Start()的参数。

The Java command will be Java命令将是

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

So the issues here are 所以这里的问题是

  1. is java available, is it on your path? 是java可用的,它在你的路径上?
  2. what is the path of the jarfile jar文件的路径是什么
  3. what is the name of the class in the jar file implementing main? 实现main的jar文件中类的名称是什么? 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. 如果传递jar文件的人友好,那么JAR的manifest指定主类的名称,因此您不需要该参数。 Otherwise, we'd hope it was documented somewhere. 否则,我们希望它被记录在某个地方。

If Java has been installed nicely then it should already be on your PATH. 如果已经很好地安装了Java,那么它应该已经在你的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: 我认为预先安装java是合理的,所以在你的c#app我只是假设Java可用并尝试启动它,然后用以下代码捕获任何失败:

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

etc. 等等

Have batch file which contains whatever you need to start your java app. 有批处理文件,其中包含启动Java应用程序所需的任何内容。

Eg "javaw -classpath path-of-jar" 例如“javaw -classpath path-of-jar”

System.Diagnostics.Process.Start("batchfile");

More here 更多这里

For question 2, elder_george's suggestion should work. 对于问题2,elder_george的建议应该有效。

If the java/bin folder is in the environment path, you might try: 如果java / bin文件夹位于环境路径中,您可以尝试:

System.Diagnostics.Process.Start("java.exe", "-version");

If no exception, you probably have a valid java.exe file. 如果没有异常,您可能有一个有效的java.exe文件。

  • Use Process to start java process passing arguments -jar yourjar.jar . 使用Process启动java进程传递参数-jar yourjar.jar

  • JDKs is usually installed in c:\\Program Files\\Java\\jdk... . JDK通常安装在c:\\Program Files\\Java\\jdk... check if this folder exists, it should fit most cases. 检查此文件夹是否存在,它应该适合大多数情况。 Or check HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Development Kit in the registry. 或者在注册表中检查HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Development Kit

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM