Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从Windows服务运行jar文件_C#_Java_Windows_Jar_Windows Services - Fatal编程技术网

C# 如何从Windows服务运行jar文件

C# 如何从Windows服务运行jar文件,c#,java,windows,jar,windows-services,C#,Java,Windows,Jar,Windows Services,我已经编写了一个WPF应用程序,在其中我可以输入我的dos命令,并使用C#获得一个带有响应的消息框,它工作得非常好。现在我想编写一个服务,它可以每隔一段时间执行命令“java-jarmyfile.jar”。jar文件正在访问weblogic队列并返回由下面的函数读取的结果。问题是,尽管下面的功能已在WFP中成功运行,但该服务仍无法运行 如何使此功能在Windows服务中工作? /// <summary> /// Executes a shell command syn

我已经编写了一个WPF应用程序,在其中我可以输入我的dos命令,并使用C#获得一个带有响应的消息框,它工作得非常好。现在我想编写一个服务,它可以每隔一段时间执行命令“java-jarmyfile.jar”。jar文件正在访问weblogic队列并返回由下面的函数读取的结果。问题是,尽管下面的功能已在WFP中成功运行,但该服务仍无法运行

如何使此功能在Windows服务中工作?

    /// <summary>
    /// Executes a shell command synchronously.
    /// </summary>
    /// <param name="command">string command</param>
    /// <returns>string, as output of the command.</returns>
    public void ExecuteCommandSync(object command)
    {
     try
     {
         // create the ProcessStartInfo using "cmd" as the program to be run,
         // and "/c " as the parameters.
         // Incidentally, /c tells cmd that we want it to execute the command that follows,
         // and then exit.
    System.Diagnostics.ProcessStartInfo procStartInfo =
        new System.Diagnostics.ProcessStartInfo("cmd", "/c " + command);

    // The following commands are needed to redirect the standard output.
    // This means that it will be redirected to the Process.StandardOutput StreamReader.
    procStartInfo.RedirectStandardOutput = true;
    procStartInfo.UseShellExecute = false;
    // Do not create the black window.
    procStartInfo.CreateNoWindow = true;
    // Now we create a process, assign its ProcessStartInfo and start it
    System.Diagnostics.Process proc = new System.Diagnostics.Process();
    proc.StartInfo = procStartInfo;
    proc.Start();
    // Get the output into a string
    string result = proc.StandardOutput.ReadToEnd();
    // Display the command output.
    Console.WriteLine(result);
      }
      catch (Exception objException)
      {
      // Log the exception
      }
}
//
///同步执行shell命令。
/// 
///字符串命令
///字符串,作为命令的输出。
public void ExecuteCommandSync(对象命令)
{
尝试
{
//使用“cmd”作为要运行的程序创建ProcessStartInfo,
//和“/c”作为参数。
//顺便说一下,/c告诉cmd我们希望它执行下面的命令,
//然后退出。
System.Diagnostics.ProcessStartInfo procStartInfo=
新系统.Diagnostics.ProcessStartInfo(“cmd”、“/c”+命令);
//重定向标准输出需要以下命令。
//这意味着它将被重定向到Process.StandardOutput StreamReader。
procStartInfo.RedirectStandardOutput=true;
procStartInfo.UseShellExecute=false;
//不要创建黑色窗口。
procStartInfo.CreateNoWindow=true;
//现在我们创建一个进程,分配它的ProcessStartInfo并启动它
System.Diagnostics.Process proc=新的System.Diagnostics.Process();
proc.StartInfo=procStartInfo;
proc.Start();
//将输出转换为字符串
字符串结果=proc.StandardOutput.ReadToEnd();
//显示命令输出。
控制台写入线(结果);
}
捕获(异常对象异常)
{
//记录异常
}
}

获取的代码您尝试运行的完整命令行是什么?您是在安装Java的帐户中运行服务,还是在LocalSystem帐户(默认)中运行服务?由于您从未将Java安装为LocalSystem,因此它可能无法在那里正常工作。@CoreTech我在同一个帐户中运行。以下命令您忘记包含该命令?@CoreTech该命令是
java-jarc:/myApp.jar
。我为之前不完整的评论道歉,我想我之前编辑过。从WPF函数调用时,它运行良好。但是,我发现,如果调用函数OnStart和OnStop,则命令正在执行。但是没有在计时器中被调用。我正在使用System.Timer.Timer。