8
8
8Yurasik82016-02-12 16:10:19
Java
8Yurasik8, 2016-02-12 16:10:19

How to open a non-default browser from a Java program, such as Tor?

There is a need to open not the default browser from the program code, but the Tor browser, but on the Internet I found only how to pass the URL to the browser, which is set as the default browser. Thanks in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Dorofeev, 2016-02-12
@8Yurasik8

There are two ways:

String pathToTor = "D:\\Tor\\Tor Browser\\Browser\\firefox.exe"; //путь к браузеру 
        String link = "http:\\stackoverflow.com\\"; //ссылка
        String request = String.format("%s %s", pathToTor, link);
        Runtime runtime = Runtime.getRuntime();
        runtime.exec(request);

Use my class (drafted in haste):
public class BrowserOpener {

    private String os;
    private Runtime runtime;

    public BrowserOpener() {

        os = System.getProperty("os.name").toLowerCase();
        runtime = Runtime.getRuntime();
    }

    public void openLinkInBrowser(String url) {

        try {
            if (isWindows()) {
                runtime.exec("rundll32 url.dll,FileProtocolHandler " + url);
            } else if (isMac()) {
                runtime.exec("open " + url);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private boolean isWindows() {
        return os.contains("win");
    }

    private boolean isMac() {
        return os.contains("mac");
    }


}

import java.io.IOException;
import java.net.URISyntaxException;

public class Main {

    public static void main(String[] args) throws IOException, URISyntaxException {

        BrowserOpener opener = new BrowserOpener();
        opener.openLinkInBrowser("http://stackoverflow.com/");


    }


}

  • Absolute path not needed
  • Offers a choice to the user
- Launches something other than Tor

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question