开发者

Run command prompt from java?

开发者 https://www.devze.com 2023-03-11 08:50 出处:网络
Hi i want to run something from command prompt using java i want to go to the following directory C:\\Program Files\\OpenOffice.org 3\\program\\

Hi i want to run something from command prompt using java

i want to go to the following directory C:\Program Files\OpenOffice.org 3\program\ and then run soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard

i tried but i am not able to do that!

my code

public static void main(String[] args) {
        // TODO Auto-generated method stub
        try {
            Runtime rt = Runtime.getRuntime();
            //Process pr = rt.exec("cmd /c dir");

           // Process pr = rt.exec("cmd /c dir");

            Process pr = rt.exec(new String[]{"C:\\Program Files\\OpenOffice.org 3\\program\\soffice", 
                    "-headless",
                    "-accept='socket,host=127.0.0.1,port=8100;urp;'",
                    "-nofirststartwizard"});

            BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));

       开发者_如何学运维     String line=null;

            while((line=input.readLine()) != null) {
                System.out.println(line);
            }

            int exitVal = pr.waitFor();
            System.out.println("Exited with error code "+exitVal);

        } catch(Exception e) {
            System.out.println(e.toString());
            e.printStackTrace();
        }
    }


Don't use cd, and use the string array method:

rt.exec(new String[]{"C:\\Program Files\\OpenOffice.org 3\\program\\soffice.exe", 
    "-headless",
    "-accept='socket,host=127.0.0.1,port=8100;urp;'",
    "-nofirststartwizard"});


Finally i solved it

String[] SOFFICE_CMD = { "C:/Program Files/OpenOffice.org 3/program/soffice", "-accept=socket,host=localhost,port=8100;urp;StarOffice.ServiceManager", "-invisible", "-nologo"}; 
        Runtime.getRuntime().exec(SOFFICE_CMD); 

Thank u all for supporting!!


@Harinder : I would like to suggest an alternative method. What u can do is ;

  1. First try to run whatever u want to run from the command prompt directly with all attributes etc. Once u have successfully run the service/application from the command prompt directly do 2.

  2. Go and save the command in a .bat file.

For example: C:\m-admin\app.exe I saved this as app.bat on C:\

  1. Now modify ur java code accordingly to execute this script which will in turn execute ur application or service.

For example:

 ProcessBuilder builder = new ProcessBuilder(new String[]{"cmd", "/c","C:\\app.bat"});
 Process pr =   builder.start();
 BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));
  1. if even this does not work ...we need to start from scratch again.


I have edited the code(below) using the process builder method. See if this works for you. Using exec sometimes does not work due to access violations:

public static void main(String[] args) {
    // TODO Auto-generated method stub
    try {
        Runtime rt = Runtime.getRuntime();
        //Process pr = rt.exec("cmd /c dir");

       // Process pr = rt.exec("cmd /c dir");
       ProcessBuilder builder = new ProcessBuilder(new String[]{"cmd", "/c", "C:\\Program Files\\OpenOffice.org 3\\program", "soffice",
        "-headless",
        "-accept='socket,host=127.0.0.1,port=8100;urp;'",
        "-nofirststartwizard"});
       Process pr =   builder.start();
       BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));


        String line=null;

        while((line=input.readLine()) != null) {
            System.out.println(line);
        }

        int exitVal = pr.waitFor();
        System.out.println("Exited with error code "+exitVal);

    } catch(Exception e) {
        System.out.println(e.toString());
        e.printStackTrace();
    }
}

}


I think I have found your mistake: change your argument to the following: See if it works:

(new String[]{"cmd", "/c", "C:\\Program Files\\OpenOffice.org 3\\program\\soffice",
            "-headless",
            "-accept='socket,host=127.0.0.1,port=8100;urp;'",
            "-nofirststartwizard"})


Exit status 0 usually means no error.

Try using ProcssBuilder instead.

With ProcessBuilder you can set the working directory.

Here are some links that might help.

0

精彩评论

暂无评论...
验证码 换一张
取 消