Reputation: 11944
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();
}
}
Upvotes: 4
Views: 10125
Reputation: 11944
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!!
Upvotes: 3
Reputation: 496
@Harinder : I would like to suggest an alternative method. What u can do is ;
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.
Go and save the command in a .bat file.
For example: C:\m-admin\app.exe I saved this as app.bat on C:\
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()));
Upvotes: 2
Reputation: 50147
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"});
Upvotes: 6
Reputation: 496
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"})
Upvotes: 1
Reputation: 496
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();
}
}
}
Upvotes: 1