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();
 }
 }
asked Jun 8, 2011 at 5:09
4
  • 1
    What sort of error are you getting? It's possible that soffice is not in you system path. Commented Jun 8, 2011 at 5:12
  • please post the minimum complete code that exhibits the problem behavior, and also post the stacktrace. Commented Jun 8, 2011 at 5:12
  • @joekarl Exited with error code 0 Commented Jun 8, 2011 at 5:16
  • @jcomeau-ictx na its there when i run it diretely from my comman prompt Commented Jun 8, 2011 at 5:17

6 Answers 6

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"});
answered Jun 8, 2011 at 5:12
Sign up to request clarification or add additional context in comments.

3 Comments

can i first change the directory and then run "soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard"
No you can't change the directory first
@Harinder I updated the answer to include .exe, could you try again with this new line? By the way you can not and do not need to use cd here.
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!!

answered Jun 10, 2011 at 7:31

Comments

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.
answered Jun 8, 2011 at 7:30

Comments

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

}

answered Jun 8, 2011 at 5:26

1 Comment

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
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"})
answered Jun 8, 2011 at 5:42

6 Comments

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...
@Harinder : I am making my last effort, I have done some edits above...try this one for me...
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
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 you forgot the .exe after soffice in your answer (well I did as well...)
|
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.

answered Jun 8, 2011 at 5:25

3 Comments

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.
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
na actually i am trying to run a service

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.