Java (Commandline에서 작동하지만 Java에서는 그렇지 않은 명령)에서 OpenOffice Service (Soffice)를 시작하는 문제

StackOverflow https://stackoverflow.com/questions/378338

문제

나는 쉘에서 작동하지만 Java에서 작동하지 않는 간단한 명령을 내리고 싶습니다. 이것이 제가 실행하고 싶은 명령입니다.

soffice -headless "-accept=socket,host=localhost,port=8100;urp;" 

이것은 내가이 명령을 실행하려는 Java에서 발굴중인 코드입니다.

String[] commands = new String[] {"soffice","-headless","\"-accept=socket,host=localhost,port=8100;urp;\""};
Process process = Runtime.getRuntime().exec(commands)
int code = process.waitFor();
if(code == 0)
    System.out.println("Commands executed successfully");

이 프로그램을 실행하면 "명령이 성공적으로 실행"됩니다. 그러나 프로그램이 완료되면 프로세스가 실행되지 않습니다. JVM이 프로그램이 운영 된 후 프로그램을 죽일 수 있습니까?

왜 이것이 효과가 없습니까?

도움이 되었습니까?

해결책 2

나는 이것을 어떻게 해결했는지 말하고 싶습니다. 기본적으로 Soffice의 명령을 실행하는 SH 스크립트를 만들었습니다.

그런 다음 Java에서 방금 스크립트를 실행하면 다음과 같이 잘 작동합니다.

public void startSOfficeService() throws InterruptedException, IOException {
        //First we need to check if the soffice process is running
        String commands = "pgrep soffice";
        Process process = Runtime.getRuntime().exec(commands);
        //Need to wait for this command to execute
        int code = process.waitFor();

        //If we get anything back from readLine, then we know the process is running
        BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
        if (in.readLine() == null) {
            //Nothing back, then we should execute the process
            process = Runtime.getRuntime().exec("/etc/init.d/soffice.sh");
            code = process.waitFor();
            log.debug("soffice script started");
        } else {
            log.debug("soffice script is already running");
        }

        in.close();
    }

또한이 방법을 호출하여 소파 과정을 죽입니다.

public void killSOfficeProcess() throws IOException {
        if (System.getProperty("os.name").matches(("(?i).*Linux.*"))) {
            Runtime.getRuntime().exec("pkill soffice");
        }
    }

이것은 Linux에서만 작동합니다.

다른 팁

내가 착각하지 않았는지 확실하지 않지만, 당신이 명령을 생성하고 있지만 "실행"메소드로 전달하지 않는 한, 당신은 실행 중입니다. "

runtime.getRuntime (). exec (명령) =)를 사용해보십시오.

나는 당신이 인용을 올바르게 처리하지 않는다고 생각합니다. 원래 SH 명령 줄에는 쉘이 세미콜론을 해석하는 것을 방지하기위한 이중 인용문이 포함되어 있습니다. 쉘은 소파 과정이 그들을보기 전에 그들을 벗겨냅니다.

Java 코드에서 쉘은 인수를 볼 수 없으므로 여분의 이중 인용문 (백 슬래시로 탈출)은 필요하지 않으며 아마도 소파를 혼란스럽게 할 것입니다.

다음은 추가 인용문이 제거 된 코드입니다 (그리고 세미콜론이 던져졌습니다)

String[] commands = new String[] {"soffice","-headless","-accept=socket,host=localhost,port=8100;urp;"};
Process process = Runtime.getRuntime().exec(commands);
int code = process.waitFor();
if(code == 0) 
    System.out.println("Commands executed successfully");

(면책 조항 : 나는 Java를 모른다. 그리고 나는 이것을 테스트하지 않았다!)

"/Applications/OpenOffice.org\ 2.4.app/Contents/MacOS/soffice.bin -headless -nofirststartwizard -accept='socket,host=localhost,port=8100;urp;StartOffice.Service'"

또는 단순히 인용문을 피하는 것도 효과가 있습니다. 우리는 이와 같은 명령을 ANT 스크립트에 공급하는데, 궁극적으로 위에서와 같이 exec 전화로 끝납니다. 또한 OOO가 메모리를 제대로 무료로 사용하지 않기 때문에 (실행중인 버전에 따라) 500 개 정도의 전환마다 프로세스를 다시 시작하는 것이 좋습니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top