如何重新启动java应用程序,记住它的命令行参数

发布于 2024-09-25 16:10:20 字数 125 浏览 8 评论 0原文

我有一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

双马尾 2024-10-02 16:10:20

使用 RuntimeMXBean 您可以检索、类路径、Bootclasspath 等。

package com;

import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;

class JMXTest {
    public static void main(String args[]) {
        try {
            for ( int i = 0 ; i < args.length ; i++ ) 
                 System.out.println( "args   :" + args[i] );

            RuntimeMXBean mx = ManagementFactory.getRuntimeMXBean();
            System.out.println( "boot  CP:" + mx.getBootClassPath() );
            System.out.println( "      CP:" + mx.getClassPath() );
            System.out.println( "cmd args:" + mx.getInputArguments() );
        }
        catch( Exception e ) {
            e.printStackTrace();
        }
    }
}

Using the RuntimeMXBean you could retrieve , Classpath, Bootclasspath etc.

package com;

import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;

class JMXTest {
    public static void main(String args[]) {
        try {
            for ( int i = 0 ; i < args.length ; i++ ) 
                 System.out.println( "args   :" + args[i] );

            RuntimeMXBean mx = ManagementFactory.getRuntimeMXBean();
            System.out.println( "boot  CP:" + mx.getBootClassPath() );
            System.out.println( "      CP:" + mx.getClassPath() );
            System.out.println( "cmd args:" + mx.getInputArguments() );
        }
        catch( Exception e ) {
            e.printStackTrace();
        }
    }
}
北方的韩爷 2024-10-02 16:10:20

使用调用新函数关闭

java -jar appname.jar arg1 arg2  

当前函数

System.exit(0);   

在这里,您不会遇到保留arg

这里是从 java 应用程序调用命令的示例

invoke new using

java -jar appname.jar arg1 arg2  

close current one using

System.exit(0);   

Here you won't face problem of retaining arg

Here is example to invoke commands from java app

要走就滚别墨迹 2024-10-02 16:10:20

无论如何,您必须保留命令行参数。如果参数集相当固定,请考虑编写一个小批处理或 shell 脚本文件,该文件除了使用这组参数调用 java 之外什么也不做。

如果您只想使用参数启动一次,然后在不带参数的情况下重新启动应用程序,希望让它使用上一次调用中的参数,请执行以下操作:

public static void main(String[] args) {

   if (args.length == 0)
     args = readArgsFromFile();
   else
     writeArgsToFile();

   // ...

}

旁注:为了简单起见,我重用了 参数。为了获得更好的代码,如果需要,请将接收或存储的参数复制到另一个数据结构、另一个数组、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:

public static void main(String[] args) {

   if (args.length == 0)
     args = readArgsFromFile();
   else
     writeArgsToFile();

   // ...

}

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, ...

久光 2024-10-02 16:10:20

它根据用户的操作系统而有所不同,如果你真的想做操作系统跨平台兼容。然后你应该提供启动脚本: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.

知你几分 2024-10-02 16:10:20

为什么不在重新启动时从磁盘序列化并再次创建对象?

您需要在 "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.

撧情箌佬 2024-10-02 16:10:20

在此写下关闭的解决方案如何在错误后重新启动应用程序?我的观点 - 与此主题不重复,这是恢复工作应用程序的问题。

如果您可以运行 Bash 脚本而不是您的程序:

#!/bin/bash
while : ; do
    myprogram
    [[ $? -ne 0 ]] || break
done

并以 nohup script.sh &
运行
请参阅 https://superuser.com/a/1223132/561025https://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:

#!/bin/bash
while : ; do
    myprogram
    [[ $? -ne 0 ]] || break
done

And run as nohup script.sh &
See https://superuser.com/a/1223132/561025 and https://superuser.com/a/1223132/561025

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文