Answer the question
In order to leave comments, you need to log in
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
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);
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/");
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question