2

I am trying to invoke an HTML File stored on my desktop from inside a JAVA code as below.
I found this code snippet here

 try
 {
 Runtime r= Runtime.getRuntime();
 String url = "C:\\Users\\Rana\\Desktop\\test.html";
 String browser ="C:/Program Files/Mozilla Firefox/firefox.exe ";
 Process p = r.exec(browser);
 p.waitFor();
 }
 catch(Exception e)
 {
 e.printStackTrace();
 }

I tried using backslash and forward slashes, both. But it is throwing this error in both case....

java.io.IOException: Cannot run program "C:/Program": CreateProcess error=2, The system cannot find the file specified
 at java.lang.ProcessBuilder.start(ProcessBuilder.java:1041)
 at java.lang.Runtime.exec(Runtime.java:617)
 at java.lang.Runtime.exec(Runtime.java:450)
 at java.lang.Runtime.exec(Runtime.java:347)
 at package1.Test.main(Test.java:22)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
 at java.lang.ProcessImpl.create(Native Method)
 at java.lang.ProcessImpl.<init>(ProcessImpl.java:376)
 at java.lang.ProcessImpl.start(ProcessImpl.java:136)

The path "browser" is present.
Please suggest where I am doing wrong.

asked Nov 15, 2013 at 19:08
2
  • the first line says Cannot run program C:/Program -- which isn't what you are trying to run. I think the space in the path is making it not work. Commented Nov 15, 2013 at 19:13
  • stackoverflow.com/questions/1734424/… Commented Nov 15, 2013 at 19:14

6 Answers 6

2

The issue is with the space in your path to the browser. The system thinks you are trying to run a program called "C:/Program" with "Files/Mozilla" and "Firefox/firefox.exe" as the arguments. Try adding quotes around the exe name:

String browser ="\"C:/Program Files/Mozilla Firefox/firefox.exe\" ";

To incorporate SnakeDoc's advice, you could use the environment variable to take care of the portion of the path down to "Program Files", but you will still need the quotes to take care of any other spaces in the path:

String browser = "\"" + System.getenv("ProgramFiles(X86)") + "Mozilla Firefox/firefox.exe\"";
answered Nov 15, 2013 at 19:15
Sign up to request clarification or add additional context in comments.

6 Comments

or: String browser = System.getenv("ProgramFiles(X86)") + "Mozilla Firefox/firefox.exe";
Cool - didn't know there was an environment variable for Program Files. Might still have a problem with the space between "Mozilla" and "Firefox", correct?
not sure actually... i like using environment vars whenever possible, helps make my program more dynamic with less static/hard-coded things.
Agree - Definitely advocate the use of the env var - you may just have to quote the whole thing when you are done...
right, which should be easy like; String browser = "\"" + System.getenv("ProgramFiles(X86)") + "Mozilla Firefox/firefox.exe\"";
|
2

Try using the system properties:

System.getenv("ProgramFiles");

or

System.getenv("ProgramFiles(X86)");
answered Nov 15, 2013 at 19:15

Comments

1

Better advice,

use AutoHotKey for such tasks.You should give it a try.I promise you would definitely find it useful.

answered Nov 15, 2013 at 19:15

Comments

0

if you are using the 32bit version browser should be String browser ="C:/Program Files(x86)/Mozilla Firefox/firefox.exe ";

hope this helps

answered Nov 15, 2013 at 19:12

Comments

0

Try

String browser ="\"C:/Program Files/Mozilla Firefox/firefox.exe\"";

There is problem with space in Program files that get treated as separator if not inside Quotes. It is briliant idea by Microsoft...

answered Nov 15, 2013 at 19:19

Comments

0

The best way would be to do this with java.awt.Desktop

File htmlFile = new File("C:" + File.separator + "Users" + File.separator + "theuser" + File.separator + "Desktop" + File.separator + "Test.html");
if(Desktop.isDesktopSupported()) {
 Desktop.getDesktop().open(htmlFile);
}
answered Nov 15, 2013 at 19:27

Comments

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.