0

Im writing an application. There the app has to open firefox when certain req are met. I did a small research. All i can find is the following code.

Runtime rt = null;
rt = Runtime.getRuntime();

try {
    rt.exec("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe google.com");
} catch (IOException e) {
    e.printStackTrace();
}

What i want to know is that is that any method to open a specific application without giving the path because all users wont install the application in same path. So like searching with the name only. Please help. Thanks in advance.

1
  • Are you happy to load the default browser to open your URL? Commented Sep 10, 2014 at 12:27

4 Answers 4

4

You may use the Desktop class, find more in detail doc here

        URI uri = null;
        try {
            uri = new URI("http://www.google.com");
            desktop.browse(uri);
        } catch(IOException ioe) {
            System.out.println("The system cannot find the " + uri + 
                " file specified");
            //ioe.printStackTrace();
        } catch(URISyntaxException use) {
            System.out.println("Illegal character in path");
            //use.printStackTrace();
        }
Sign up to request clarification or add additional context in comments.

3 Comments

+1 This is the best solution if you want to open the browser the user prefers. See How to open the default webbrowser using java for further info.
that is helpful. But im working with test automation. So there has to be a defined web browser. Thanks for the help.
@Vithushan, with this default web browser will be launched, kindly accept the answer if it worked for you.
1

Giving the name of the application and providing an automatic search is really a Herculean task. It has got so many complexities involved:

  • What kind of Operating System, the user has (Windows has different path style and Unix based systems have different)
  • Should the search be in a particular folder or the entire computer? Where to search???

I would give you a simple suggestion, if you are developing a GUI apllication, allow the user to give the path and set a button ("Set Default Path") and allow user to click on. So, the default path will be stored in a string variable and you can call it from the `exec()`

1 Comment

yeah i thought of doing like that. But already the requirement has so many selections to do. So i thought this wont be user friendly. Anyway thanks for the advice. I appreciate it.
1

Searching of executable in path is the responsibility of shell. This mechanism is invoked when you execute command from command prompt or from OS launcher. To enable this mechanism from java you should run the application through cmd on Windows or /bin/sh on Linux.

In your case change your code to

rt.exec("cmd /c chrome.exe google.com")

EDIT 1. this will work whether chrome is the default browser or not. 2. This will NOT work if chrome is not available in system path (e.g. if it is not installed).

So, then it depends on what do you really want. If you have to show URL in default browser use desktop.browse(uri); (as it was already mentioned by @Arvind). If however you really want to open specific application (whether it is browser or not) use methods I suggested you.

Comments

1

It's actually quite complex, you can use feature provided by nio package in Java 7:

The new java.nio.file.Files class provides a factory method, walkFileTree, you can use to traverse a tree of directories and files.

to find out more go directly to the blog I just quoted: https://blogs.oracle.com/thejavatutorials/entry/traversing_a_file_tree_in

2 Comments

Thanks. Ill have a look at this. (y)
How do I write a good answer? Provide context for links - Links to external resources are encouraged, but please add context around the link so your fellow users will have some idea what it is and why it’s there. Always quote the most relevant part of an important link, in case the target site is unreachable or goes permanently offline.

Your Answer

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.