如何重新启动java应用程序,记住它的命令行参数
我有一个java应用程序。它可以通过几个命令行标志启动。我想提供由用户“重新启动”应用程序的能力。
目前,我们将参数保存在控制文件中,在重新启动应用程序时读取它。重新启动应用程序的最佳方法是什么 - 如何保留命令行参数?
I have a java application. It can be started with couple of command line flags. I want to provide ability "restart" the application by user.
Currently we save the the arguments on a control file, reads it when restarting the application. What is the best way to restart the application - how can I retain the command line arguments?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
使用 RuntimeMXBean 您可以检索、类路径、Bootclasspath 等。
Using the RuntimeMXBean you could retrieve , Classpath, Bootclasspath etc.
使用调用新函数关闭
当前函数
在这里,您不会遇到保留arg
这里是从 java 应用程序调用命令的示例
invoke new using
close current one using
Here you won't face problem of retaining arg
Here is example to invoke commands from java app
无论如何,您必须保留命令行参数。如果参数集相当固定,请考虑编写一个小批处理或 shell 脚本文件,该文件除了使用这组参数调用 java 之外什么也不做。
如果您只想使用参数启动一次,然后在不带参数的情况下重新启动应用程序,希望让它使用上一次调用中的参数,请执行以下操作:
旁注:为了简单起见,我重用了
参数。为了获得更好的代码,如果需要,请将接收或存储的参数复制到另一个数据结构、另一个数组、Properties 实例......
Anyway, you will have to persist the commandline arguments. If the set of arguments is pretty fixed, consider writing a small batch or shell script file that does nothing but calling java with this set of arguments.
If you just want to start it once with arguments and then, if you restart the application without arguments, want to have it to use the arguments from the previous call, do something like that:
Sidenote: For simplicity reasons I've reused
args
. For better code, if needed, copy the received or stored parameters to another data structure, another array, a Properties instance, ...它根据用户的操作系统而有所不同,如果你真的想做操作系统跨平台兼容。然后你应该提供启动脚本:Linux 的 shell 就像 Windows 的 OS / bat 一样,这些脚本设置类路径和参数。
我不认为在应用程序中创建“重新启动”按钮是一个明智的决定,但是如果您想要“eclipse重新启动”之类的东西,您应该看看 RuntimeMXBean 它可以为您获取启动类路径。
It varies according to an OS of the user, If you really want to do it OS cross-platform compatible. Then you should supplied starting scripts : shell for linux like OS / bat for windows, these scripts set up the classpath and arguments.
I don't think that creating "restart" button in the application is a wise decision, but If you want something like "eclipse restart", you should take a look at RuntimeMXBean which can get booting classpath for you.
为什么不在重新启动时从磁盘序列化并再次创建对象?
您需要在
"CommandLineParams"
类中实现Serializable
接口才能执行此操作。我认为这是完成您想要做的事情的最结构化的方式。
Why not serialize and create the object again from disk when restarted?
You will need to implement the
Serializable
interface in a"CommandLineParams"
class to do this.I think it's the most structured way to accomplish what you are trying to do.
在此写下关闭的解决方案如何在错误后重新启动应用程序?我的观点 - 与此主题不重复,这是恢复工作应用程序的问题。
如果您可以运行 Bash 脚本而不是您的程序:
并以
nohup script.sh &
运行
请参阅 https://superuser.com/a/1223132/561025 和 https://superuser.com/a/1223132/561025
Write here solution for closed How to restart the application after an error? My opinion - no duplication with this topic, it's a problem of recover working application.
If you can ran the Bash script instead of your program:
And run as
nohup script.sh &
See https://superuser.com/a/1223132/561025 and https://superuser.com/a/1223132/561025