4

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();
        }
    }
Harinder
  • 11,776
  • 16
  • 70
  • 126

6 Answers6

6

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"});
Thomas Mueller
  • 48,905
  • 14
  • 116
  • 132
3

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
  • 11,776
  • 16
  • 70
  • 126
2

@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.
knurdy
  • 496
  • 6
  • 18
1

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();
    }
}

}

knurdy
  • 496
  • 6
  • 18
  • i want C:\\Program Files\\OpenOffice.org 3\\program\\ to be my directory when i run soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard – Harinder Jun 08 '11 at 05:31
1

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"})
knurdy
  • 496
  • 6
  • 18
  • I think u was correct about changing the directory first. See the above code again...I made some edits to the original thing i posted... – knurdy Jun 08 '11 at 05:47
  • @Harinder : I am making my last effort, I have done some edits above...try this one for me... – knurdy Jun 08 '11 at 05:57
  • if i remove "cmd", "/c", and then run this i get Cannot run program "C:\Program Files\OpenOffice.org 3\program": CreateProcess error=5, Access is denied – Harinder Jun 08 '11 at 05:57
  • the following works for me: `new String[]{"cmd", "/c","C:\\m-admin\\app.exe"}` did u got it running?? if yes do post your solution – knurdy Jun 08 '11 at 06:11
  • @knurdy you forgot the `.exe` after `soffice` in your answer (well I did as well...) – Thomas Mueller Jun 08 '11 at 06:59
  • even ProcessBuilder builder = new ProcessBuilder(new String[]{"cmd", "/c", "C:\\Program Files\\OpenOffice.org 3\\program\\soffice.exe"}); doesnot open my application – Harinder Jun 08 '11 at 07:09
0

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.

Community
  • 1
  • 1
OscarRyz
  • 196,001
  • 113
  • 385
  • 569
  • BTW, if you're trying to launch the app you may also try `Desktop.open()` but I'm not sure if that's what you need. – OscarRyz Jun 08 '11 at 05:28
  • i want C:\\Program Files\\OpenOffice.org 3\\program\\ to be my directory when i run soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard – Harinder Jun 08 '11 at 05:31
  • na actually i am trying to run a service – Harinder Jun 08 '11 at 05:31