Jar文件独立运行时工作,但在Windows服务下无法运行

我有一个Java项目,它编译成一个可执行的jar文件v-agent-exe.jar。 这个jar是一个日志服务器,日志行被发送给它进行处理。 我可以使用这个命令来执行它:

`java -jar v-agent-exe.jar -a watch -f config.ini`. 

执行后,这个jar文件将在端口1235创build一个ServerSocket,并监听来自客户端的input数据。 收到数据后,程序将处理数据并将结果发送回客户端。 当我从CMD窗口执行jar时,处理工作正常。

现在我正在试图将Jar文件作为Windows服务(我正在使用Windows 10)进行打包。 我在Visual Studio中创build了一个“ Windows服务项目 ”,如下所示: – 调用者类有call()方法来执行jar文件的使用过程。 – AgentService是在另一个线程中执行Caller-> call()的服务。 – 程序是加载AgentService的主要条目。

Caller.cs

 public class Caller { static Process proc; public Process GetProcess(){ return proc; } public void call() { try { String dir = AppDomain.CurrentDomain.BaseDirectory; proc = new Process { StartInfo = new ProcessStartInfo { WorkingDirectory = dir, FileName = "java.exe", Arguments = @"-jar v-agent-exe.jar -a watch -f config.ini", UseShellExecute = false, RedirectStandardOutput = true, RedirectStandardError = true, RedirectStandardInput = true, CreateNoWindow = true } }; proc.Start(); while (!proc.StandardError.EndOfStream) { string line = proc.StandardError.ReadLine(); } } catch (Exception ex) { VAgentService.writeLog("Error when call process: " + ex.Message); } } } 

AgentService

 public partial class AgentService : ServiceBase { private string jarPath; private string iniPath; static Process proc; Caller caller; public AgentService() { InitializeComponent(); } protected override void OnStart(string[] args) { writeLog("On start"); try { caller = new Caller(); writeLog("Prepare to launch thread"); Thread t = new Thread(new ThreadStart(caller.call)); t.Start(); } catch (Exception ex) { EventLog.WriteEntry("Demo error: " + ex.Message); } } protected override void OnStop() { proc = caller.GetProcess(); if (proc != null && !proc.HasExited) { proc.Kill(); } else { ... } } } 

Program.cs中

 static class Program { /// <summary> /// The main entry point for the application. /// </summary> static void Main(String[] args) { ServiceBase[] ServicesToRun; ServicesToRun = new ServiceBase[] { new AgentService() }; ServiceBase.Run(ServicesToRun); } } 

build立服务项目后,我有AgentService.exe。 我将它安装到我的系统使用:

 sc create VAgentLogging binpath= %CD%\AgentService.exe depend= lmhosts start= auto 

在service.msc中启动服务之后,我可以telnet到java进程正在监听的端口“1235”(我确信只有运行在这个端口上的jar)。 根据java程序的日志,它仍然可以接收到部分数据,但似乎无法发送回客户端或其他东西,导致后续的过程无法完成。 我认为我的问题是:jar文件可以作为独立执行,但不知何故,当它包装在我的服务项目时,它很糟糕。 我还没有发布jar的代码,因为我认为这个错误与Windows服务项目有关。 如果您需要Java代码,请告诉我,我会在这里更新它。 任何帮助,将不胜感激。