Run command line from java?

Hi, I want to run something from the command line 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 soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard

I tried, but I am not able to do it!

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(); } } 
+4
source share
6 answers

Finally, I decided 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 you all for your support!!

+3
source

Do not 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"}); 
+6
source

@Harinder: I would suggest an alternative method. What you can do is:

  • First, try to run everything you want to run from the command line directly with all attributes, etc. After you have successfully started the service / application from the command line, do 2.

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

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

  • Now modify the Java java code to execute this script, which in turn will execute the oral application or service.

For instance:

  ProcessBuilder builder = new ProcessBuilder(new String[]{"cmd", "/c","C:\\app.bat"}); Process pr = builder.start(); BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream())); 
  • if even that doesn't work ... we need to start from scratch again.
+2
source

I edited the code (see below) using the process builder method. See if this works for you. Using exec sometimes fails 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(); } } 

}

+1
source

I think I found your mistake: change your argument to the following: See if this 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"}) 
+1
source

An output status of 0 usually means no error.

Try using ProcssBuilder instead.

In ProcessBuilder you can set the working directory .

Here are some links that may help.

0
source