仅在尚未运行的情况下启动Java程序 [英] Start Java program only if not already running

查看:94
本文介绍了仅在尚未运行的情况下启动Java程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在Java应用程序中启动1-3个具有用户定义路径的外部程序。我的要求很少:

I need to start 1-3 external programs in my Java application that have paths defined by the user. I have few requirements:


  1. 如果程序已在运行,我不希望程序执行

  1. I don't want the program to execute if it is already running

我不希望任何程序从我的Java应用程序中窃取焦点

I don't want any of the programs to steal focus from my Java application

我不知道如果他们中的任何一个无法启动或关注他们。他们只需要默默地失败。

I don't care if any of them fail to start or not. They just need to fail silently.

这是我到目前为止所提出的:

Here is what I have come up with so far:

ProcessBuilder pb = new ProcessBuilder(userDefinedPath1);
try {
    pb.start();
}
catch (Exception e) {
    // Something went wrong, just ignore
}

然后我用另外两条路径再重复3次。这开始就像我期望的那样,并且满足我的第三个要求就好了,但前两个失败了。

And then I repeat that 3 more times with the other two paths. This starts like I would expect and meets my third requirement just fine, but fails on the first two.

最好的方法是什么?

编辑:


  1. 我对这些其他应用程序没有任何控制权。他们是第三方。此外,用户可以随时手动启动或停止它们。

  1. I don't have any control of these other apps. They are third party. Also, they could have been start or stopped by the user manually at any time.

我知道可执行文件的确切名称(例如blah.exe)并且它们总是是相同的,但可执行文件的路径不一定是。

I know the exact names of the executables (e.g. "blah.exe") and they will always be the same, but the paths to the executables won't necessarily be.

批处理文件包装器在这里不可行。

Batch file wrappers are not feasible here.

其他应用程序不是 java应用程序,只是普通的旧Windows可执行文件。

The other apps are not java apps, just plain old Windows executables.


推荐答案

我猜你无法控制对方两个应用程序...如果你这样做,这不会太糟糕 - 你可以让他们听一个套接字,看看当你出现时套接字是否可用。

I'm guessing you don't have control over the other two apps... If you did, this wouldn't be too bad--you could just have them listen to a socket and see if the socket is available when you come up.

下一个解决方案实际上可能与语言无关。您可以通过批处理文件包装器管理整个系统。编写一个批处理文件,在启动时创建一个文件,并在文件停止时删除它。 Unix系统经常使用这种技术 - 他们经常将文件称为锁定文件。

The next solution may actually be language independent. You could manage the whole system through batch file wrappers. Write a batch file that creates a file when it starts up and deletes it when it stops. Unix systems use this technique a lot--they call the file a lock file more often than not.

如果只有你的应用程序会启动这些其他应用程序,那么你可以只是跟踪你是否已经启动它,所以我猜这是不可能的,或者你不会问,所以我假设用户可能通过其他一些机制启动了这些程序。

If only your app will ever start these other apps, then you could simply track if you've started it or not, so I'm guessing this isn't possible or you wouldn't be asking, so I'm assuming that the user may have launched these programs through some other mechanism.

如果您无法控制其他应用程序的启动,甚至无法编写批处理文件来启动它们,那么您就无法做您想做的事情(请注意,应用程序必须始终使用批处理文件,即使用户手动启动它们。)

If you have NO control over the launching of the other apps and can't even write a batch file to launch them, then you just can't do what you want to do (Note, the apps would have to always use the batch file, even if the user started them by hand).

我只是最后的努力可能是获得一个进程状态并解析它,但你必须确切地知道在PS中调用其他应用程序的内容,这实际上并不是微不足道的。此外,所有Java应用程序在大多数进程状态打印输出中往往具有相同的确切签名,这可能使这无用。

I just a very-last ditch effort might be to get a process status and parse it, but you'd have to know exactly what the other apps were called in the PS, this isn't really trivial. Also, all java apps tend to have the same exact signature in most process status printouts which could make this useless.

问题是,如果其中一个程序是在外面启动的你的应用程序,你几乎没有办法确定这个事实,除非你碰巧知道它的确切过程状态签名,即便如此它也是不稳定的。

The problem is that if one of these programs were started outside your app, you have virtually NO WAY to identify that fact unless you happen to know it's exact process status signature, and even then it's flaky.

这篇关于仅在尚未运行的情况下启动Java程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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