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();
}
}
-
1What sort of error are you getting? It's possible that soffice is not in you system path.joekarl– joekarl2011年06月08日 05:12:11 +00:00Commented Jun 8, 2011 at 5:12
-
please post the minimum complete code that exhibits the problem behavior, and also post the stacktrace.jcomeau_ictx– jcomeau_ictx2011年06月08日 05:12:46 +00:00Commented Jun 8, 2011 at 5:12
-
@joekarl Exited with error code 0Harinder– Harinder2011年06月08日 05:16:26 +00:00Commented Jun 8, 2011 at 5:16
-
@jcomeau-ictx na its there when i run it diretely from my comman promptHarinder– Harinder2011年06月08日 05:17:00 +00:00Commented Jun 8, 2011 at 5:17
6 Answers 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"});
3 Comments
.exe, could you try again with this new line? By the way you can not and do not need to use cd here.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!!
Comments
@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:\
- 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()));
- if even this does not work ...we need to start from scratch again.
Comments
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();
}
}
}
1 Comment
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"})
6 Comments
new String[]{"cmd", "/c","C:\\m-admin\\app.exe"} did u got it running?? if yes do post your solution.exe after soffice in your answer (well I did as well...)Exit status 0 usually means no error.
Try using ProcssBuilder instead.
With ProcessBuilder you can set the working directory.
3 Comments
Desktop.open() but I'm not sure if that's what you need.