I need code that will open url and close it after some random time(3-10minutes)
Something like:
Desktop d = Desktop.getDesktop();
d.browse(new URI("http://google.pl"));
d.wait(1000);
d.destroy(); //error there is no destroy function here
any ideas?
I looked into this some more, the Desktop class does not have a close() feature to it.
If you want to launch another program via Java you should use the Process class. Doing something similar to this:
import java.io.IOException;
public class Process {
public static void main(String[] args) {
try {
Runtime runTime = Runtime.getRuntime();
Process process = runTime.exec("notepad");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
process.destroy();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Related
I have a URL(like: "https://www.google.co.in/") that should be open on browser(on same browser where my application is running.) at run time from Java code.
Searched many thing and found below java code but it's not working for me.
Desktop desktop = java.awt.Desktop.getDesktop();
URI uri = new URI(String.valueOf("My_Url"));
desktop.browse(uri);
While using above code, i am getting below exception
java.awt.HeadlessException
at java.awt.Desktop.getDesktop(Desktop.java:142)
Also i have added and tried with below line of code with static block and without static block and getting same exception. Please suggest.
System.setProperty("java.awt.headless", "true");
use ProcessBuilder
List<String> list = new ArrayList<String>();
list.add("start");
list.add("www.google.com");
ProcessBuilder build = new ProcessBuilder(list);
build.start();
Perhaps this will work?
String url = "http://www.google.co.in";
if(Desktop.isDesktopSupported()){
Desktop desktop = Desktop.getDesktop();
try {
desktop.browse(new URI(url));
} catch (IOException | URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
Runtime runtime = Runtime.getRuntime();
try {
runtime.exec("xdg-open " + url);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
You can use Selenium Web Driver for opening a website into browser.
click here to see web driver setup how to setup
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class OpenSite
{
public static void main(String[] args) throws Exception
{
WebDriver driver=new FirefoxDriver();
// tries to open https://google.com
driver.get("https://google.com");
}
}
So I've been trying to automatically login through a WinSCP client. I have all the of credentials saved in the client for when I open it. If I manually press Enter on my keyboard, the client will log in. I tried using the robot method credited to #Slanec
Robot r = new Robot();
r.keyPress(KeyEvent.VK_ENTER);
r.keyRelease(KeyEvent.VK_ENTER);
However, when I checked Java API this just seems to check whether or not a key is pressed by a user?
Anyways... this is the code I currently have. The code opens the command prompt and enters a specified file path to open up a specific application WinSCP. Once the application is open I want the Enter Key to be pressed so that I can log in. If I manually press the enter key during this process it will work fine. SO is it possible to have the java code press the enter for me so that the process is fully automatic? I added the program to terminate in 5 seconds simply for testing purposes. Any input would be greatly appreciated.
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.io.IOException;
public class runADP
{
public static void main(String[] args)
{
try
{
System.out.println("Opening WinSCP");
Runtime runTime = Runtime.getRuntime();
Process process = runTime.exec("C:\\Program Files (x86)\\WinSCP\\WinSCP.exe");
try
{
Thread.sleep(5000);
Robot r = null;
try
{
r = new Robot();
r.keyPress(KeyEvent.VK_ENTER);
r.keyRelease(KeyEvent.VK_ENTER);
}
catch (AWTException e)
{
e.printStackTrace();
}
}
catch (InterruptedException e)
{
e.printStackTrace();
}
System.out.println("Closing WinSCP");
process.destroy();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
I figured out what was going on. The two keypress lines were being executed before the client could open. I simply added a thread.sleep command surrounded by a try and catch block and now the code works perfectly.
public void go()
{
try
{
System.out.println("Opening WinSCP");
Runtime runTime = Runtime.getRuntime();
Process process = runTime.exec("C:\\Program Files (x86)\\WinSCP\\WinSCP.exe");
Robot r = null;
try
{
Thread.sleep(2000);
r = new Robot();
r.keyPress(KeyEvent.VK_ENTER);
r.keyRelease(KeyEvent.VK_ENTER);
}
catch (AWTException | InterruptedException e)
{
e.printStackTrace();
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
I am having an issue running a command line string in Java. My code is below. When I extract the exact string I am running and paste it into the terminal it works perfectly. Am I missing something? I've condensed my code into this basic example, but the issue is the same.
import java.io.File;
import java.io.IOException;
public class TestCommandLine {
public static void main(String[] args) {
Runtime rt = Runtime.getRuntime();
Process pr = null;
try {
pr = rt.exec("/Users/comp/Desktop/executable /Users/comp/Desktop/115.png");
} catch (IOException e) {
e.printStackTrace();
}
try {
System.out.println(pr.waitFor());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
If I make a simple program that browses to google.com, the program doesn't exit automatically when reaching the end of the main method. I would rather not call System.exit(0), so I was wondering if there was a way to "close" the desktop thread so it can exit automagically.
public static void main(String[] args) {
try {
Desktop.getDesktop().browse(new URI("http://google.com"));
} catch (IOException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
This question already has answers here:
java.rmi.NoSuchObjectException: no such object in table
(7 answers)
Closed 7 years ago.
I have a problem with JAVA RMI.
I have a java ee application running, which uses rmi to call specific methods, which have to be implemented specificly for each customer.
The vendor gave me an example RMI interface implementation to show how to get a specific hostinterface running.
The original codefragment looked like posted example1.
The Interface is running but every 2 to 3 days it stops working, without any known reason. The interfaces' log looks like it would be still running.
The glassfish server, which calls the interface, shows log entries like this:
[#|2015-10-01T16:27:53.446+0200|SEVERE|glassfish3.1.2|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=106;_ThreadName=Thread-2;|egatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
...
The application error log shows entries like this:
2015-10-05 08:44:07,819 [http-thread-pool-8080(4)] ERROR medexter.arden.server.engine.DelegatingHostInterface - The method evaluateRead on remote interface does not work correctly.
java.rmi.NoSuchObjectException: no such object in table
at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(Unknown Source)
...
I found several threads like this one:
java.rmi.NoSuchObjectException: no such object in table
which tells me the garbage-collection could be responsible and the registry has to be kept statically to prevent the GC from destroying the object.
Meanwhile I tried out diffrent ways. My last one (which somehow shows my desparation - restarting the interface every 24h should not be the golden solution) is posted in example2.
At the moment I generate an executable jar file and start it as an application. I wanted to get it running as a service later on, but first it should work without any mistakes.
Does anybody hava an idea, what the reason for the described behaviour could be?
Any imporovement of the given code apprechiated. I just want to get this stuff working, without loosing connection every few days.
Thank you very much,
Martin
Example 1:
import java.rmi.AlreadyBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
public class SampleRMIProvider {
private static RmiRegistryThread thr;
public static void main(String[] args) {
SampleRMIProvider prv = new SampleRMIProvider();
prv.init();
}
public void init(){
SampleRMIProvider.thr = new RmiRegistryThread();
thr.start();
}
public class RmiRegistryThread extends Thread {
public boolean run = true;
#Override
public void run() {
System.err.println("thread start");
String name = "RMIHostInterface";
int servicePorti = Integer.parseInt("18989");
try {
RmiHostInterface engine = new RmiHostInterfaceImpl();
RmiHostInterface stub = (RmiHostInterface) UnicastRemoteObject.exportObject(engine, 0);
Registry registry;
try {
registry = LocateRegistry.createRegistry(servicePorti);
} catch (RemoteException e) {
registry = LocateRegistry.getRegistry(servicePorti);
}
registry.bind(name, stub);
} catch (RemoteException e) {
e.printStackTrace();
} catch (AlreadyBoundException e) {
e.printStackTrace();
}
try {
while (this.run) {
Thread.sleep(10000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
Example 2:
import java.rmi.AlreadyBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
public class SampleRMIProvider extends Thread{
private static Registry r = null;
#Override
public void run() {
String name = "RMIHostInterface";
int servicePorti = Integer.parseInt("20002"); //18989
try {
RmiHostInterface engine = new SampleInterfaceImpl();
RmiHostInterface stub = (RmiHostInterface) UnicastRemoteObject.exportObject(engine, 0);
try {
SampleRMIProvider.r = LocateRegistry.createRegistry(servicePorti);
} catch (RemoteException e) {
SampleRMIProvider.r = LocateRegistry.getRegistry(servicePorti);
}
SampleRMIProvider.r.bind(name, stub);
} catch (RemoteException e) {
e.printStackTrace();
} catch (AlreadyBoundException e) {
e.printStackTrace();
}
System.err.println("RMI Interface created...");
try{
while(true){
Thread.sleep(10000);
}
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
try {
while (true) {
//RMI Interface instanzieren
SampleRMIProvider provider = new SampleRMIProvider();
provider.start();
//Ein Tag pause
Thread.sleep(86400000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
14.10.2015
Finally - this minimal example shows a solution that worked for me. Thanks for the useful advices.
import java.rmi.AlreadyBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
public class SampleRMIProvider{
private static Registry r = null;
private static SampleHostInterfaceImpl hif = null;
private static RmiHostInterface stub = null;
SampleRMIProvider(){
try{
SampleRMIProvider.r.bind("RMIHostInterface", SampleRMIProvider.stub);
}
catch(RemoteException | AlreadyBoundException e){
e.printStackTrace();
}
}
public static void main(String[] args) {
//setup static references to prevent from beeing collected by GC
try {
SampleRMIProvider.hif = new SampleHostInterfaceImpl();
SampleRMIProvider.stub = (RmiHostInterface) UnicastRemoteObject.exportObject(SampleRMIProvider.hif, 0);
try {
SampleRMIProvider.r = LocateRegistry.createRegistry(20002);
} catch (RemoteException e) {
SampleRMIProvider.r = LocateRegistry.getRegistry(20002);
}
} catch (RemoteException e) {
e.printStackTrace();
}
new SampleRMIProvider();
}
}
I don't know what you mean by 'no known cause', when the cause is documented in the Javadoc of the exception.
You say that you've read that the Registry reference must be static, yet you don't have a static Registry reference anywhere.
Try implementing that.