I'm having some trouble interacting with the system clipboard (Editing it, clearing it, etc).
I have the following code that when you build it and double click the Jar file, it opens up multiple browser windows for different sites of your choosing.
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.awt.datatransfer.*;
import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
//import clip.Clipboard.*;
public class main {
public static void main(String[] args) {
//Open Chrome
int keyControl = KeyEvent.VK_CONTROL;
int keyT = KeyEvent.VK_T;
// TODO Auto-generated method stub
try {
Process p = Runtime.getRuntime().exec("\"/Program Files (x86)/Google/Chrome/Application/chrome.exe\"");
p.waitFor();
System.out.println("Google Chrome launched!");
} catch (Exception e) {
e.printStackTrace();
}
try {
Robot robot = new Robot();
//Sites
String site = "www.youtube.com";
String site2 = "www.facebook.com";
//SITE 1 \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
//Load Url
StringSelection ss = new StringSelection(site);
Clipboard clpbrd = Toolkit.getDefaultToolkit().getSystemClipboard();
clpbrd.setContents(ss, null);
//Enter URL
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
//Clear Clipboard
String empty = "";
StringSelection ss2 = new StringSelection(empty);
clpbrd = Toolkit.getDefaultToolkit().getSystemClipboard();
clpbrd.setContents(ss2, null);
//SITE 2 \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
//Load Url
StringSelection ss3 = new StringSelection(site2);
clpbrd = Toolkit.getDefaultToolkit().getSystemClipboard();
clpbrd.setContents(ss3, null);
//Create new tab
robot.keyPress(keyControl);
robot.keyPress(keyT);
robot.keyRelease(keyControl);
robot.keyRelease(keyT);
//Enter URL
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
When I try running this program, it opens the tabs but every tab is the same link. So it seems to be running into an issue with the clipboard. This is my first experience using the clipboard in my code.
How do I make this work? I can't download any webdrivers/etc to solve the problem.
I have refactored your code a little bit into cleaner code without changing what you basically do in yours because your code works nicely for me. I just want to use this version as a basis for discussion:
package de.scrum_master.stackoverflow;
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
public class BrowserRemoteControl {
private static final Clipboard CLIPBOARD = Toolkit.getDefaultToolkit().getSystemClipboard();
private static final Robot ROBOT = createRobot();
public static void main(String[] args) throws IOException, InterruptedException, AWTException {
startChrome();
List<String> sites = Arrays.asList("youtube.com", "facebook.com");
for (String site : sites) {
setClipboardContent(site);
enterURLFromClipboard();
createNewBrowserTab();
}
}
private static Robot createRobot() {
try {
return new Robot();
} catch (AWTException e) {
throw new RuntimeException("Cannot create Robot instance", e);
}
}
private static void startChrome() throws IOException, InterruptedException {
Process process = Runtime.getRuntime().exec("\"C:/Program Files (x86)/Google/Chrome/Application/chrome.exe\"");
process.waitFor();
System.out.println("Google Chrome launched");
}
private static void setClipboardContent(String text) {
CLIPBOARD.setContents(new StringSelection(text), null);
}
private static void enterURLFromClipboard() throws InterruptedException {
// Ctrl-V
ROBOT.keyPress(KeyEvent.VK_CONTROL);
ROBOT.keyPress(KeyEvent.VK_V);
ROBOT.keyRelease(KeyEvent.VK_V);
ROBOT.keyRelease(KeyEvent.VK_CONTROL);
// Enter
ROBOT.keyPress(KeyEvent.VK_ENTER);
ROBOT.keyRelease(KeyEvent.VK_ENTER);
// Wait a little while
Thread.sleep(250);
}
private static void createNewBrowserTab() {
// Ctrl-T
ROBOT.keyPress(KeyEvent.VK_CONTROL);
ROBOT.keyPress(KeyEvent.VK_T);
ROBOT.keyRelease(KeyEvent.VK_CONTROL);
ROBOT.keyRelease(KeyEvent.VK_T);
}
}
I hope you do not mind the extra empty browser tab at the end, but I wanted to avoid making an exception for the last site.
As your code works for me even without cleaning the clipboard in between, I guess you have some kind of timing problem or race condition.
Update: I managed to reproduce the problem once (and only once) just now. So I just added a Thread.sleep()
after pasting the URL and pressing Enter in order to show you how you can do the same in all places that might cause you problems.
Feel free to ask follow-up questions.