Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run command prompt from java?

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();
        }
    }
like image 331
Harinder Avatar asked Dec 13 '22 12:12

Harinder


1 Answers

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"});
like image 126
Thomas Mueller Avatar answered Jan 02 '23 13:01

Thomas Mueller