programing

Java 애플리케이션을 다시 시작하려면 어떻게해야합니까?

nasanasas 2020. 9. 5. 10:01
반응형

Java 애플리케이션을 다시 시작하려면 어떻게해야합니까?


Java AWT 애플리케이션을 어떻게 다시 시작할 수 있습니까? 이벤트 핸들러를 첨부 한 버튼이 있습니다. 응용 프로그램을 다시 시작하려면 어떤 코드를 사용해야합니까?

Application.Restart()C # 애플리케이션에서하는 것과 똑같은 일을하고 싶습니다 .


물론 Java 애플리케이션을 다시 시작할 수 있습니다.

다음 방법은 Java 애플리케이션을 다시 시작하는 방법을 보여줍니다.

public void restartApplication()
{
  final String javaBin = System.getProperty("java.home") + File.separator + "bin" + File.separator + "java";
  final File currentJar = new File(MyClassInTheJar.class.getProtectionDomain().getCodeSource().getLocation().toURI());

  /* is it a jar file? */
  if(!currentJar.getName().endsWith(".jar"))
    return;

  /* Build command: java -jar application.jar */
  final ArrayList<String> command = new ArrayList<String>();
  command.add(javaBin);
  command.add("-jar");
  command.add(currentJar.getPath());

  final ProcessBuilder builder = new ProcessBuilder(command);
  builder.start();
  System.exit(0);
}

기본적으로 다음을 수행합니다.

  1. Java 실행 파일을 찾습니다 (여기서 Java 바이너리를 사용했지만 요구 사항에 따라 다름).
  2. 응용 프로그램을 찾습니다 (제 경우에는 항아리, MyClassInTheJar클래스를 사용 하여 항아리 위치 자체를 찾습니다)
  3. jar를 다시 시작하는 명령을 빌드합니다 (이 경우 Java 바이너리 사용).
  4. 실행 해! (따라서 현재 응용 프로그램을 종료하고 다시 시작)

import java.io.File;
import java.io.IOException;
import java.lang.management.ManagementFactory;

public class Main {
    public static void main(String[] args) throws IOException, InterruptedException {
        StringBuilder cmd = new StringBuilder();
        cmd.append(System.getProperty("java.home") + File.separator + "bin" + File.separator + "java ");
        for (String jvmArg : ManagementFactory.getRuntimeMXBean().getInputArguments()) {
            cmd.append(jvmArg + " ");
        }
        cmd.append("-cp ").append(ManagementFactory.getRuntimeMXBean().getClassPath()).append(" ");
        cmd.append(Main.class.getName()).append(" ");
        for (String arg : args) {
            cmd.append(arg).append(" ");
        }
        Runtime.getRuntime().exec(cmd.toString());
        System.exit(0);
    }
}

불가능하다고 말하는 모든 사람들에게 바칩니다.

이 프로그램은 원래 명령 줄을 재구성하는 데 사용할 수있는 모든 정보를 수집합니다. 그런 다음 시작하고 동일한 명령이므로 응용 프로그램이 두 번째로 시작됩니다. 그런 다음 원래 프로그램을 종료하면 자식 프로그램이 계속 실행되고 (리눅스에서도) 동일한 작업을 수행합니다.

경고 : 이것을 실행하면 포크 폭탄 과 유사한 새로운 프로세스 생성이 끝나지 않는다는 점에 유의하십시오 .


기본적으로 할 수 없습니다. 적어도 신뢰할 수있는 방식은 아닙니다.

Java 프로그램을 다시 시작하려면 JVM을 다시 시작해야합니다. JVM을 다시 시작하려면 다음을 수행해야합니다.

  1. java사용 된 실행기를 찾습니다 . 시도해 볼 수 System.getProperty("java.home")있지만 이것이 실제로 응용 프로그램을 시작하는 데 사용 된 실행기를 가리킬 것이라는 보장은 없습니다. (반환 된 값 이 애플리케이션을 시작하는 데 사용 된 JRE를 가리 키지 않거나에서 재정의되었을 수 있습니다 -Djava.home.)

  2. 당신은 아마도 설정 등 (원래 메모리 명예 할 것입니다 -Xmx, -Xms당신이 사용하는 설정이 먼저 JVM을 시작하는 알아낼 필요가 ...) 그래서를. 사용해 볼 수는 ManagementFactory.getRuntimeMXBean().getInputArguments()있지만 이것이 사용 된 설정을 반영한다는 보장은 없습니다. 이것은 해당 방법 의 문서 에도 나와 있습니다 .

    일반적으로 'java'명령에 대한 모든 명령 행 옵션이 JVM (Java Virtual Machine)에 전달되는 것은 아닙니다. 따라서 반환 된 입력 인수에 모든 명령 줄 옵션이 포함되지 않을 수 있습니다.

  3. 프로그램 Standard.in이 원래의 stdin 에서 입력을 읽으면 다시 시작할 때 손실됩니다.

  4. 이러한 트릭과 해킹의 대부분은 SecurityManager.


반면에 : 그럴 필요는 없습니다.

모든 것을 정리하기 쉽도록 응용 프로그램을 디자인하고 그 후에 "main"클래스의 새 인스턴스를 만드는 것이 좋습니다.

많은 애플리케이션은 메인 메소드에서 인스턴스를 생성하는 것 외에는 아무것도하지 않도록 설계되었습니다.

public class MainClass {
    ...
    public static void main(String[] args) {
        new MainClass().launch();
    }
    ...
}

이 패턴을 사용하면 다음과 같은 작업을 쉽게 수행 할 수 있습니다.

public class MainClass {
    ...
    public static void main(String[] args) {
        boolean restart;
        do {
            restart = new MainClass().launch();
        } while (restart);
    }
    ...
}

및 수 있도록 launch()응용 프로그램 방식으로 종료 된 경우에만 다시 시작해야 함을하는 경우에 true를 돌려줍니다.


엄밀히 말하면 Java 프로그램은 자체적으로 다시 시작할 수 없습니다. 그렇게하려면 실행중인 JVM을 종료 한 다음 다시 시작해야하지만 JVM이 더 이상 실행 (종료)되지 않으면 조치를 취할 수 없습니다.

AWT 구성 요소를 다시로드, 압축 및 시작하기 위해 사용자 정의 클래스 로더로 몇 가지 트릭을 수행 할 수 있지만 GUI 이벤트 루프와 관련하여 많은 골칫거리가 될 수 있습니다.

애플리케이션이 시작되는 방법에 따라 JVM이 특정 코드와 함께 종료되는 동안 계속되는 do / while 루프가 포함 된 래퍼 스크립트에서 JVM을 시작할 수 있습니다. 그런 다음 AWT 앱은를 호출해야합니다 System.exit(RESTART_CODE). 예를 들어 스크립팅 의사 코드에서 :

DO
  # Launch the awt program
  EXIT_CODE = # Get the exit code of the last process
WHILE (EXIT_CODE == RESTART_CODE)

AWT 앱은 다시 시작할 필요가없는 "정상"종료시 RESTART_CODE 이외의 다른 항목으로 JVM을 종료해야합니다.


Eclipse는 일반적으로 플러그인이 설치된 후 다시 시작됩니다. Windows 용 래퍼 eclipse.exe (런처 앱)를 사용하여이를 수행합니다. 이 애플리케이션은 핵심 Eclipse 러너 jar를 실행하고 Eclipse Java 애플리케이션이 재실행 코드로 종료되면 eclipse.exe가 워크 벤치를 다시 시작합니다. 다시 시작하기 위해 유사한 원시 코드, 쉘 스크립트 또는 다른 Java 코드 래퍼를 빌드 할 수 있습니다.


실제로 앱을 다시 시작해야하는 경우 별도의 앱을 작성하여 시작할 수 있습니다.

이 페이지에서는 다양한 시나리오에 대한 다양한 예를 제공합니다.

http://www.rgagnon.com/javadetails/java-0014.html


윈도우

public void restartApp(){

    // This launches a new instance of application dirctly, 
    // remember to add some sleep to the start of the cmd file to make sure current instance is
    // completely terminated, otherwise 2 instances of the application can overlap causing strange
    // things:)

    new ProcessBuilder("cmd","/c start /min c:/path/to/script/that/launches/my/application.cmd ^& exit").start();
    System.exit(0);
}

/ min 최소화 된 창에서 스크립트 시작

^ & 종료 후 cmd 창 닫기

샘플 cmd 스크립트는

@echo off
rem add some sleep (e.g. 10 seconds) to allow the preceding application instance to release any open resources (like ports) and exit gracefully, otherwise the new instance could fail to start
sleep 10   
set path=C:\someFolder\application_lib\libs;%path%
java -jar application.jar

수면 10 10 초 동안 수면


이 질문은 오래되고 답변되었지만 일부 솔루션에서 문제를 발견하고 제안을 믹스에 추가하기로 결정했습니다.

일부 솔루션의 문제점은 단일 명령 문자열을 빌드한다는 것입니다. 이로 인해 일부 매개 변수에 공백, 특히 java.home이 포함 된 경우 문제가 발생합니다 .

예를 들어, 창문에서는

final String javaBin = System.getProperty("java.home") + File.separator + "bin" + File.separator + "java";

다음과 같은 것을 반환 할 수 있습니다.C:\Program Files\Java\jre7\bin\java

이 문자열은 따옴표로 묶거나 공백으로 인해 이스케이프해야합니다 Program Files. 큰 문제는 아니지만 특히 크로스 플랫폼 애플리케이션에서 다소 성 가시고 오류가 발생하기 쉽습니다.

따라서 내 솔루션은 명령 을 명령 배열 로 빌드 합니다.

public static void restart(String[] args) {

        ArrayList<String> commands = new ArrayList<String>(4 + jvmArgs.size() + args.length);
        List<String> jvmArgs = ManagementFactory.getRuntimeMXBean().getInputArguments();

        // Java
        commands.add(System.getProperty("java.home") + File.separator + "bin" + File.separator + "java");

        // Jvm arguments
        for (String jvmArg : jvmArgs) {
            commands.add(jvmArg);
        }

        // Classpath
        commands.add("-cp");
        commands.add(ManagementFactory.getRuntimeMXBean().getClassPath());

        // Class to be executed
        commands.add(BGAgent.class.getName());

        // Command line arguments
        for (String arg : args) {
            commands.add(arg);
        }

        File workingDir = null; // Null working dir means that the child uses the same working directory

        String[] env = null; // Null env means that the child uses the same environment

        String[] commandArray = new String[commands.size()];
        commandArray = commands.toArray(commandArray);

        try {
            Runtime.getRuntime().exec(commandArray, env, workingDir);
            System.exit(0);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

이 질문을 접했을 때 직접 주제를 조사하고있었습니다.

답변이 이미 받아 들여 졌다는 사실과 상관없이, 저는 여전히 완전성을위한 대안적인 접근 방식을 제공하고 싶습니다. 특히 Apache Ant는 매우 유연한 솔루션이었습니다.

기본적으로 모든 것은 Java 코드 ( 여기 참조)에서 호출 된 단일 Java 실행 태스크 ( 여기여기 참조)가있는 Ant 스크립트 파일로 요약됩니다 . 메소드 launch 일 수있는 이 Java 코드는 다시 시작해야하는 애플리케이션의 일부일 수 있습니다. 애플리케이션에는 Apache Ant 라이브러리 (jar)에 대한 종속성이 있어야합니다.

애플리케이션을 다시 시작해야 할 때마다 메서드 launch를 호출 하고 VM을 종료 해야합니다 . Ant Java 태스크에는 옵션 forkspawn 이 true로 설정되어 있어야합니다.

다음은 Ant 스크립트의 예입니다.

<project name="applaucher" default="launch" basedir=".">
<target name="launch">
    <java classname="package.MasinClass" fork="true" spawn="true">
        <jvmarg value="-splash:splash.jpg"/>
        <jvmarg value="-D other VM params"/>
        <classpath>
            <pathelement location="lib-1.jar" />
            ...
            <pathelement location="lib-n.jar" />
        </classpath>
    </java>
</target>
</project>

시작 메서드 의 코드는 다음과 같습니다.

public final void launch(final String antScriptFile) {
 /* configure Ant and execute the task */
   final File buildFile = new File(antScriptFile);
   final Project p = new Project();
   p.setUserProperty("ant.file", buildFile.getAbsolutePath());

   final DefaultLogger consoleLogger = new DefaultLogger();
   consoleLogger.setErrorPrintStream(System.err);
   consoleLogger.setOutputPrintStream(System.out);
   consoleLogger.setMessageOutputLevel(Project.MSG_INFO);
   p.addBuildListener(consoleLogger);

   try {
       p.fireBuildStarted();
       p.init();
       final ProjectHelper helper = ProjectHelper.getProjectHelper();
       p.addReference("ant.projectHelper", helper);
       helper.parse(p, buildFile);
       p.executeTarget(p.getDefaultTarget());
       p.fireBuildFinished(null);
   } catch (final BuildException e) {
       p.fireBuildFinished(e);
   }

   /* exit the current VM */
   System.exit(0);

}

여기서 매우 편리한 점은 초기 응용 프로그램 시작 및 다시 시작에 동일한 스크립트가 사용된다는 것입니다.


다른 답변에없는 정보를 추가하는 것뿐입니다.

procfs /proc/self/cmdline 를 사용할 수있는 경우

If you are running in an environment which provides procfs and therefore has the /proc file system available (which means this is not a portable solution), you can have Java read /proc/self/cmdline in order to restart itself, like this:

public static void restart() throws IOException {
    new ProcessBuilder(getMyOwnCmdLine()).inheritIO().start();
}
public static String[] getMyOwnCmdLine() throws IOException {
    return readFirstLine("/proc/self/cmdline").split("\u0000");
}
public static String readFirstLine(final String filename) throws IOException {
    try (final BufferedReader in = new BufferedReader(new FileReader(filename))) {
        return in.readLine();
    }
}

On systems with /proc/self/cmdline available, this probably is the most elegant way of how to "restart" the current Java process from Java. No JNI involved, and no guessing of paths and stuff required. This will also take care of all JVM options passed to the java binary. The command line will be exactly identical to the one of the current JVM process.

Many UNIX systems including GNU/Linux (including Android) nowadays have procfs However on some like FreeBSD, it is deprecated and being phased out. Mac OS X is an exception in the sense that it does not have procfs. Windows also does not have procfs. Cygwin has procfs but it's invisible to Java because it's only visible to applications using the Cygwin DLLs instead of Windows system calls, and Java is unaware of Cygwin.

Don't forget to use ProcessBuilder.inheritIO()

The default is that stdin / stdout / stderr (in Java called System.in / System.out / System.err) of the started Process are set to pipes which allow the currently running process to communicate with the newly started process. If you want to restart the current process, this is most likely not what you want. Instead you would want that stdin / stdout / stderr are the same as those of the current VM. This is called inherited. You can do so by calling inheritIO() of your ProcessBuilder instance.

Pitfall on Windows

A frequent use case of a restart() function is to restart the application after an update. The last time I tried this on Windows this was problematic. When overwrote the application's .jar file with the new version, the application started to misbehave and giving exceptions about the .jar file. I'm just telling, in case this is your use case. Back then I solved the issue by wrapping the application in a batch file and using a magic return value from System.exit() that I queried in the batch file and had the batch file restart the application instead.


Old question and all of that. But this is yet another way that offers some advantages.

On Windows, you could ask the task scheduler to start your app again for you. This has the advantage of waiting a specific amount of time before the app is restarted. You can go to task manager and delete the task and it stops repeating.

SimpleDateFormat hhmm = new SimpleDateFormat("kk:mm");    
Calendar aCal = Calendar.getInstance(); 
aCal.add(Calendar.SECOND, 65);
String nextMinute = hhmm.format(aCal.getTime()); //Task Scheduler Doesn't accept seconds and won't do current minute.
String[] create = {"c:\\windows\\system32\\schtasks.exe", "/CREATE", "/F", "/TN", "RestartMyProg", "/SC", "ONCE", "/ST", nextMinute, "/TR", "java -jar c:\\my\\dev\\RestartTest.jar"};  
Process proc = Runtime.getRuntime().exec(create, null, null);
System.out.println("Exit Now");
try {Thread.sleep(1000);} catch (Exception e){} // just so you can see it better
System.exit(0);

Similar to Yoda's 'improved' answer, but with further improvements (both functional, readability, and testability). It's now safe to run, and restarts for as as many times as the amount of program arguments given.

  • No accumulation of JAVA_TOOL_OPTIONS options.
  • Automatically finds main class.
  • Inherits current stdout/stderr.

public static void main(String[] args) throws Exception {
    if (args.length == 0)
        return;
    else
        args = Arrays.copyOf(args, args.length - 1);

    List<String> command = new ArrayList<>(32);
    appendJavaExecutable(command);
    appendVMArgs(command);
    appendClassPath(command);
    appendEntryPoint(command);
    appendArgs(command, args);

    System.out.println(command);
    try {
        new ProcessBuilder(command).inheritIO().start();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

private static void appendJavaExecutable(List<String> cmd) {
    cmd.add(System.getProperty("java.home") + File.separator + "bin" + File.separator + "java");
}

private static void appendVMArgs(Collection<String> cmd) {
    Collection<String> vmArguments = ManagementFactory.getRuntimeMXBean().getInputArguments();

    String javaToolOptions = System.getenv("JAVA_TOOL_OPTIONS");
    if (javaToolOptions != null) {
        Collection<String> javaToolOptionsList = Arrays.asList(javaToolOptions.split(" "));
        vmArguments = new ArrayList<>(vmArguments);
        vmArguments.removeAll(javaToolOptionsList);
    }

    cmd.addAll(vmArguments);
}

private static void appendClassPath(List<String> cmd) {
    cmd.add("-cp");
    cmd.add(ManagementFactory.getRuntimeMXBean().getClassPath());
}

    private static void appendEntryPoint(List<String> cmd) {
    StackTraceElement[] stackTrace          = new Throwable().getStackTrace();
    StackTraceElement   stackTraceElement   = stackTrace[stackTrace.length - 1];
    String              fullyQualifiedClass = stackTraceElement.getClassName();
    String              entryMethod         = stackTraceElement.getMethodName();
    if (!entryMethod.equals("main"))
        throw new AssertionError("Entry point is not a 'main()': " + fullyQualifiedClass + '.' + entryMethod);

    cmd.add(fullyQualifiedClass);
}

private static void appendArgs(List<String> cmd, String[] args) {
    cmd.addAll(Arrays.asList(args));
}

V1.1 Bugfix: null pointer if JAVA_TOOL_OPTIONS is not set


Example:

$ java -cp Temp.jar Temp a b c d e
[/usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java, -cp, Temp.jar, Temp, a, b, c, d]
[/usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java, -cp, Temp.jar, Temp, a, b, c]
[/usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java, -cp, Temp.jar, Temp, a, b]
[/usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java, -cp, Temp.jar, Temp, a]
[/usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java, -cp, Temp.jar, Temp]
$

System.err.println("Someone is Restarting me...");
setVisible(false);
try {
    Thread.sleep(600);
} catch (InterruptedException e1) {
    e1.printStackTrace();
}
setVisible(true);

I guess you don't really want to stop the application, but to "Restart" it. For that, you could use this and add your "Reset" before the sleep and after the invisible window.

참고URL : https://stackoverflow.com/questions/4159802/how-can-i-restart-a-java-application

반응형