简体   繁体   中英

using java not javascript, open URL in same tab of default browser

I have written a java script whose work is to open URL's mentioned in the text file and it should repeat this process continuously but the problem is as soon as it reads the new URL mention in next line of text file it opens that URL in new tab but I want to open all the URL's in same tab of chrome browser.

Code what I have written:

while(true){ 
    BufferedReader buf = new BufferedReader(new FileReader("C:\\link.txt"));
    String currentLine = null;
    while((currentLine = buf.readLine())!=null){
        System.out.print(currentLine+"\n");
         Desktop.getDesktop().browse(new URL(currentLine).toURI());
         Thread.sleep(10000);
    }
    }
}

Is there any other option then Desktop.getDesktop() that will also work

My text file has two links like this:

https://www.google.co.in/?gfe_rd=cr&ei=y8a_WPTUFLOl8weF8bK4DQ
https://in.yahoo.com/

How to open them in same tab?

 Desktop.getDesktop().browse()

Does NOT support this, as seen in Can Java's Desktop.browse provide an HTML Target in order to reuse a browser window?

Though, alternatives for this are the use of Process as seen in

Process oProc = Runtime.getRuntime().exec( currentLine );

And then make use of this process to send more commands to.

Or make use of a library / API such as Browserlaunch

The issue with Chrome, however is that Chrome likes to open new tabs in a new screen. If you hit ctrl + N (new tab on a lot of browsers) on Chrome, it'll open up a new screen instead

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM